Download C++ PDF

TitleC++
File Size5.8 MB
Total Pages566
Table of Contents
                            The Context of Software Development
	Software
	Development Tools
	Learning Programming with C++
	Summary
	Exercises
Writing a C++ Program
	General Structure of a Simple C++ Program
	Compiling the source code
	Variations of our simple program
	Template for simple C++ programs
	Summary
	Exercises
Values and Variables
	Integer Values
	Variables and Assignment
	Identifiers
	Floating-point Types
	Constants
	Other Numeric Types
	Characters
	Enumerated Types
	Type Inference with auto
	Summary
	Exercises
Expressions and Arithmetic
	Expressions
	Mixed Type Expressions
	Operator Precedence and Associativity
	Comments
	Formatting
	Errors and Warnings
		Compile-time Errors
		Run-time Errors
		Logic Errors
		Compiler Warnings
	Arithmetic Examples
	More Arithmetic Operators
	Algorithms
	Summary
	Exercises
Conditional Execution
	Type bool
	Boolean Expressions
	The Simple if Statement
	Compound Statements
	The if/else Statement
	Compound Boolean Expressions
	Nested Conditionals
	Multi-way if/else Statements
	Errors in Conditional Statements
	Summary
	Exercises
Iteration
	The while Statement
	Nested Loops
	Abnormal Loop Termination
		The break statement
		The goto Statement
		The continue Statement
	Infinite Loops
	Iteration Examples
		Drawing a Tree
		Printing Prime Numbers
	Summary
	Exercises
Other Conditional and Iterative Statements
	The switch Statement
	The Conditional Operator
	The do/while Statement
	The for Statement
	Summary
	Exercises
Using Functions
	Introduction to Using Functions
	Standard Math Functions
	Maximum and Minimum
	clock Function
	Character Functions
	Random Numbers
	Summary
	Exercises
Writing Functions
	Function Basics
	Using Functions
	Call by Value
	Function Examples
		Better Organized Prime Generator
		Command Interpreter
		Restricted Input
		Better Die Rolling Simulator
		Tree Drawing Function
		Floating-point Equality
		Multiplication Table with Functions
	Commenting Functions
	Custom Functions vs. Standard Functions
	Summary
	Exercises
More on Functions
	Global Variables
	Static Variables
	Overloaded Functions
	Recursion
	Making Functions Reusable
	Pointers
	Call by Reference
		Call by Reference via Pointers
		Call by Reference via References
	Function Pointers
	Summary
	Exercises
Arrays
	Declaring and Using Arrays
	Arrays and Functions
	Prime Generation with an Array
	Multidimensional Arrays
	Pointers and Arrays
	Array Ranges
	C Strings
	Dynamic Arrays
	The sizeof Operator
	Copying an Array
	Memory Management
	Summary
	Exercises
Array Algorithms
	Sorting
	Flexible Sorting
	Search
		Linear Search
		Binary Search
	Array Permutations
	Randomly Permuting an Array
	Summary
	Exercises
Standard C++ Classes
	String Objects
	Vectors
		Declaring and Using Vectors
		Copying Vectors
		Vectors and Arrays
	Input/Output Streams
	File Streams
	Complex Numbers
	Summary
	Exercises
Custom Objects
	Object Basics
	Fields
	Methods
	Constructors
	Destructors
	Defining a New Numeric Type
	Encapsulation
	Summary
	Exercises
More on Objects
	Pointers to Objects and Object Arrays
	The this Pointer
	const Methods
	Separating Method Declarations and Definitions
	Preventing Multiple Inclusion
	Overloaded Operators
		Operator Functions
		Operator Methods
	static Members
	Classes vs. structs
	Summary
	Exercises
Building some Useful Classes
	A Better Rational Number Class
	Stopwatch
	Sorting with Logging
	Linked Lists
	Automating Testing
	Summary
	Exercises
Inheritance and Polymorphism
	I/O Stream Inheritance
	Inheritance Mechanics
	Uses of Inheritance
	Polymorphism
	Protected Members
	Fine Tuning Inheritance
	Summary
	Exercises
Generic Programming
	Function Templates
	Class Templates
	The Standard Template Library
	Iterators
	Summary
	Exercises
Exception Handling
	Summary
	Exercises
Appendices
Visual C++ Command Line Development
Developing C++ Programs under Unix/Linux with the GNU Tools
Introduction to Binary Numbers and Computer Arithmetic
	The Integer Types
	The Floating-Point Types
Bitwise Operators
Bibliography
Index
                        
Document Text Contents
Page 1

Fundamentals of

Programming

C++

DR
AF

T

Richard L. Halterman
School of Computing

Southern.Adventist University

January 27, 2013

Page 2

Copyright ' 2013 Richard L. Halterman. All rights reserved.

Page 283

11.1. DECLARING AND USING ARRAYS 272

5 -3 12

0 1 2

l i s t

l i s t [ 2] = 12;

l i s t [ 1] = - 3;

l i s t [ 0] = 5;

i nt l i s t [ 3] ;

Figure 11.1: A simple array with three elements

list[1] = -3 ; // Make the second element -3

is a similar situation. The expression to the left of the assignment operator is not a simple, single variable
name. This is legal because the expression to the left of the assignment operator refers to a memory location
that can store a value. The memory location it identifies is a part of the memory reserved for the list array.

The array square brackets operator has two distinct meanings depending on the
context:

1. At the array’s declaration, for example,

double nums[10];

the number within the square brackets specifies the number of elements that
the array can hold. The compiler uses this number along with the type of
the array to determine how much memory to allocate for the array.

2. After the array’s declaration, for example,

nums[3] = 10.45;

the number within the square brackets represents an index locating a specific
element within the memory allocated to the array.

Declarations of arrays and non-array values can be interspersed, as in:

int list[5], x, s[3], *p, reg;

Here, list and s are integer arrays, but x and reg are simple integer variables, and p is a pointer to an
integer (see Section 10.6, for a discussion of pointers).

The contents of an array may be initialized when it is declared. The following code fragment declares
three arrays and specifies their initial contents:

©2013 Richard L. Halterman Draft date: January 27, 2013

Page 284

11.1. DECLARING AND USING ARRAYS 273

int list[] = { 2, 4, 6, 8 };
double collection[10] = { 1.0, 3.5, 0.5, 7.2 };
char letters[] = { 'a', 'b', 'c' };

The elements of the array are provided by a comma-separated list within curly braces. This comma sepa-
rated list of initial values is called an array initialization list. When an array initialization list is provided,
the square brackets can be left empty as the compiler can calculate the array’s size based on the number of
elements in the list. If a number is provided within the square brackets, the array is allocated with the spec-
ified size, and the first positions within the array are filled with the elements specified in the initialization
list.

The code fragment above declares three arrays: list, collection, and letters. The list array
is created to hold four integer values consisting of 2, 4, 6, and 8. The collection array is created to hold
10 double-precision floating-point values, and the first four elements consist of 1.0, 3.5, 0.5, and 7.2. The
letters array holds the three characters shown. Figure 11.2 visualizes the contents of the three arrays.

2 4 6 8

0 1 2 3

list

1.0 3.5 0.5 7.2 ? ? ? ? ? ?

0 1 2 3 4 5 6 7 8 9

collection

double collection[10] = { 1.0, 3.5, 0.5, 7.2 };

int list[] = { 2, 4, 6, 8 };

‘a’ ‘b’ ‘c’

0 1 2

letters

char letters[] = { 'a', 'b', 'c' };

Figure 11.2: Array declarations and memory layout

We can observe three key points from Figure 11.2:

• An array name represents the location of the first element in an array. For an array named list, the
expressions list and &list[0] within C++ source code are equivalent.

• Elements in an array are stored in a contiguous block of memory. This means, for example, the
memory occupied by the element at index 3 follows immediately after the memory occupied by the

©2013 Richard L. Halterman Draft date: January 27, 2013

Page 565

554

Index

abstract class, 487
actual parameter, 195
alias, 302
and, bitwise, 549
and, logical, 86
array initialization list, 273
assignment, 23
associativity, operator, 50
auxiliary methods, 397

binary search, 322
bitwise and, 549
bitwise exclusive or, 550
bitwise left shift, 551
bitwise negation, 550
bitwise operators, 549
bitwise or, 550
bitwise right shift, 551
bitwise shift operators, 551
bitwise xor, 550
break statement, 126
buffer overrun, 292

call by reference, 254
call by value, 201
case label in a switch statement, 149
class, 376
class derivation, 468
class specialization, 468
class template, 516
client, 348
coincidental correctness, 465
compiler, 2
concrete class, 487
constructor, 390
constructor initialization list, 391
continue statement, 129

data members, 377
default constructor, 392
delegation, method, 475
derivation, class, 468

destructor, 392
dot operator (.), 379
dynamic binding, 480

early binding, 480
encapsulation, 399
enumerated type, 34
enumeration type, 34
exclusive or, bitwise, 550
explicit template instantiation, 515

fields, 377
floating-point types, 544
for statment, 155
function call, 169
function definition, 189
function invocation, 169
function overloading, 238

generic function, 512
goto statement, 127

helper methods, 397
homogeneous, 271

if statement, 78
inheritance, 468
instance, 377
integer types, 539
is-a relationship, 469
iteration, 107
iterator, 358, 526

late binding, 480
left shift, 551
linear search, 321
linker, 4
Liskov Substitution Principle, 469
logical and, 86
logical negation, 86
logical not, 86
logical or, 86
loop, 107

©2013 Richard L. Halterman Draft date: January 27, 2013

Page 566

INDEX 555

loop unrolling, 334

matrix, 284
member function, 348, 388
memory leak, 298, 410
method, 348
method delegation, 475
multiple inheritance, 471

negation, bitwise, 550
negation, logical, 86
nested loop, 117
nibble, 541
non-type parameters, template, 515
non-type template parameters, 515
not, logical, 86

object slicing, 477
operator associativity, 50
operator precedence, 50
or, logical, 86
overflow, 542
overloaded function, 238
override, 475

parameter, actual, 195
pass by reference, 254
pass by value, 201
polymorphism, 483
precedence, operator, 50
private inheritance, 471
private members, 386
profiler, 5
protected member, 485
public members, 377
pure virtual methods, 487

ranged-based array parameter passing, 291
rational number, 394
real number, 544
return statement, 194
right shift, 551

shift, bitwise, 551
short-circuit evaluation, 88
specialization, class, 468
static binding, 480
steam manipulator, 114
string concatenation, 351
string objects, 348
struct, 434
subclassing, 468

switch statement, 147
syntactic sugar, 350

template function, 511
template instantiation, 512
template value parameters, 515
this, 410
Two’s complement binary, 541
two-dimensional array, 284
typedef, 285

underflow, 542

value parameters, template, 515

while statement, 108

xor, bitwise, 550

©2013 Richard L. Halterman Draft date: January 27, 2013

Similer Documents