Top Banner
Review of C++ Programm ing Part I Sheng-Fang Huang
106

Review of C++ Programming Part I Sheng-Fang Huang.

Jan 18, 2018

Download

Documents

Elinor Newton

Random Number Generation To produce integers in a specific range, use the modulus operator ( % ) with rand –Example rand() % 6; –Produces numbers in the range 0 to 5 –This is called scaling, 6 is the scaling factor –Shifting can move the range to 1 to rand() % 6;
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Review of C++ Programming Part I Sheng-Fang Huang.

Review of C++ ProgrammingPart I

Sheng-Fang Huang

Page 2: Review of C++ Programming Part I Sheng-Fang Huang.

Random Number Generation

• C++ Standard Library function rand – Introduces the element of chance into computer ap

plications– Example

• i = rand();– Generates an unsigned integer between 0 and RAND_MAX (a sym

bolic constant defined in header file <cstdlib>)

– Function prototype for the rand function is in <cstdlib>

Page 3: Review of C++ Programming Part I Sheng-Fang Huang.

Random Number Generation

• To produce integers in a specific range, use the modulus operator (%) with rand– Example

• rand() % 6;– Produces numbers in the range 0 to 5

– This is called scaling, 6 is the scaling factor– Shifting can move the range to 1 to 6

• 1 + rand() % 6;

Page 4: Review of C++ Programming Part I Sheng-Fang Huang.

20 21 // if counter is divisible by 5, start a new line of output 22 if ( counter % 5 == 0 ) 23 cout << endl; 24 } // end for 25 26 return 0; // indicates successful termination 27 } // end main 6 6 5 5 6 5 1 1 5 3 6 6 2 4 2 6 2 3 4 1

1 // Fig. 6.8: fig06_08.cpp 2 // Shifted and scaled random integers. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <iomanip> 8 using std::setw; 9 10 #include <cstdlib> // contains function prototype for rand 11 using std::rand; 12 13 int main() 14 { 15 // loop 20 times 16 for ( int counter = 1; counter <= 20; counter++ ) 17 { 18 // pick random number from 1 to 6 and output it 19 cout << setw( 10 ) << ( 1 + rand() % 6 );

#include and using for function rand

Calling function rand

Page 5: Review of C++ Programming Part I Sheng-Fang Huang.

Random Number Generation

• Function rand – Generates pseudorandom numbers – The same sequence of numbers repeats itself each

time the program executes• Randomizing

– Conditioning a program to produce a different sequence of random numbers for each execution

• C++ Standard Library function srand– Takes an unsigned integer argument– Seeds the rand function to produce a different

sequence of random numbers

Page 6: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 6.10: fig06_10.cpp 2 // Randomizing die-rolling program. 3 #include <iostream> 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 #include <iomanip> 9 using std::setw; 10 11 #include <cstdlib> // contains prototypes for functions srand and rand 12 using std::rand; 13 using std::srand; 14 15 int main() 16 { 17 unsigned seed; // stores the seed entered by the user 18 19 cout << "Enter seed: "; 20 cin >> seed; 21 srand( seed ); // seed random number generator 22

using statement for function srand

Data type unsigned is short for unsigned int

Passing seed to srand to randomize the program

Page 7: Review of C++ Programming Part I Sheng-Fang Huang.

23 // loop 10 times 24 for ( int counter = 1; counter <= 10; counter++ ) 25 { 26 // pick random number from 1 to 6 and output it 27 cout << setw( 10 ) << ( 1 + rand() % 6 ); 28 29 // if counter is divisible by 5, start a new line of output 30 if ( counter % 5 == 0 ) 31 cout << endl; 32 } // end for 33 34 return 0; // indicates successful termination 35 } // end main Enter seed: 67 6 1 4 6 2 1 6 1 6 4 Enter seed: 432 4 6 3 1 6 3 1 5 4 2 Enter seed: 67 6 1 4 6 2 1 6 1 6 4

Program outputs show that each unique seed value produces a different

sequence of random numbers

Page 8: Review of C++ Programming Part I Sheng-Fang Huang.

Random Number Generation

• To randomize without having to enter a seed each time– srand(time(0));

• This causes the computer to read its clock to obtain the seed value

– Function time (with the argument 0) • Returns the current time as the number of seconds

since January 1, 1970 at midnight Greenwich Mean Time (GMT)

• Function prototype for time is in <ctime>

Page 9: Review of C++ Programming Part I Sheng-Fang Huang.

Random Number Generation

• Common Programming Error– Calling function srand more than once in a

program restarts the pseudorandom number sequence and can affect the randomness of the numbers produced by rand.

– Using srand in place of rand to attempt to generate random numbers is a compilation error—function srand does not return a value.

Page 10: Review of C++ Programming Part I Sheng-Fang Huang.

References and Reference Parameters

• Two ways to pass arguments to functions– Pass-by-value

• A copy of the argument’s value is passed to the called function

• Changes to the copy do not affect the original variable’s value in the caller

– Prevents accidental side effects of functions

– Pass-by-reference• Gives called function the ability to access and

modify the caller’s argument data directly

Page 11: Review of C++ Programming Part I Sheng-Fang Huang.

References and Reference Parameters

• Reference Parameter– An alias for its corresponding argument in a function ca

ll– & placed after the parameter type in the function prototy

pe and function header– Example

• int &count in a function header– Pronounced as “count is a reference to an int”

– Parameter name in the body of the called function actually refers to the original variable in the calling function

Page 12: Review of C++ Programming Part I Sheng-Fang Huang.

References and Reference Parameters

• Pass-by-reference is good for performance reasons, because it can eliminate the pass-by-value overhead of copying large amounts of data.

• Pass-by-reference can weaken security, because the called function can corrupt the caller’s data.

Page 13: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 6.19: fig06_19.cpp 2 // Comparing pass-by-value and pass-by-reference with references. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int squareByValue( int ); // function prototype (value pass) 8 void squareByReference( int & ); // function prototype (reference pass) 9 10 int main() 11 { 12 int x = 2; // value to square using squareByValue 13 int z = 4; // value to square using squareByReference 14 15 // demonstrate squareByValue 16 cout << "x = " << x << " before squareByValue\n"; 17 cout << "Value returned by squareByValue: " 18 << squareByValue( x ) << endl; 19 cout << "x = " << x << " after squareByValue\n" << endl; 20 21 // demonstrate squareByReference 22 cout << "z = " << z << " before squareByReference" << endl; 23 squareByReference( z ); 24 cout << "z = " << z << " after squareByReference" << endl; 25 return 0; // indicates successful termination 26 } // end main 27

Function illustrating pass-by-value

Function illustrating pass-by-reference

Variable is simply mentioned by name in both function calls

Page 14: Review of C++ Programming Part I Sheng-Fang Huang.

28 // squareByValue multiplies number by itself, stores the 29 // result in number and returns the new value of number 30 int squareByValue( int number ) 31 { 32 return number *= number; // caller's argument not modified 33 } // end function squareByValue 34 35 // squareByReference multiplies numberRef by itself and stores the result 36 // in the variable to which numberRef refers in function main 37 void squareByReference( int &numberRef ) 38 { 39 numberRef *= numberRef; // caller's argument modified 40 } // end function squareByReference x = 2 before squareByValue Value returned by squareByValue: 4 x = 2 after squareByValue z = 4 before squareByReference z = 16 after squareByReference

Receives copy of argument in main

Receives reference to argument in main

Modifies variable in main

Page 15: Review of C++ Programming Part I Sheng-Fang Huang.

References and Reference Parameters

• For the combined reasons of clarity and performance, many C++ programmers prefer that modifiable arguments be passed to functions by using pointers (which we study in Chapter 8), small nonmodifiable arguments be passed by value and large nonmodifiable arguments be passed to functions by using references to constants.

Page 16: Review of C++ Programming Part I Sheng-Fang Huang.

References and Reference Parameters

• References– Can also be used as aliases for other variables withi

n a function• All operations supposedly performed on the alias (i.e., the r

eference) are actually performed on the original variable• An alias is simply another name for the original variable• Must be initialized in their declarations

– Cannot be reassigned afterward

– Exampleint count = 1;int &cRef = count;cRef++;

– Increments count through alias cRef

Page 17: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 6.20: fig06_20.cpp 2 // References must be initialized. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int x = 3; 10 int &y = x; // y refers to (is an alias for) x 11 12 cout << "x = " << x << endl << "y = " << y << endl; 13 y = 7; // actually modifies x 14 cout << "x = " << x << endl << "y = " << y << endl; 15 return 0; // indicates successful termination 16 } // end main x = 3 y = 3 x = 7 y = 7

Creating a reference as an alias to another variable in the function

Assign 7 to x through alias y

Page 18: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 6.21: fig06_21.cpp 2 // References must be initialized. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int x = 3; 10 int &y; // Error: y must be initialized 11 12 cout << "x = " << x << endl << "y = " << y << endl; 13 y = 7; 14 cout << "x = " << x << endl << "y = " << y << endl; 15 return 0; // indicates successful termination 16 } // end main Borland C++ command-line compiler error message: Error E2304 C:\cpphtp5_examples\ch06\Fig06_21\fig06_21.cpp 10: Reference variable 'y' must be initialized in function main() Microsoft Visual C++ compiler error message: C:\cpphtp5_examples\ch06\Fig06_21\fig06_21.cpp(10) : error C2530: 'y' : references must be initialized GNU C++ compiler error message: fig06_21.cpp:10: error: 'y' declared as a reference but not initialized

Uninitialized reference

Page 19: Review of C++ Programming Part I Sheng-Fang Huang.

References and Reference Parameters

• Returning a reference from a function– Functions can return references to variables

• Should only be used when the variable is static

– Dangling reference• Returning a reference to an automatic variable

– That variable no longer exists after the function ends

Page 20: Review of C++ Programming Part I Sheng-Fang Huang.

Common Programming Error

• Not initializing a reference variable when it is declared is a compilation error, unless the declaration is part of a function’s parameter list. Reference parameters are initialized when the function in which they are declared is called.

Page 21: Review of C++ Programming Part I Sheng-Fang Huang.

Common Programming Error

• Attempting to reassign a previously declared reference to be an alias to another variable is a logic error. The value of the other variable is simply assigned to the variable for which the reference is already an alias.

Page 22: Review of C++ Programming Part I Sheng-Fang Huang.

Common Programming Error

• Returning a reference to an automatic variable in a called function is a logic error. Some compilers issue a warning when this occurs.

Page 23: Review of C++ Programming Part I Sheng-Fang Huang.

Recursion• Recursive function

– A function that calls itself.• Recursion

– Base case(s)• The simplest case(s), which the function knows

how to handle– For all other cases, the function typically divides the

problem into two conceptual pieces• A piece that the function knows how to do • A piece that it does not know how to do

– Slightly simpler or smaller version of the original problem

Page 24: Review of C++ Programming Part I Sheng-Fang Huang.

Recursion

• Recursion (Cont.)– Recursive call (also called the recursion step)

• The function launches (calls) a fresh copy of itself to work on the smaller problem

• Can result in many more recursive calls, as the function keeps dividing each new problem into two conceptual pieces

• This sequence of smaller and smaller problems must eventually converge on the base case

– Otherwise the recursion will continue forever

Page 25: Review of C++ Programming Part I Sheng-Fang Huang.

Recursion

• Factorial– The factorial of a nonnegative integer n, written n! (and pronounced “n factorial”), is the product

• n · (n – 1) · (n – 2) · … · 1– Recursive definition of the factorial function

• n! = n · (n – 1)! • Example

5! = 5 · 4 · 3 · 2 · 15! = 5 · ( 4 · 3 · 2 · 1)5! = 5 · ( 4! )

Page 26: Review of C++ Programming Part I Sheng-Fang Huang.

Recursive evaluation of 5!.

Page 27: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 6.29: fig06_29.cpp 2 // Testing the recursive factorial function. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <iomanip> 8 using std::setw; 9 10 unsigned long factorial( unsigned long ); // function prototype 11 12 int main() 13 { 14 // calculate the factorials of 0 through 10 15 for ( int counter = 0; counter <= 10; counter++ ) 16 cout << setw( 2 ) << counter << "! = " << factorial( counter ) 17 << endl; 18 19 return 0; // indicates successful termination 20 } // end main

First call to factorial function

Page 28: Review of C++ Programming Part I Sheng-Fang Huang.

21 22 // recursive definition of function factorial 23 unsigned long factorial( unsigned long number ) 24 { 25 if ( number <= 1 ) // test for base case 26 return 1; // base cases: 0! = 1 and 1! = 1 27 else // recursion step 28 return number * factorial( number - 1 ); 29 } // end function factorial 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800

Base cases simply return 1

Recursive call to factorial function with a slightly smaller problem

Page 29: Review of C++ Programming Part I Sheng-Fang Huang.

Arrays• Array

– Consecutive group of memory locations• All of which have the same type

– Index• Position number used to refer to a specific location/element• Also called subscript• Place in square brackets

– Must be positive integer or integer expression• First element has index zero• Example (assume a = 5 and b = 6)

– c[ a + b ] += 2;» Adds 2 to array element c[ 11 ]

Page 30: Review of C++ Programming Part I Sheng-Fang Huang.

Fig.7.1 | Array of 12 elements

• Examine array c– c is the array name– c has 12 elements ( c[0], c[1], … c[11] )

• The value of c[0] is –45• Brackets used to enclose an array subscript are actually

an operator in C++.

Page 31: Review of C++ Programming Part I Sheng-Fang Huang.

7.4 Examples Using Arrays

• Using a loop to initialize the array’s elements– Declare array, specify number of elements– Use repetition statement to loop for each

element• Use body of repetition statement to initialize each

individual array element

Page 32: Review of C++ Programming Part I Sheng-Fang Huang.

Examples Using Arrays• Initializing an array in a declaration with a

n initializer list– If fewer initializers than elements in the array

• Remaining elements are initialized to zero• Example

– int n[ 10 ] = { 0 };– If more initializers than elements in the array

• Compilation error

Page 33: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 7.4: fig07_04.cpp 2 // Initializing an array in a declaration. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <iomanip> 8 using std::setw; 9 10 int main() 11 { 12 // use initializer list to initialize array n 13 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 14 15 cout << "Element" << setw( 13 ) << "Value" << endl;

Declare n as an array of ints Compiler uses

initializer list to initialize array

16 17 // output each array element's value 18 for ( int i = 0; i < 10; i++ ) 19 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 20 21 return 0; // indicates successful termination 22 } // end main Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37

Page 34: Review of C++ Programming Part I Sheng-Fang Huang.

Examples Using Arrays

• Specifying an array’s size with a constant variable and setting array elements with calculations– Initialize elements of 10-element array to even

integers– Use repetition statement that calculates value

for current element, initializes array element using calculated value

Page 35: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 7.5: fig07_05.cpp 2 // Set array s to the even integers from 2 to 20. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include <iomanip> 8 using std::setw; 9 10 int main() 11 { 12 // constant variable can be used to specify array size 13 const int arraySize = 10; 14 15 int s[ arraySize ]; // array s has 10 elements 16 17 for ( int i = 0; i < arraySize; i++ ) // set the values 18 s[ i ] = 2 + 2 * i;

Declare constant variable arraySize using the const keyword

Declare array that contains 10 ints

Page 36: Review of C++ Programming Part I Sheng-Fang Huang.

Examples Using Arrays• Using character arrays to store and

manipulate strings– Arrays may be of any type, including chars

• We can store character strings in char arrays– Can be initialized using a string literal

• Example– char string1[] = "Hi";

• Equivalent to– char string1[] = { 'H', 'i', '\0' };– Array contains each character plus a special string-

termination character called the null character ('\0')

Page 37: Review of C++ Programming Part I Sheng-Fang Huang.

Examples Using Arrays• Using character arrays to store and manipulate s

trings (Cont.)– Can also be initialized with individual character const

ants in an initializer listchar string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

– Can also input a string directly into a character array from the keyboard using cin and >>cin >> string1;•cin >> may read more characters than the array can store

– A character array representing a null-terminated string can be output with cout and <<

Page 38: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 7.12: fig07_12.cpp 2 // Treating character arrays as strings. 3 #include <iostream> 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 int main() 9 { 10 char string1[ 20 ]; // reserves 20 characters 11 char string2[] = "string literal"; // reserves 15 characters 12 13 // read string from user into array string1 14 cout << "Enter the string \"hello there\": "; 15 cin >> string1; // reads "hello" [space terminates input] 16 17 // output strings 18 cout << "string1 is: " << string1 << "\nstring2 is: " << string2; 19 20 cout << "\nstring1 with spaces between characters is:\n"; 21

Store "string literal" as an array

of characters

Initializing an array of characters

using cin

22 // output characters until null character is reached 23 for ( int i = 0; string1[ i ] != '\0'; i++ ) 24 cout << string1[ i ] << ' '; 25 26 cin >> string1; // reads "there" 27 cout << "\nstring1 is: " << string1 << endl; 28 29 return 0; // indicates successful termination 30 } // end main Enter the string "hello there": hello there string1 is: hello string2 is: string literal string1 with spaces between characters is: h e l l o string1 is: there

Accessing specific characters in the

array

Loop until the terminating null character is reached

Page 39: Review of C++ Programming Part I Sheng-Fang Huang.

Examples Using Arrays• static local arrays and automatic local

arrays– A static local variable in a function

• Exists for the duration of the program• But is visible only in the function body

– A static local array• Exists for the duration of the program• Is initialized when its declaration is first encountered

– All elements are initialized to zero if not explicitly initialized» This does not happen for automatic local arrays

Page 40: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 7.13: fig07_13.cpp 2 // Static arrays are initialized to zero. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 void staticArrayInit( void ); // function prototype 8 void automaticArrayInit( void ); // function prototype 9 10 int main() 11 { 12 cout << "First call to each function:\n"; 13 staticArrayInit(); 14 automaticArrayInit(); 15 16 cout << "\n\nSecond call to each function:\n"; 17 staticArrayInit(); 18 automaticArrayInit(); 19 cout << endl; 20 21 return 0; // indicates successful termination 22 } // end main

Page 41: Review of C++ Programming Part I Sheng-Fang Huang.

23 24 // function to demonstrate a static local array 25 void staticArrayInit( void ) 26 { 27 // initializes elements to 0 first time function is called 28 static int array1[ 3 ]; // static local array 29 30 cout << "\nValues on entering staticArrayInit:\n"; 31 32 // output contents of array1 33 for ( int i = 0; i < 3; i++ ) 34 cout << "array1[" << i << "] = " << array1[ i ] << " "; 35 36 cout << "\nValues on exiting staticArrayInit:\n"; 37 38 // modify and output contents of array1 39 for ( int j = 0; j < 3; j++ ) 40 cout << "array1[" << j << "] = " << ( array1[ j ] += 5 ) << " "; 41 } // end function staticArrayInit 42 43 // function to demonstrate an automatic local array 44 void automaticArrayInit( void ) 45 { 46 // initializes elements each time function is called 47 int array2[ 3 ] = { 1, 2, 3 }; // automatic local array 48 49 cout << "\n\nValues on entering automaticArrayInit:\n";

Create a static array using keyword static

Create an automatic local array

Page 42: Review of C++ Programming Part I Sheng-Fang Huang.

50 51 // output contents of array2 52 for ( int i = 0; i < 3; i++ ) 53 cout << "array2[" << i << "] = " << array2[ i ] << " "; 54 55 cout << "\nValues on exiting automaticArrayInit:\n"; 56 57 // modify and output contents of array2 58 for ( int j = 0; j < 3; j++ ) 59 cout << "array2[" << j << "] = " << ( array2[ j ] += 5 ) << " "; 60 } // end function automaticArrayInit First call to each function: Values on entering staticArrayInit: array1[0] = 0 array1[1] = 0 array1[2] = 0 Values on exiting staticArrayInit: array1[0] = 5 array1[1] = 5 array1[2] = 5 Values on entering automaticArrayInit: array2[0] = 1 array2[1] = 2 array2[2] = 3 Values on exiting automaticArrayInit: array2[0] = 6 array2[1] = 7 array2[2] = 8 Second call to each function: Values on entering staticArrayInit: array1[0] = 5 array1[1] = 5 array1[2] = 5 Values on exiting staticArrayInit: array1[0] = 10 array1[1] = 10 array1[2] = 10 Values on entering automaticArrayInit: array2[0] = 1 array2[1] = 2 array2[2] = 3 Values on exiting automaticArrayInit: array2[0] = 6 array2[1] = 7 array2[2] = 8

Values reflect changes from the previous function call – the array was not reinitialized

Page 43: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Variable

• Pointer variables– Contain memory addresses as values

• Normally, variable contains specific value (direct reference)

• Pointers contain address of variable that has specific value (indirect reference)

• Indirection– Referencing value through pointer

Page 44: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Variable Declarations and Initialization

• Pointer declarations– * indicates variable is a pointer

• Example– int *myPtr;

» Declares pointer to int, of type int *• Multiple pointers require multiple asterisks

int *myPtr1, *myPtr2;• Pointer initialization

– Initialized to 0, NULL, or an address•0 or NULL points to nothing (null pointer)

Page 45: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Variable Declarations and Initialization

number1

45

number2

30

number1

number2

int number1;

int *number1;

Page 46: Review of C++ Programming Part I Sheng-Fang Huang.

Directly and indirectly referencing a variable.

Page 47: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Operators

• Address operator (&)– Returns memory address of its operand– Example

• int y = 5;int *yPtr;yPtr = &y;assigns the address of variable y to pointer variable yPtr

– Variable yPtr “points to” y» yPtr indirectly references variable y’s value

Page 48: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Operators• * operator

– Also called indirection operator or dereferencing operator

– *yPtr returns y (because yPtr points to y)– Dereferenced pointer can be an lvalue

*yptr = 9;• * and & are inverses of each other

Page 49: Review of C++ Programming Part I Sheng-Fang Huang.

Common Programming Error

• Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory could cause a fatal execution-time error, or it could accidentally modify important data and allow the program to run to completion, possibly with incorrect results.

Page 50: Review of C++ Programming Part I Sheng-Fang Huang.

Common Programming Error

• An attempt to dereference a variable that is not a pointer is a compilation error.

• Dereferencing a null pointer is normally a fatal execution-time error.

Page 51: Review of C++ Programming Part I Sheng-Fang Huang.

14 15 cout << "The address of a is " << &a 16 << "\nThe value of aPtr is " << aPtr; 17 cout << "\n\nThe value of a is " << a 18 << "\nThe value of *aPtr is " << *aPtr; 19 cout << "\n\nShowing that * and & are inverses of " 20 << "each other.\n&*aPtr = " << &*aPtr 21 << "\n*&aPtr = " << *&aPtr << endl; 22 return 0; // indicates successful termination 23 } // end main The address of a is 0012F580 The value of aPtr is 0012F580 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012F580 *&aPtr = 0012F580

* and & are inverses of each other

Address of a and the value of aPtr are identical

* and & are inverses; same result when both are applied to aPtr

Value of a and the dereferenced aPtr are identical

Outline 1 // Fig. 8.4: fig08_04.cpp 2 // Using the & and * operators. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int a; // a is an integer 10 int *aPtr; // aPtr is an int * -- pointer to an integer 11 12 a = 7; // assigned 7 to a 13 aPtr = &a; // assign the address of a to aPtr

Variable aPtr is a point to an int

Initialize aPtr with the address of variable a

Page 52: Review of C++ Programming Part I Sheng-Fang Huang.

Passing Arguments to Functions by Reference with Pointers

• Three ways to pass arguments to a function– Pass-by-value– Pass-by-reference with reference arguments– Pass-by-reference with pointer arguments

• A function can return only one value• Arguments passed to a function using reference

arguments– Function can modify original values of arguments

• More than one value “returned”

Page 53: Review of C++ Programming Part I Sheng-Fang Huang.

Passing Arguments to Functions by Reference with Pointers

• Pass-by-reference with pointer arguments– Simulates pass-by-reference

• Use pointers and indirection operator– Pass address of argument using & operator– Arrays not passed with & because array name

is already a pointer– * operator used as alias/nickname for variable

inside of function

Page 54: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.6: fig08_06.cpp 2 // Cube a variable using pass-by-value. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int cubeByValue( int ); // prototype 8 9 int main() 10 { 11 int number = 5; 12 13 cout << "The original value of number is " << number; 14 15 number = cubeByValue( number ); // pass number by value to cubeByValue 16 cout << "\nThe new value of number is " << number << endl; 17 return 0; // indicates successful termination 18 } // end main 19 20 // calculate and return cube of integer argument 21 int cubeByValue( int n ) 22 { 23 return n * n * n; // cube local variable n and return result 24 } // end function cubeByValue The original value of number is 5 The new value of number is 125

Pass number by value; result returned by cubeByValue

cubeByValue receives parameter passed-by-value

Cubes local variable nand return the result

Page 55: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.7: fig08_07.cpp 2 // Cube a variable using pass-by-reference with a pointer argument. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 void cubeByReference( int * ); // prototype 8 9 int main() 10 { 11 int number = 5; 12 13 cout << "The original value of number is " << number; 14 15 cubeByReference( &number ); // pass number address to cubeByReference 16 17 cout << "\nThe new value of number is " << number << endl; 18 return 0; // indicates successful termination 19 } // end main 20 21 // calculate cube of *nPtr; modifies variable number in main 22 void cubeByReference( int *nPtr ) 23 { 24 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr 25 } // end function cubeByReference The original value of number is 5 The new value of number is 125

Prototype indicates parameter is a pointer to an int

Apply address operator & to pass address of number to cubeByReference

cubeByReference modifies variable number

cubeByReference receives address of an int variable,i.e., a pointer to an int

Modify and access int variable using indirection operator *

Page 56: Review of C++ Programming Part I Sheng-Fang Huang.
Page 57: Review of C++ Programming Part I Sheng-Fang Huang.
Page 58: Review of C++ Programming Part I Sheng-Fang Huang.

Using const with Pointers

• Four ways to pass pointer to function– Nonconstant pointer to nonconstant data

• Highest amount of access• Data can be modified through the dereferenced poi

nter• Pointer can be modified to point to other data

– Pointer arithmetic» Operator ++ moves array pointer to the next element

• Its declaration does not include const qualifier

Page 59: Review of C++ Programming Part I Sheng-Fang Huang.

Using const with Pointers

– Nonconstant pointer to constant data• Pointer can be modified to point to any appropriate

data item• Data cannot be modified through this pointer• Provides the performance of pass-by-reference an

d the protection of pass-by-value

Page 60: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.11: fig08_11.cpp 2 // Printing a string one character at a time using 3 // a non-constant pointer to constant data. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 void printCharacters( const char * ); // print using pointer to const data 9 10 int main() 11 { 12 const char phrase[] = "print characters of a string"; 13 14 cout << "The string is:\n"; 15 printCharacters( phrase ); // print characters in phrase 16 cout << endl; 17 return 0; // indicates successful termination 18 } // end main 19 20 // sPtr can be modified, but it cannot modify the character to which 21 // it points, i.e., sPtr is a "read-only" pointer 22 void printCharacters( const char *sPtr ) 23 { 24 for ( ; *sPtr != '\0'; sPtr++ ) // no initialization 25 cout << *sPtr; // display character without modification 26 } // end function printCharacters The string is: print characters of a string

Parameter is a nonconstant pointer to constant data

Pass pointer phrase to function printCharacters

sPtr is a nonconstant pointer to constant data; it cannot modify the character to which it points

Increment sPtr to point to the next character

Page 61: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.12: fig08_12.cpp 2 // Attempting to modify data through a 3 // non-constant pointer to constant data. 4 5 void f( const int * ); // prototype 6 7 int main() 8 { 9 int y; 10 11 f( &y ); // f attempts illegal modification 12 return 0; // indicates successful termination 13 } // end main

Parameter is a nonconstant pointer to constant data

Pass the address of int variable y to attempt an illegal modification

Page 62: Review of C++ Programming Part I Sheng-Fang Huang.

Outline14 15 // xPtr cannot modify the value of constant variable to which it points 16 void f( const int *xPtr ) 17 { 18 *xPtr = 100; // error: cannot modify a const object 19 } // end function f Borland C++ command-line compiler error message:

Error E2024 fig08_12.cpp 18: Cannot modify a const object in function f(const int *) Microsoft Visual C++ compiler error message:

c:\cpphtp5_examples\ch08\Fig08_12\fig08_12.cpp(18) : error C2166: l-value specifies const object GNU C++ compiler error message:

fig08_12.cpp: In function `void f(const int*)': fig08_12.cpp:18: error: assignment of read-only location

Attempt to modify a const object pointed to by xPtr

Error produced when attempting to compile

• Pass large objects using pointers to constant data, or references to constant data, to obtain the security of pass-by-value.

Page 63: Review of C++ Programming Part I Sheng-Fang Huang.

Using const with Pointers

– Constant pointer to nonconstant data• Always points to the same memory location

– Can only access other elements using subscript notation

• Data can be modified through the pointer• Default for an array name

– Can be used by a function to receive an array argument

• Must be initialized when declared

Page 64: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.13: fig08_13.cpp 2 // Attempting to modify a constant pointer to non-constant data. 3 4 int main() 5 { 6 int x, y; 7 8 // ptr is a constant pointer to an integer that can 9 // be modified through ptr, but ptr always points to the 10 // same memory location. 11 int * const ptr = &x; // const pointer must be initialized 12 13 *ptr = 7; // allowed: *ptr is not const 14 ptr = &y; // error: ptr is const; cannot assign to it a new address 15 return 0; // indicates successful termination 16 } // end main Borland C++ command-line compiler error message: Error E2024 fig08_13.cpp 14: Cannot modify a const object in function main()s Microsoft Visual C++ compiler error message: c:\cpphtp5e_examples\ch08\Fig08_13\fig08_13.cpp(14) : error C2166: l-value specifies const object GNU C++ compiler error message: fig08_13.cpp: In function `int main()': fig08_13.cpp:14: error: assignment of read-only variable `ptr'

ptr is a constant pointer to an integer

Can modify x (pointed to by ptr) since x is not constant

Cannot modify ptr to point to a new address since ptr is constant

Line 14 generates a compiler error by attempting to assign a new address to a constant pointer

Page 65: Review of C++ Programming Part I Sheng-Fang Huang.

Using const with Pointers

– Constant pointer to constant data• Least amount of access• Always points to the same memory location• Data cannot be modified using this pointer

Page 66: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.14: fig08_14.cpp 2 // Attempting to modify a constant pointer to constant data. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int x = 5, y; 10 11 // ptr is a constant pointer to a constant integer. 12 // ptr always points to the same location; the integer 13 // at that location cannot be modified. 14 const int *const ptr = &x; 15 16 cout << *ptr << endl; 17 18 *ptr = 7; // error: *ptr is const; cannot assign new value 19 ptr = &y; // error: ptr is const; cannot assign new address 20 return 0; // indicates successful termination 21 } // end main

ptr is a constant pointer to a constant integer

Cannot modify x (pointed to by ptr) since *ptr is constant

Cannot modify ptr to point to a new address since ptr is constant

Page 67: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Expressions and Pointer Arithmetic

• Pointer arithmetic– Increment/decrement pointer (++ or --)– Add/subtract an integer to/from a pointer (+ or +=,- or -=)

– Pointer arithmetic is meaningless unless performed on a pointer to an array

Page 68: Review of C++ Programming Part I Sheng-Fang Huang.

Pointer Expressions and Pointer Arithmetic

• 5 element int array on a machine using 4 byte ints– vPtr points to first element v[ 0 ], at location 3000

vPtr = &v[ 0 ];– vPtr += 2; sets vPtr to 3008 (3000 + 2 * 4)

vPtr points to v[ 2 ]

Page 69: Review of C++ Programming Part I Sheng-Fang Huang.

Array v and a pointer variable vPtr that points to v.

after pointer arithmetic

Page 70: Review of C++ Programming Part I Sheng-Fang Huang.

Relationship Between Pointers and Arrays

• Arrays and pointers are closely related– Array name is like constant pointer– Pointers can do array subscripting operations

Page 71: Review of C++ Programming Part I Sheng-Fang Huang.

Relationship Between Pointers and Arrays• Accessing array elements with pointers

– Assume declarations: int b[ 5 ]; int *bPtr; bPtr = b;– Element b[ n ] can be accessed by *( bPtr + n )

• Called pointer/offset notation

– Addresses• &b[ 3 ] is same as bPtr + 3

– Array name can be treated as pointer• b[ 3 ] is same as *( b + 3 )

– Pointers can be subscripted (pointer/subscript notation)• bPtr[ 3 ] is same as b[ 3 ]

Page 72: Review of C++ Programming Part I Sheng-Fang Huang.

Common Programming Error 8.14

• Although array names are pointers to the beginning of the array and pointers can be modified in arithmetic expressions, array names cannot be modified in arithmetic expressions, because array names are constant pointers.

• For clarity, use array notation instead of pointer notation when manipulating arrays.

Page 73: Review of C++ Programming Part I Sheng-Fang Huang.

Outline 1 // Fig. 8.20: fig08_20.cpp 2 // Using subscripting and pointer notations with arrays. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int b[] = { 10, 20, 30, 40 }; // create 4-element array b 10 int *bPtr = b; // set bPtr to point to array b 11 12 // output array b using array subscript notation 13 cout << "Array b printed with:\n\nArray subscript notation\n"; 14 15 for ( int i = 0; i < 4; i++ ) 16 cout << "b[" << i << "] = " << b[ i ] << '\n'; 17 18 // output array b using the array name and pointer/offset notation 19 cout << "\nPointer/offset notation where " 20 << "the pointer is the array name\n"; 21 22 for ( int offset1 = 0; offset1 < 4; offset1++ ) 23 cout << "*(b + " << offset1 << ") = " << *( b + offset1 ) << '\n';

Using array subscript notation

Using array name and pointer/offset notation

Page 74: Review of C++ Programming Part I Sheng-Fang Huang.

Outline24 25 // output array b using bPtr and array subscript notation 26 cout << "\nPointer subscript notation\n"; 27 28 for ( int j = 0; j < 4; j++ ) 29 cout << "bPtr[" << j << "] = " << bPtr[ j ] << '\n'; 30 31 cout << "\nPointer/offset notation\n"; 32 33 // output array b using bPtr and pointer/offset notation 34 for ( int offset2 = 0; offset2 < 4; offset2++ ) 35 cout << "*(bPtr + " << offset2 << ") = " 36 << *( bPtr + offset2 ) << '\n'; 37 38 return 0; // indicates successful termination 39 } // end main

Using pointer subscript notation

Using pointer name and pointer/offset notation

Page 75: Review of C++ Programming Part I Sheng-Fang Huang.

Outline Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 Pointer/offset notation where the pointer is the array name *(b + 0) = 10 *(b + 1) = 20 *(b + 2) = 30 *(b + 3) = 40 Pointer subscript notation bPtr[0] = 10 bPtr[1] = 20 bPtr[2] = 30 bPtr[3] = 40 Pointer/offset notation *(bPtr + 0) = 10 *(bPtr + 1) = 20 *(bPtr + 2) = 30 *(bPtr + 3) = 40

Page 76: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 8.21: fig08_21.cpp 2 // Copying a string using array notation and pointer notation. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 void copy1( char *, const char * ); // prototype 8 void copy2( char *, const char * ); // prototype 9 10 int main() 11 { 12 char string1[ 10 ]; 13 char *string2 = "Hello"; 14 char string3[ 10 ]; 15 char string4[] = "Good Bye"; 16 17 copy1( string1, string2 ); // copy string2 into string1 18 cout << "string1 = " << string1 << endl; 19 20 copy2( string3, string4 ); // copy string4 into string3 21 cout << "string3 = " << string3 << endl; 22 return 0; // indicates successful termination 23 } // end main

Page 77: Review of C++ Programming Part I Sheng-Fang Huang.

24 25 // copy s2 to s1 using array notation 26 void copy1( char * s1, const char * s2 ) 27 { 28 // copying occurs in the for header 29 for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) 30 ; // do nothing in body 31 } // end function copy1 32 33 // copy s2 to s1 using pointer notation 34 void copy2( char *s1, const char *s2 ) 35 { 36 // copying occurs in the for header 37 for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) 38 ; // do nothing in body 39 } // end function copy2 string1 = Hello string3 = Good Bye

Use array subscript notation to copy string in s2 to character array s1

Use pointer notation to copy string in s2 to character array in s1

Increment both pointers to point to next elements in corresponding arrays

Page 78: Review of C++ Programming Part I Sheng-Fang Huang.

Arrays of Pointers• Arrays can contain pointers

– Commonly used to store array of strings (string array)

• Array does not store strings, only pointers to strings

• Example– const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

» Each element of suit points to a char * (string)

•suit array has fixed size (4), but strings can be of any size

• Commonly used with command-line arguments to function main

Page 79: Review of C++ Programming Part I Sheng-Fang Huang.

Graphical representation of the suit array.

Page 80: Review of C++ Programming Part I Sheng-Fang Huang.

Introduction of Object-Oriented Programming

• Integrated Time class case study• Preprocessor wrapper• Three types of “handles” on an object

– Name of an object– Reference to an object– Pointer to an object

• Class functions

Page 81: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.1: Time.h 2 // Declaration of class Time. 3 // Member functions are defined in Time.cpp 4 5 // prevent multiple inclusions of header file 6 #ifndef TIME_H 7 #define TIME_H 8 9 // Time class definition 10 class Time 11 { 12 public: 13 Time(); // constructor 14 void setTime( int, int, int ); // set hour, minute and second 15 void printUniversal(); // print time in universal-time format 16 void printStandard(); // print time in standard-time format 17 private: 18 int hour; // 0 - 23 (24-hour clock format) 19 int minute; // 0 - 59 20 int second; // 0 - 59 21 }; // end class Time 22 23 #endif

Preprocessor directive #ifndef determines whether a name is defined

Preprocessor directive #define defines a name (e.g., TIME_H)

Preprocessor directive #endif marks the end of the code that should not be included multiple times

Page 82: Review of C++ Programming Part I Sheng-Fang Huang.

Software Engineering Observation

• Use #ifndef, #define and #endif to form a preprocessor wrapper that prevents header files from being included more than once in a program.

• Each element of a class should have private visibility unless it can be proven that the element needs public visibility. This is another example of the principle of least privilege.

Page 83: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.2: Time.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> 4 using std::cout; 5 6 #include <iomanip> 7 using std::setfill; 8 using std::setw; 9 10 #include "Time.h" // include definition of class Time from Time.h 11 12 // Time constructor initializes each data member to zero. 13 // Ensures all Time objects start in a consistent state. 14 Time::Time() 15 { 16 hour = minute = second = 0; 17 } // end Time constructor 18 19 // set new Time value using universal time; ensure that 20 // the data remains consistent by setting invalid values to zero 21 void Time::setTime( int h, int m, int s ) 22 { 23 hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour 24 minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute 25 second = ( s >= 0 && s < 60 ) ? s : 0; // validate second 26 } // end function setTime

Ensure that hour, minute and second values remain valid

Page 84: Review of C++ Programming Part I Sheng-Fang Huang.

27 28 // print Time in universal-time format (HH:MM:SS) 29 void Time::printUniversal() 30 { 31 cout << setfill( '0' ) << setw( 2 ) << hour << ":" 32 << setw( 2 ) << minute << ":" << setw( 2 ) << second; 33 } // end function printUniversal 34 35 // print Time in standard-time format (HH:MM:SS AM or PM) 36 void Time::printStandard() 37 { 38 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" 39 << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) 40 << second << ( hour < 12 ? " AM" : " PM" ); 41 } // end function printStandard

Using setfill stream manipulator to specify a fill character

Page 85: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.3: fig09_03.cpp 2 // Program to test class Time. 3 // NOTE: This file must be compiled with Time.cpp. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 #include "Time.h" // include definition of class Time from Time.h 9 10 int main() 11 { 12 Time t; // instantiate object t of class Time 13 14 // output Time object t's initial values 15 cout << "The initial universal time is "; 16 t.printUniversal(); // 00:00:00 17 cout << "\nThe initial standard time is "; 18 t.printStandard(); // 12:00:00 AM 19 20 t.setTime( 13, 27, 6 ); // change time 21 22 // output Time object t's new values 23 cout << "\n\nUniversal time after setTime is "; 24 t.printUniversal(); // 13:27:06 25 cout << "\nStandard time after setTime is "; 26 t.printStandard(); // 1:27:06 PM 27 28 t.setTime( 99, 99, 99 ); // attempt invalid settings

Page 86: Review of C++ Programming Part I Sheng-Fang Huang.

29 30 // output t's values after specifying invalid values 31 cout << "\n\nAfter attempting invalid settings:" 32 << "\nUniversal time: "; 33 t.printUniversal(); // 00:00:00 34 cout << "\nStandard time: "; 35 t.printStandard(); // 12:00:00 AM 36 cout << endl; 37 return 0; 38 } // end main The initial universal time is 00:00:00 The initial standard time is 12:00:00 AM Universal time after setTime is 13:27:06 Standard time after setTime is 1:27:06 PM After attempting invalid settings: Universal time: 00:00:00 Standard time: 12:00:00 AM

Page 87: Review of C++ Programming Part I Sheng-Fang Huang.

std::setfill

• Parameterized stream manipulator setfill – Specifies the fill character

• Which is displayed when an output field wider than the number of digits in the output value

• By default, fill characters appear to the left of the digits in the number

– setfill is a “sticky” setting• Applies for all subsequent values that are displaye

d in fields wider than the value being displayed

Page 88: Review of C++ Programming Part I Sheng-Fang Huang.

Declaration of Object• Using class Time

– Once class Time has been defined, it can be used in declarations•Time sunset;•Time arrayOfTimes[ 5 ];•Time &dinnerTime = sunset;•Time *timePtr = &dinnerTime;

Page 89: Review of C++ Programming Part I Sheng-Fang Huang.

Performance

• Objects contain only data, so objects are much smaller than if they also contained member functions.

• The compiler creates one copy (only) of the member functions separate from all objects of the class.

• All objects of the class share this one copy. Each object, of course, needs its own copy of the class’s data, because the data can vary among the objects.

Page 90: Review of C++ Programming Part I Sheng-Fang Huang.

Accessing Class Members

• Dot member selection operator (.) – Accesses the object’s members.– Used with an object’s name or with a reference

to an object.• Arrow member selection operator (->)

– Accesses members with a pointer to an object..– Used with a pointer to an object.

Page 91: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.4: fig09_04.cpp 2 // Demonstrating the class member access operators . and -> 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 // class Count definition 8 class Count 9 { 10 public: // public data is dangerous 11 // sets the value of private data member x 12 void setX( int value ) 13 { 14 x = value; 15 } // end function setX 16 17 // prints the value of private data member x 18 void print() 19 { 20 cout << x << endl; 21 } // end function print 22 23 private: 24 int x; 25 }; // end class Count

Page 92: Review of C++ Programming Part I Sheng-Fang Huang.

26 27 int main() 28 { 29 Count counter; // create counter object 30 Count *counterPtr = &counter; // create pointer to counter 31 Count &counterRef = counter; // create reference to counter 32 33 cout << "Set x to 1 and print using the object's name: "; 34 counter.setX( 1 ); // set data member x to 1 35 counter.print(); // call member function print 36 37 cout << "Set x to 2 and print using a reference to an object: "; 38 counterRef.setX( 2 ); // set data member x to 2 39 counterRef.print(); // call member function print 40 41 cout << "Set x to 3 and print using a pointer to an object: "; 42 counterPtr->setX( 3 ); // set data member x to 3 43 counterPtr->print(); // call member function print 44 return 0; 45 } // end main Set x to 1 and print using the object's name: 1 Set x to 2 and print using a reference to an object: 2 Set x to 3 and print using a pointer to an object: 3

Using the dot member selection operator with an object

Using the dot member selection operator with a reference

Using the arrow member selection operator with a pointer

Page 93: Review of C++ Programming Part I Sheng-Fang Huang.

Access Functions and Utility Functions

• Access functions– Can read or display data– Can test the truth or falsity of conditions

• Such functions are often called predicate functions• For example, isEmpty function for a class capable of holdin

g many objects

• Utility functions (also called helper functions)– private member functions that support the operatio

n of the class’s public member functions– Not part of a class’s public interface

• Not intended to be used by clients of a class

Page 94: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.5: SalesPerson.h 2 // SalesPerson class definition. 3 // Member functions defined in SalesPerson.cpp. 4 #ifndef SALESP_H 5 #define SALESP_H 6 7 class SalesPerson 8 { 9 public: 10 SalesPerson(); // constructor 11 void getSalesFromUser(); // input sales from keyboard 12 void setSales( int, double ); // set sales for a specific month 13 void printAnnualSales(); // summarize and print sales 14 private: 15 double totalAnnualSales(); // prototype for utility function 16 double sales[ 12 ]; // 12 monthly sales figures 17 }; // end class SalesPerson 18 19 #endif

Prototype for a private utility function

Page 95: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.6: SalesPerson.cpp 2 // Member functions for class SalesPerson. 3 #include <iostream> 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> 10 using std::setprecision; 11 12 #include "SalesPerson.h" // include SalesPerson class definition 13 14 // initialize elements of array sales to 0.0 15 SalesPerson::SalesPerson() 16 { 17 for ( int i = 0; i < 12; i++ ) 18 sales[ i ] = 0.0; 19 } // end SalesPerson constructor

Page 96: Review of C++ Programming Part I Sheng-Fang Huang.

20 21 // get 12 sales figures from the user at the keyboard 22 void SalesPerson::getSalesFromUser() 23 { 24 double salesFigure; 25 26 for ( int i = 1; i <= 12; i++ ) 27 { 28 cout << "Enter sales amount for month " << i << ": "; 29 cin >> salesFigure; 30 setSales( i, salesFigure ); 31 } // end for 32 } // end function getSalesFromUser 33 34 // set one of the 12 monthly sales figures; function subtracts 35 // one from month value for proper subscript in sales array 36 void SalesPerson::setSales( int month, double amount ) 37 { 38 // test for valid month and amount values 39 if ( month >= 1 && month <= 12 && amount > 0 ) 40 sales[ month - 1 ] = amount; // adjust for subscripts 0-11 41 else // invalid month or amount value 42 cout << "Invalid month or sales figure" << endl; 43 } // end function setSales

Page 97: Review of C++ Programming Part I Sheng-Fang Huang.

44 45 // print total annual sales (with the help of utility function) 46 void SalesPerson::printAnnualSales() 47 { 48 cout << setprecision( 2 ) << fixed 49 << "\nThe total annual sales are: $" 50 << totalAnnualSales() << endl; // call utility function 51 } // end function printAnnualSales 52 53 // private utility function to total annual sales 54 double SalesPerson::totalAnnualSales() 55 { 56 double total = 0.0; // initialize total 57 58 for ( int i = 0; i < 12; i++ ) // summarize sales results 59 total += sales[ i ]; // add month i sales to total 60 61 return total; 62 } // end function totalAnnualSales

Calling a private utility function

Definition of a private utility function

Page 98: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.7: fig09_07.cpp 2 // Demonstrating a utility function. 3 // Compile this program with SalesPerson.cpp 4 5 // include SalesPerson class definition from SalesPerson.h 6 #include "SalesPerson.h" 7 8 int main() 9 { 10 SalesPerson s; // create SalesPerson object s 11 12 s.getSalesFromUser(); // note simple sequential code; 13 s.printAnnualSales(); // no control statements in main 14 return 0; 15 } // end main Enter sales amount for month 1: 5314.76 Enter sales amount for month 2: 4292.38 Enter sales amount for month 3: 4589.83 Enter sales amount for month 4: 5534.03 Enter sales amount for month 5: 4376.34 Enter sales amount for month 6: 5698.45 Enter sales amount for month 7: 4439.22 Enter sales amount for month 8: 5893.57 Enter sales amount for month 9: 4909.67 Enter sales amount for month 10: 5123.45 Enter sales amount for month 11: 4024.97 Enter sales amount for month 12: 5923.92 The total annual sales are: $60120.59

Page 99: Review of C++ Programming Part I Sheng-Fang Huang.

Constructors with Default Arguments

• Constructors can specify default arguments– Can initialize data members to a consistent

state• Even if no values are provided in a constructor

call– Constructor that defaults all its arguments is

also a default constructor• Can be invoked with no arguments• Maximum of one default constructor per class

Page 100: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.8: Time.h 2 // Declaration of class Time. 3 // Member functions defined in Time.cpp. 4 5 // prevent multiple inclusions of header file 6 #ifndef TIME_H 7 #define TIME_H 8 9 // Time abstract data type definition 10 class Time 11 { 12 public: 13 Time( int = 0, int = 0, int = 0 ); // default constructor 14 15 // set functions 16 void setTime( int, int, int ); // set hour, minute, second 17 void setHour( int ); // set hour (after validation) 18 void setMinute( int ); // set minute (after validation) 19 void setSecond( int ); // set second (after validation)

Prototype of a constructor with default arguments

20 21 // get functions 22 int getHour(); // return hour 23 int getMinute(); // return minute 24 int getSecond(); // return second 25 26 void printUniversal(); // output time in universal-time format 27 void printStandard(); // output time in standard-time format 28 private: 29 int hour; // 0 - 23 (24-hour clock format) 30 int minute; // 0 - 59 31 int second; // 0 - 59 32 }; // end class Time 33 34 #endif

Page 101: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.9: Time.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> 4 using std::cout; 5 6 #include <iomanip> 7 using std::setfill; 8 using std::setw; 9 10 #include "Time.h" // include definition of class Time from Time.h 11 12 // Time constructor initializes each data member to zero; 13 // ensures that Time objects start in a consistent state 14 Time::Time( int hr, int min, int sec ) 15 { 16 setTime( hr, min, sec ); // validate and set time 17 } // end Time constructor 18 19 // set new Time value using universal time; ensure that 20 // the data remains consistent by setting invalid values to zero 21 void Time::setTime( int h, int m, int s ) 22 { 23 setHour( h ); // set private field hour 24 setMinute( m ); // set private field minute 25 setSecond( s ); // set private field second 26 } // end function setTime

Parameters could receive the default values

Page 102: Review of C++ Programming Part I Sheng-Fang Huang.

1 // Fig. 9.10: fig09_10.cpp 2 // Demonstrating a default constructor for class Time. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 #include "Time.h" // include definition of class Time from Time.h 8 9 int main() 10 { 11 Time t1; // all arguments defaulted 12 Time t2( 2 ); // hour specified; minute and second defaulted 13 Time t3( 21, 34 ); // hour and minute specified; second defaulted 14 Time t4( 12, 25, 42 ); // hour, minute and second specified 15 Time t5( 27, 74, 99 ); // all bad values specified 16 17 cout << "Constructed with:\n\nt1: all arguments defaulted\n "; 18 t1.printUniversal(); // 00:00:00 19 cout << "\n "; 20 t1.printStandard(); // 12:00:00 AM 21 22 cout << "\n\nt2: hour specified; minute and second defaulted\n "; 23 t2.printUniversal(); // 02:00:00 24 cout << "\n "; 25 t2.printStandard(); // 2:00:00 AM

Initializing Time objects using 0, 1, 2 and 3 arguments

Page 103: Review of C++ Programming Part I Sheng-Fang Huang.

26 27 cout << "\n\nt3: hour and minute specified; second defaulted\n "; 28 t3.printUniversal(); // 21:34:00 29 cout << "\n "; 30 t3.printStandard(); // 9:34:00 PM 31 32 cout << "\n\nt4: hour, minute and second specified\n "; 33 t4.printUniversal(); // 12:25:42 34 cout << "\n "; 35 t4.printStandard(); // 12:25:42 PM 36 37 cout << "\n\nt5: all invalid values specified\n "; 38 t5.printUniversal(); // 00:00:00 39 cout << "\n "; 40 t5.printStandard(); // 12:00:00 AM 41 cout << endl; 42 return 0; 43 } // end main

Page 104: Review of C++ Programming Part I Sheng-Fang Huang.

Constructed with: t1: all arguments defaulted 00:00:00 12:00:00 AM t2: hour specified; minute and second defaulted 02:00:00 2:00:00 AM t3: hour and minute specified; second defaulted 21:34:00 9:34:00 PM t4: hour, minute and second specified 12:25:42 12:25:42 PM t5: all invalid values specified 00:00:00 12:00:00 AM

Invalid values passed to constructor, so object t5 contains all default data

Page 105: Review of C++ Programming Part I Sheng-Fang Huang.

Destructors• Destructor

– A special member function– Name is the tilde character (~) followed by

the class name, e.g., ~Time– Called implicitly when an object is destroyed

• For example, this occurs as an automatic object is destroyed when program execution leaves the scope in which that object was instantiated

– Does not actually release the object’s memory

Page 106: Review of C++ Programming Part I Sheng-Fang Huang.

Destructors

• Destructor (Cont.)– Receives no parameters and returns no value

• May not specify a return type—not even void– A class may have only one destructor

• Destructor overloading is not allowed– If the programmer does not explicitly provide

a destructor, the compiler creates an “empty” destructor