Transcript Document

Review of C++ Programming
Part I
Sheng-Fang Huang
Random Number Generation
• C++ Standard Library function rand
– Introduces the element of chance into computer
applications
– Example
• i = rand();
– Generates an unsigned integer between 0 and RAND_MAX (a
symbolic constant defined in header file <cstdlib>)
– Function prototype for the rand function is in
<cstdlib>
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;
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;
#include and using for function
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 );
20
21
// if counter is divisible by 5, start a new line of output
22
if ( counter % 5 == 0 )
cout << endl;
23
24
} // end for
25
26
return 0; // indicates successful termination
27 } // end main
6
5
6
6
6
1
6
2
5
1
2
3
5
5
4
4
6
3
2
1
Calling function
rand
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
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
using statement for function srand
15 int main()
16 {
17
unsigned seed; // stores the seed entered by the user
18
Data type unsigned is short for unsigned int
19
cout << "Enter seed: ";
20
cin >> seed;
21
srand( seed ); // seed random number generator
22
Passing seed to srand to randomize the program
23
// loop 10 times
24
25
26
for ( int counter = 1; counter <= 10; counter++ )
{
// pick random number from 1 to 6 and output it
27
28
cout << setw( 10 ) << ( 1 + rand() % 6 );
29
30
// if counter is divisible by 5, start a new line of output
if ( counter % 5 == 0 )
31
32
cout << endl;
} // end for
33
34
return 0; // indicates successful termination
35 } // end main
Enter seed: 67
6
1
1
6
4
1
6
6
2
4
Enter seed: 432
4
3
6
1
3
5
1
4
6
2
Enter seed: 67
6
1
1
6
4
1
6
6
2
4
Program outputs show that each
unique seed value produces a different
sequence of random numbers
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>
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.
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
References and Reference
Parameters
• Reference Parameter
– An alias for its corresponding argument in a function
call
– & placed after the parameter type in the function
prototype 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
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.
1
// Fig. 6.19: fig06_19.cpp
2
// Comparing pass-by-value and pass-by-reference with references.
3
4
#include <iostream>
using std::cout;
5
6
7
8
9
using std::endl;
Function illustrating pass-by-value
int squareByValue( int ); // function prototype (value pass)
void squareByReference( int & ); // function prototype (reference pass)
10 int main()
11 {
12
Function illustrating pass-by-reference
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
Variable is simply mentioned
by name in both function calls
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
Receives copy of argument in main
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 )
Receives reference to argument in main
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
Modifies variable in main
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.
References and Reference
Parameters
• References
– Can also be used as aliases for other variables
within a function
• All operations supposedly performed on the alias (i.e., the
reference) 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
– Example
int count = 1;
int &cRef = count;
cRef++;
– Increments count through alias cRef
1
2
3
// Fig. 6.20: fig06_20.cpp
// References must be initialized.
#include <iostream>
4
5
6
7
8
using std::cout;
using std::endl;
9
int x = 3;
10
11
12
13
14
int &y = x; // y refers to (is an alias for) x
cout << "x = " << x << endl << "y = " << y << endl;
y = 7; // actually modifies x
cout << "x = " << x << endl << "y = " << y << endl;
15
return 0; // indicates successful termination
int main()
{
16 } // end main
x
y
x
y
=
=
=
=
3
3
7
7
Creating a reference as an alias to
another variable in the function
Assign 7 to x through alias y
1
// Fig. 6.21: fig06_21.cpp
2
// References must be initialized.
3
#include <iostream>
4
5
6
using std::cout;
using std::endl;
7 int main()
Uninitialized reference
8 {
9
int x = 3;
10
int &y; // Error: y must be initialized
11
12
13
14
cout << "x = " << x << endl << "y = " << y << endl;
y = 7;
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
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
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.
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.
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.
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
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
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 · 1
5! = 5 · ( 4 · 3 · 2 · 1)
5! = 5 · ( 4! )
Recursive evaluation of 5!.
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
17
cout << setw( 2 ) << counter << "! = " << factorial( counter )
<< endl;
18
19
return 0; // indicates successful termination
20 } // end main
First call to factorial function
21
22 // recursive definition of function factorial
23 unsigned long factorial( unsigned long number )
24 {
if ( number <= 1 ) // test for base case
25
Base cases simply return 1
return 1; // base cases: 0! = 1 and 1! = 1
26
else // recursion step
27
return number * factorial( number - 1 );
28
29 } // end function factorial
0!
1!
2!
3!
4!
5!
6!
7!
8!
9!
10!
=
=
=
=
=
=
=
=
=
=
=
1
1
2
6
24
120
720
5040
40320
362880
3628800
Recursive call to factorial function
with a slightly smaller problem
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 ]
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++.
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
Examples Using Arrays
• Initializing an array in a declaration with
an 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
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 {
Declare n as an array of
ints
Compiler uses
initializer list to
initialize array n
initialize array
12
// use initializer list to
13
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
14
15
cout << "Element" << setw( 13 ) << "Value" << endl;
16
17
// output each array element's value
18
for ( int i = 0; i < 10; i++ )
cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;
19
20
21
return 0; // indicates successful termination
22 } // end main
Element
0
1
2
3
4
5
6
7
8
9
Value
32
27
64
18
95
14
90
70
60
37
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
1
2
// Fig. 7.5: fig07_05.cpp
// Set array s to the even integers from 2 to 20.
3
4
5
#include <iostream>
using std::cout;
using std::endl;
6
7
8
#include <iomanip>
using std::setw;
9
10 int main()
11 {
12
13
14
15
16
17
18
Declare constant variable arraySize
using the const keyword
// constant variable can be used to specify array size
const int arraySize = 10;
int s[ arraySize ]; // array s has 10 elements
for ( int i = 0; i < arraySize; i++ ) // set the values
s[ i ] = 2 + 2 * i;
Declare array that contains 10 ints
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 stringtermination character called the null character ('\0')
Examples Using Arrays
• Using character arrays to store and
manipulate strings (Cont.)
– Can also be initialized with individual
character constants in an initializer list
char 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;
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;
Store "string
literal" as an array
of characters
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]
Initializing an
array of characters
using cin
16
17
// output strings
18
cout << "string1 is: " << string1 << "\nstring2 is: " << string2;
19
20
cout << "\nstring1 with spaces between characters is:\n";
21
22
// output characters until null character is reached
23
24
for ( int i = 0; string1[ i ] != '\0'; i++ )
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
Loop until the
terminating null
character is
reached
Accessing
specific characters
in the array
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
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
23
24 // function to demonstrate a static local array
25 void staticArrayInit( void )
26 {
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// initializes elements to 0 first time function is called
static int array1[ 3 ]; // static local array
cout << "\nValues on entering staticArrayInit:\n";
// output contents of array1
for ( int i = 0; i < 3; i++ )
cout << "array1[" << i << "] = " << array1[ i ] << "
Create a static array
using keyword static
";
cout << "\nValues on exiting staticArrayInit:\n";
// modify and output contents of array1
for ( int j = 0; j < 3; j++ )
cout << "array1[" << j << "] = " << ( array1[ j ] += 5 ) << "
} // end function staticArrayInit
";
// function to demonstrate an automatic local array
void automaticArrayInit( void )
{
// initializes elements each time function is called
int array2[ 3 ] = { 1, 2, 3 }; // automatic local array
cout << "\n\nValues on entering automaticArrayInit:\n";
Create an automatic local array
50
51
// output contents of array2
52
53
for ( int i = 0; i < 3; i++ )
cout << "array2[" << i << "] = " << array2[ i ] << "
54
55
56
57
58
59
";
cout << "\nValues on exiting automaticArrayInit:\n";
// modify and output contents of array2
for ( int j = 0; j < 3; j++ )
cout << "array2[" << j << "] = " << ( array2[ j ] += 5 ) << "
";
60 } // end function automaticArrayInit
First call to each function:
Values on
array1[0]
Values on
array1[0]
entering staticArrayInit:
= 0 array1[1] = 0 array1[2] = 0
exiting staticArrayInit:
= 5 array1[1] = 5 array1[2] = 5
Values on
array2[0]
Values on
array2[0]
entering automaticArrayInit:
= 1 array2[1] = 2 array2[2] = 3
exiting automaticArrayInit:
= 6 array2[1] = 7 array2[2] = 8
Second call to each function:
Values on
array1[0]
Values on
array1[0]
entering staticArrayInit:
= 5 array1[1] = 5 array1[2] = 5
exiting staticArrayInit:
= 10 array1[1] = 10 array1[2] = 10
Values on
array2[0]
Values on
array2[0]
entering automaticArrayInit:
= 1 array2[1] = 2 array2[2] = 3
exiting automaticArrayInit:
= 6 array2[1] = 7 array2[2] = 8
Values reflect changes from the previous
function call – the array was not reinitialized
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
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)
Pointer Variable Declarations and
Initialization
30
45
number2
number2
number1
number1
int number1;
int *number1;
Directly and indirectly
referencing a variable.
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
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
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.
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.
1
// Fig. 8.4: fig08_04.cpp
2
// Using the & and * operators.
3
#include <iostream>
4
using std::cout;
5
using std::endl;
Outline
Variable aPtr is
a point to an int
6
7
int main()
8
{
Initialize aPtr with the
address of variable a
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
14
aPtr = &a; // assign the address of a to aPtr
15
cout << "The address of a is " << &a
16
17
18
19
<< "\nThe value of aPtr is " << aPtr;
cout << "\n\nThe value of a is " << a
<< "\nThe value of *aPtr is " << *aPtr;
cout << "\n\nShowing that * and & are inverses of "
20
<< "each other.\n&*aPtr = " << &*aPtr
21
<< "\n*&aPtr = " << *&aPtr << endl;
22
Address of a and the value
of aPtr are identical
return 0; // indicates successful termination
Value of a and the dereferenced
aPtr are identical
* and & are inverses
of each other
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
* and & are inverses;
*&aPtr = 0012F580
same result
when both are applied to aPtr
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”
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
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;
Outline
6
7
int cubeByValue( int ); // prototype
8
9
Pass number by value; result
returned by cubeByValue
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 )
cubeByValue receives
parameter passed-by-value
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
Cubes local variable n
and return the result
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;
Outline
Prototype indicates parameter
is a pointer to an int
6
7
void cubeByReference( int * ); // prototype
8
9
Apply address operator & to
pass address of number to
cubeByReference
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
cubeByReference
modifies variable number
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
cubeByReference receives
address of an int variable,
i.e., a pointer to an int
25 } // end function cubeByReference
The original value of number is 5
The new value of number is 125
Modify and access int
variable using indirection
operator *
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
pointer
• 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
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
and the protection of pass-by-value
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
Outline
Parameter is a nonconstant
pointer to constant data
9
10 int main()
11 {
Pass pointer phrase to function
printCharacters
12
13
const char phrase[] = "print characters of a string";
14
cout << "The string is:\n";
15
16
17
printCharacters( phrase ); // print characters in phrase
cout << endl;
sPtr
return 0; // indicates successful termination
18
19
20
21
22
} // end main
// sPtr can be modified, but it cannot modify the character to which
// it points, i.e., sPtr is a "read-only" pointer
void printCharacters( const char *sPtr )
Increment sPtr to point to the next character
23 {
24
is a nonconstant pointer to constant data;
it cannot modify the character to which it points
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
1
// Fig. 8.12: fig08_12.cpp
2
3
// Attempting to modify data through a
// non-constant pointer to constant data.
4
5
void f( const int * ); // prototype
6
7
8
int main()
{
9
Outline
Parameter is a nonconstant
pointer to constant data
int y;
10
11
f( &y ); // f attempts illegal modification
12
return 0; // indicates successful termination
13 } // end main
Pass the address of int variable y
to attempt an illegal modification
14
15 // xPtr cannot modify the value of constant variable to which it points
Outline
16 void f( const int *xPtr )
17 {
18
*xPtr = 100; // error: cannot modify a const object
19 } // end function f
Attempt to modify a const
object pointed to by xPtr
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
Error produced when
attempting to compile
GNU C++ compiler error message:
fig08_12.cpp: In function `void f(const int*)':
fig08_12.cpp:18: error: assignment of read-only location
• Pass large objects using pointers to constant data,
or references to constant data, to obtain the
security of pass-by-value.
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
1
// Fig. 8.13: fig08_13.cpp
2
// Attempting to modify a constant pointer to non-constant data.
3
4
int main()
5
6
7
8
9
10
11
{
Outline
int x, y;
// ptr is a constant pointer to an integer that can
// be modified through ptr, but ptr always points to the
// same memory location.
int * const ptr = &x; // const pointer must be initialized
12
13
14
ptr is a constant pointer to an integer
Can modify x (pointed to by
ptr) since x is not constant
*ptr = 7; // allowed: *ptr is not const
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:
Cannot modify ptr to point to a
new address since ptr is constant
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'
Line 14 generates a compiler
error by attempting to assign
a new address to a constant
pointer
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
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;
Outline
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;
ptr is a constant pointer
to a constant integer
15
16
cout << *ptr << endl;
17
Cannot modify x (pointed to by
ptr) since *ptr is constant
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
Cannot modify ptr to point to a
new address since ptr is constant
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
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 ]
Array v and a pointer variable
vPtr that points to v.
after pointer
arithmetic
Relationship Between Pointers and
Arrays
• Arrays and pointers are closely related
– Array name is like constant pointer
– Pointers can do array subscripting operations
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 ]
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.
1
2
// Fig. 8.20: fig08_20.cpp
// Using subscripting and pointer notations with arrays.
3
4
5
#include <iostream>
using std::cout;
using std::endl;
6
7
8
9
int main()
{
int b[] = { 10, 20, 30, 40 }; // create 4-element array b
Outline
int *bPtr = b; // set bPtr to point to array b
10
11
12
13
// output array b using array subscript notation
cout << "Array b printed with:\n\nArray subscript notation\n";
14
15
16
for ( int i = 0; i < 4; i++ )
cout << "b[" << i << "] = " << b[ i ] << '\n';
Using array subscript notation
17
18
19
20
// output array b using the array name and pointer/offset notation
cout << "\nPointer/offset notation where "
Using
<< "the pointer is the array name\n";
21
22
23
for ( int offset1 = 0; offset1 < 4; offset1++ )
cout << "*(b + " << offset1 << ") = " << *( b + offset1 ) << '\n';
array name and
pointer/offset notation
24
25
// output array b using bPtr and array subscript notation
Outline
26
27
28
29
30
31
32
33
cout << "\nPointer subscript notation\n";
34
for ( int offset2 = 0; offset2 < 4; offset2++ )
35
cout << "*(bPtr + " << offset2 << ") = "
for ( int j = 0; j < 4; j++ )
cout << "bPtr[" << j << "] = " << bPtr[ j ] << '\n';
cout << "\nPointer/offset notation\n";
Using pointer subscript notation
// output array b using bPtr and pointer/offset notation
36
<< *( bPtr + offset2 ) << '\n';
37
38
return 0; // indicates successful termination
39 } // end main
Using
pointer name and pointer/offset notation
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
bPtr[0]
bPtr[1]
bPtr[2]
bPtr[3]
subscript notation
= 10
= 20
= 30
= 40
Pointer/offset notation
*(bPtr + 0) = 10
*(bPtr + 1) = 20
*(bPtr + 2) = 30
*(bPtr + 3) = 40
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
24
25 // copy s2 to s1 using array notation
Use array subscript notation to copy
string in s2 to character array s1
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 {
Use pointer notation to copy string
in s2 to character array in s1
36
// copying occurs in the for header
37
38
for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ )
; // do nothing in body
39 } // end function copy2
string1 = Hello
string3 = Good Bye
Increment both pointers to point to
next elements in corresponding arrays
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
Graphical representation of the
suit array.
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
1
// Fig. 9.1: Time.h
2
3
// Declaration of class Time.
// Member functions are defined in Time.cpp
4
5
6
7
8
9
10
// prevent multiple inclusions of header file
#ifndef TIME_H
Preprocessor directive
#define TIME_H
// Time class definition
class Time
#ifndef determines whether a name is defined
Preprocessor directive #define defines a name (e.g., TIME_H)
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 #endif marks the end of the
code that should not be included multiple times
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.
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
27
28 // print Time in universal-time format (HH:MM:SS)
29 void Time::printUniversal()
30 {
31
32
Using setfill stream manipulator to specify a fill character
cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< 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
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
13
Time t; // instantiate object t of class Time
14
15
// output Time object t's initial values
cout << "The initial universal time is ";
16
17
18
t.printUniversal(); // 00:00:00
cout << "\nThe initial standard time is ";
t.printStandard(); // 12:00:00 AM
19
20
21
t.setTime( 13, 27, 6 ); // change time
22
23
// output Time object t's new values
cout << "\n\nUniversal time after setTime is ";
24
t.printUniversal(); // 13:27:06
25
26
cout << "\nStandard time after setTime is ";
t.printStandard(); // 1:27:06 PM
27
28
t.setTime( 99, 99, 99 ); // attempt invalid settings
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
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
displayed in fields wider than the value being
displayed
Declaration of Object
• Using class Time
– Once class Time has been defined, it can be
used in declarations
• Time
• Time
• Time
• Time
sunset;
arrayOfTimes[ 5 ];
&dinnerTime = sunset;
*timePtr = &dinnerTime;
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.
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.
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
{
x = value;
14
15
} // end function setX
16
17
// prints the value of private data member x
18
void print()
19
{
20
21
cout << x << endl;
} // end function print
22
23 private:
24
int x;
25 }; // end class Count
26
27 int main()
28 {
29
Count counter; // create counter object
30
31
Count *counterPtr = &counter; // create pointer to counter
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
Using the dot member selection operator with an object
Using the dot member selection operator with a reference
37
38
cout << "Set x to 2 and print using a reference to an object: ";
counterRef.setX( 2 ); // set data member x to 2
39
40
41
42
counterRef.print(); // call member function print
Using the arrow member selection operator with a pointer
cout << "Set x to 3 and print using a pointer to an object: ";
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
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
holding many objects
• Utility functions (also called helper functions)
– private member functions that support the
operation 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
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:
Prototype for a private utility function
15
double totalAnnualSales(); // prototype for utility function
16
double sales[ 12 ]; // 12 monthly sales figures
17 }; // end class SalesPerson
18
19 #endif
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
18
for ( int i = 0; i < 12; i++ )
sales[ i ] = 0.0;
19 } // end SalesPerson constructor
20
21 // get 12 sales figures from the user at the keyboard
22 void SalesPerson::getSalesFromUser()
23 {
24
double salesFigure;
25
26
27
for ( int i = 1; i <= 12; i++ )
{
28
29
30
31
cout << "Enter sales amount for month " << i << ": ";
cin >> salesFigure;
setSales( i, salesFigure );
} // 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
39
40
// test for valid month and amount values
if ( month >= 1 && month <= 12 && amount > 0 )
sales[ month - 1 ] = amount; // adjust for subscripts 0-11
41
42
else // invalid month or amount value
cout << "Invalid month or sales figure" << endl;
43 } // end function setSales
44
45 // print total annual sales (with the help of utility function)
46 void SalesPerson::printAnnualSales()
47 {
48
cout << setprecision( 2 ) << fixed
Calling a private utility function
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
Definition of a private utility function
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
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
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
sales
sales
sales
sales
sales
sales
sales
sales
sales
sales
sales
sales
amount
amount
amount
amount
amount
amount
amount
amount
amount
amount
amount
amount
for
for
for
for
for
for
for
for
for
for
for
for
month
month
month
month
month
month
month
month
month
month
month
month
1: 5314.76
2: 4292.38
3: 4589.83
4: 5534.03
5: 4376.34
6: 5698.45
7: 4439.22
8: 5893.57
9: 4909.67
10: 5123.45
11: 4024.97
12: 5923.92
The total annual sales are: $60120.59
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
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
Prototype of a constructor with default arguments
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)
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
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
Parameters could receive the default values
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
1
2
// Fig. 9.10: fig09_10.cpp
// Demonstrating a default constructor for class Time.
3
4
5
#include <iostream>
using std::cout;
using std::endl;
6
7
8
#include "Time.h" // include definition of class Time from Time.h
9 int main()
10 {
Initializing Time objects
using 0, 1, 2 and 3 arguments
11
12
Time t1; // all arguments defaulted
Time t2( 2 ); // hour specified; minute and second defaulted
13
14
Time t3( 21, 34 ); // hour and minute specified; second defaulted
Time t4( 12, 25, 42 ); // hour, minute and second specified
15
16
Time t5( 27, 74, 99 ); // all bad values specified
17
cout << "Constructed with:\n\nt1: all arguments defaulted\n
18
19
20
21
t1.printUniversal(); // 00:00:00
cout << "\n ";
t1.printStandard(); // 12:00:00 AM
22
23
24
cout << "\n\nt2: hour specified; minute and second defaulted\n
t2.printUniversal(); // 02:00:00
cout << "\n ";
25
t2.printStandard(); // 2:00:00 AM
";
";
26
27
28
cout << "\n\nt3: hour and minute specified; second defaulted\n
t3.printUniversal(); // 21:34:00
29
cout << "\n
30
31
t3.printStandard(); // 9:34:00 PM
32
33
34
35
cout << "\n\nt4: hour, minute and second specified\n
t4.printUniversal(); // 12:25:42
cout << "\n ";
t4.printStandard(); // 12:25:42 PM
";
36
37
cout << "\n\nt5: all invalid values specified\n
38
39
40
t5.printUniversal(); // 00:00:00
cout << "\n ";
t5.printStandard(); // 12:00:00 AM
41
cout << endl;
42
return 0;
43 } // end main
";
";
";
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
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
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