1
Chapter 7: Classes Part II
Outline
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.8.1
7.8.2
7.8.3
7.9
7.10
Introduction
const (Constant) Objects and const Member Functions
Composition: Objects as Members of Classes
friend Functions and friend Classes
Using the this Pointer
Dynamic Memory Allocation with Operators new and delete
static Class Members
Data Abstraction and Information Hiding
Example: Array Abstract Data Type
Example: String Abstract Data Type
Example: Queue Abstract Data Type
Container Classes and Iterators
Proxy Classes
 2000 Prentice Hall, Inc. All rights reserved.
2
7.1 Introduction
• Chapters 6 through 8 discuss object-based
programming (OBP)
• Chapters 9 and 10 discuss inheritance and
polymorphism
 2000 Prentice Hall, Inc. All rights reserved.
3
7.2
const (Constant) Objects and const
Member Functions
• Principle of least privilege
– Only give objects permissions they need, no more
• Keyword const
– Specify that an object is not modifiable
– Any attempt to modify the object is a syntax error
– Example
const Time noon( 12, 0, 0 );
• Declares a const object noon of class Time and initializes it
to 12
 2000 Prentice Hall, Inc. All rights reserved.
4
7.2
const (Constant) Objects and const
Member Functions
• const objects require const functions
– Member functions declared const cannot modify their object
– const must be specified in function prototype and definition
– Prototype:
ReturnType FunctionName(param1,param2…) const;
– Definition:
ReturnType FunctionName(param1,param2…) const { …}
– Example:
int A::getValue() const { return
privateDataMember };
• Returns the value of a data member but doesn’t modify anything so is
declared const
• Constructors / Destructors cannot be const
– They need to initialize variables, therefore modifying them
 2000 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Fig. 7.1: time5.h
5
Outline
// Declaration of the class Time.
// Member functions defined in time5.cpp
#ifndef TIME5_H
#define TIME5_H
class Time {
public:
Time( int = 0, int = 0, int = 0 );
1. Class definition
1.1 Function
prototypes
// default constructor
1.2 Member variables
// set functions
void setTime( int, int, int ); // set time
void setHour( int );
// set hour
void setMinute( int );
// set minute
void setSecond( int );
// set second
// get functions (normally declared const)
int getHour() const;
// return hour
int getMinute() const;
// return minute
int getSecond() const;
// return second
const
functions
// print functions (normally declared const)
void printMilitary() const; // print military time
void printStandard();
// print standard timenon-const
private:
functions
int hour;
// 0 - 23
int minute;
// 0 - 59
int second;
// 0 - 59
};

2000 Prentice Hall, Inc. All rights reserved.
#endif
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Fig. 7.1: time5.cpp
// Member function definitions for Time class.
#include <iostream>
using std::cout;
The constructor is non-const but it can be
called for const objects.
#include "time5.h"
// Constructor function to initialize private data.
// Default values are 0 (see class definition).
Time::Time( int hr, int min, int sec )
{ setTime( hr, min, sec ); }
// Set the values of hour, minute, and second.
void Time::setTime( int h, int m, int s )
{
setHour( h );
setMinute( m );
setSecond( s );
}
// Set the hour value
void Time::setHour( int h )
{ hour = ( h >= 0 && h < 24 ) ? h : 0; }
// Set the minute value
void Time::setMinute( int m )
{ minute = ( m >= 0 && m < 60 ) ? m : 0; }
// Set the second value
void Time::setSecond( int s )
 2000
Prentice Hall,
All rights
{ second
= (Inc.
s >=
0 &&reserved.
s < 60 ) ? s : 0; }
6
Outline
Source Code
1. Load Header
1.1 Function
definitions
64
7
Outline
65 // Get the hour value
66 int Time::getHour() const { return hour; }
1.1 Function
Keyword const in function
definition and prototype.definitions
67
68 // Get the minute value
69 int Time::getMinute() const { return minute; }
70
71 // Get the second value
72 int Time::getSecond() const { return second; }
1.2 Purposely leave
out const keyword for
printStandard
73
74 // Display military format time: HH:MM
75 void Time::printMilitary() const
76 {
77
cout << ( hour < 10 ? "0" : "" )
78
<< ( minute < 10 ? "0" : ""
Non-const functions cannot use
const objects, even if they don’t
<< hour << ":"
modify them (such as
) << minute;printStandard).
79 }
80
81 // Display standard format time: HH:MM:SS AM (or PM)
82 void Time::printStandard()
// should be const
83 {
84
cout << ( ( hour == 12 ) ? 12 : hour % 12 ) << ":"
85
<< ( minute < 10 ? "0" : "" ) << minute << ":"
86
<< ( second < 10 ? "0" : "" ) << second
87
<< ( hour < 12 ? " AM" : " PM" );
88 
} 2000 Prentice Hall, Inc. All rights reserved.
89 // Fig. 7.1: fig07_01.cpp
90 // Attempting to access a const object with
91 // non-const member functions.
92 #include "time5.h"
93
94 int main()
95 {
96
Time wakeUp( 6, 45, 0 );
// non-constant object
97
const Time noon( 12, 0, 0 );
// constant object
98
99
// MEMBER FUNCTION
OBJECT
100
wakeUp.setHour( 18 ); // non-const
non-const
101
102
noon.setHour( 12 );
// non-const
const
103
104
wakeUp.getHour();
// const
non-const
105
106
noon.getMinute();
// const
const
107
noon.printMilitary(); // const
const
108
noon.printStandard(); // non-const
const
109
return 0;
110 }
8
Outline
1. Initialize variables
2. Attempt to use nonconst functions with
const objects
Compiler errors generated.
Compiling...
Fig07_01.cpp
d:fig07_01.cpp(14) : error C2662: 'setHour' : cannot convert 'this'
pointer from 'const class Time' to 'class Time &'
Conversion loses qualifiers
d:\fig07_01.cpp(20) : error C2662: 'printStandard' : cannot convert
'this' pointer from 'const class Time' to 'class Time &'
Conversion loses qualifiers
Time5.cpp
Error executing cl.exe.
test.exe - 2 error(s), 0 warning(s)
 2000 Prentice Hall, Inc. All rights reserved.
Program Output
9
7.2
const (Constant) Objects and const
Member Functions
• Member initializer syntax
– Data member increment in class Increment
– constructor for Increment is modified as follows:
Increment::Increment( int c, int i )
: increment( i )
{ count = c; }
– : increment( i ) initializes increment to i
– All data members can be initialized using member initializer
syntax
– consts and references must be initialized using member
initializer syntax
– Multiple member initializers
• Use comma-separated list after the colon
 2000 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Fig. 7.2: fig07_02.cpp
// Using a member initializer to initialize a
// constant of a built-in data type.
#include <iostream>
10
Outline
1. Class definition
using std::cout;
using std::endl;
class Increment {
public:
Increment( int c = 0, int i = 1 );
void addIncrement() { count += increment; }
void print() const;
private:
int count;
const int increment;
};
// const data member
// Constructor for class Increment
Increment::Increment( int c, int i )
: increment( i )
// initializer for const member
{ count = c; }
// Print the data
void Increment::print() const
{
cout << "count = " << count
<< ", increment = " << increment << endl;
}
int main()
1.1 Function
definitions

{ 2000 Prentice Hall, Inc. All rights reserved.
If we try to initialize increment
with an assignment statement
(such as increment = i )
instead of a member initializer we
get an error.
34
Increment value( 10, 5 );
11
Outline
35
36
cout << "Before incrementing: ";
37
value.print();
1.2 Initialize variables
38
39
for ( int j = 0; j < 3; j++ ) {
2. Function calls
40
value.addIncrement();
41
cout << "After increment " << j + 1 << ": ";
42
value.print();
43
3. Output results
}
44
45
return 0;
46 }
Before incrementing: count
After increment 1: count =
After increment 2: count =
After increment 3: count =
= 10, increment
15, increment =
20, increment =
25, increment =
 2000 Prentice Hall, Inc. All rights reserved.
= 5
5
5
5
12
7.3
Composition: Objects as Members of
Classes
• Composition
– Class has objects of other classes as members
• Construction of objects
– Member objects constructed in order declared
• Not in order of constructor’s member initializer list
– Constructed before their enclosing class objects (host objects)
 2000 Prentice Hall, Inc. All rights reserved.
1
// Fig. 7.4: date1.h
2
// Declaration of the Date class.
3
// Member functions defined in date1.cpp
4
#ifndef DATE1_H
5
#define DATE1_H
13
Outline
1. Class definition
1.1 Member functions
6
7
class Date {
8
public:
9
1.2 Member variables
Date( int = 1, int = 1, int = 1900 ); // default constructor
10
void print() const;
11
~Date();
// print date in month/day/year format
// provided to confirm destruction order
12 private:
13
int month;
// 1-12
14
int day;
// 1-31 based on month
15
int year;
// any year
16
17
// utility function to test proper day for month and year
18
int checkDay( int );
19 };
20
21 
#endif
2000 Prentice Hall, Inc. All rights reserved.
22 // Fig. 7.4: date1.cpp
14
Outline
23 // Member function definitions for Date class.
24 #include <iostream>
25
1. Load header
26 using std::cout;
27 using std::endl;
1.1 Function
definitions
28
29 #include "date1.h"
30
31 // Constructor: Confirm proper value for month;
1.2 Date constructor
32 // call utility function checkDay to confirm proper
33 // value for day.
34 Date::Date( int mn, int dy, int yr )
35 {
36
if ( mn > 0 && mn <= 12 )
37
38
// validate the month
month = mn;
else {
39
month = 1;
40
cout << "Month " << mn << " invalid. Set to month 1.\n";
41
}
42
43
year = yr;
// should validate yr
44
day = checkDay( dy );
// validate the day
Constructor will print
a line when called.
45
46
cout << "Date object constructor for date ";
47
print();
48
cout << endl;
49 }
// interesting: a print with no arguments
50  2000 Prentice Hall, Inc. All rights reserved.
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
Destructor will print
// Destructor: provided to confirm destruction order
a line when called.
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
}
// Utility function to confirm proper day value
// based on month and year.
// Is the year 2000 a leap year?
int Date::checkDay( int testDay )
{
static const int daysPerMonth[ 13 ] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 &&
// February: Check for leap year
testDay == 29 &&
( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1;
// leave object in consistent state if bad value

} 2000 Prentice Hall, Inc. All rights reserved.
15
Outline
1.3 print function
1.4 Date destructor
1.5 checkDay function
84 // Fig. 7.4: emply1.h
16
85 // Declaration of the Employee class.
Outline
86 // Member functions defined in emply1.cpp
87 #ifndef EMPLY1_H
1. Load header
88 #define EMPLY1_H
89
1.1 Class definition
90 #include "date1.h"
91
92 class Employee {
1.2 Member functions
93 public:
94
Employee( char *, char *, int, int, int, int, int, int );
95
void print() const;
96
~Employee();
1.3 Member variables
// provided to confirm destruction order
1.3.1 Include const
variables from Date
class
97 private:
98
char firstName[ 25 ];
99
char lastName[ 25 ];
100
const Date birthDate;
101
const Date hireDate;
102 };
103
104 #endif
 2000 Prentice Hall, Inc. All rights reserved.
Composition - including
objects of other classes.
105 // Fig. 7.4: emply1.cpp
17
106 // Member function definitions for Employee class.
107 #include <iostream>
108
1. Load header files
109 using std::cout;
110 using std::endl;
111
1.1 Function
112 #include <cstring>
definitions
113 #include "emply1.h"
114 #include "date1.h"
115
1.2 Employee
116 Employee::Employee( char *fname, char *lname,
constructor
117
int bmonth, int bday, int byear,
118
int hmonth, int hday, int hyear )
119
: birthDate( bmonth, bday, byear ),
1.2.1 Use member120
hireDate( hmonth, hday, hyear )
initializer syntax for
121 {
const Date members
122
// copy fname into firstName and be sure that it fits
123
int length = strlen( fname );
124
length = ( length < 25 ? length : 24 );
125
strncpy( firstName, fname, length );
126
firstName[ length ] = '\0';
127
128
// copy lname into lastName and be sure that it fits
129
length = strlen( lname );
130
length = ( length < 25 ? length : 24 );
Constructor will
131
strncpy( lastName, lname, length );
print a line when
132
lastName[ length ] = '\0';
called.
133
134
cout << "Employee object constructor: "
135
<< firstName << ' ' << lastName << endl;

2000
Prentice
Hall, Inc. All rights reserved.
136 }
Outline
137
18
Outline
138 void Employee::print() const
1.3 print definition
139 {
140
cout << lastName << ", " << firstName << "\nHired: ";
141
hireDate.print();
142
cout << "
143
birthDate.print();
144
cout << endl;
Birth date: ";
1.4 Employee
destructor
The print function is const and will
print whenever a Date object is created
or destroyed. It can print const
objects because it is a const function.
Print requires no arguments, it is
linked implicitly to the object that calls
it.
145 }
146
147 // Destructor: provided to confirm destruction order
148 Employee::~Employee()
149 {
150
151
152 }
cout << "Employee object destructor: "
<< lastName << ", " << firstName << endl;
 2000 Prentice Hall, Inc. All rights reserved.
Destructor will
print a line when
called.
153 // Fig. 7.4: fig07_04.cpp
19
154 // Demonstrating composition: an object with member objects.
155 #include <iostream>
Outline
1. Load header files
156
157 using std::cout;
2. Create Employee
object
158 using std::endl;
159
160 #include "emply1.h"
Only emply.h has to be loaded;
that file has the command to load
date.h.
161
162 int main()
163 {
164
Employee e( "Bob", "Jones", 7, 24, 1949, 3, 12, 1988 );
165
166
cout << '\n';
167
e.print();
168
169
cout << "\nTest Date constructor with invalid values:\n";
170
Date d( 14, 35, 1994 );
171
cout << endl;
172
return 0;
// invalid Date values
173
} 2000 Prentice Hall, Inc. All rights reserved.
2.1 Attempt invalid
Date setting
Date object constructor for date 7/24/1949
Date object constructor for date 3/12/1988
Employee object constructor: Bob Jones
Jones, Bob
Hired: 3/12/1988
20
Outline
Birth date: 7/24/1949
Test Date constructor with invalid values:
Month 14 invalid. Set to month 1.
Day 35 invalid. Set to day 1.
Date object constructor for date 1/1/1994
Program Output
Date object destructor for date 1/1/1994
Employee object destructor: Jones, Bob
Date object destructor for date 3/12/1988
Date object destructor for date 7/24/1949
Notice how inner objects are created first and
destroyed last.
 2000 Prentice Hall, Inc. All rights reserved.
21
7.4
friend Functions and friend Classes
• friend function and friend classes
– Can access private and protected members of another
class
– friend functions are not member functions of class
• Defined outside of class scope
• Properties of friendship
– Friendship is granted, not taken
– Not symmetric (if B a friend of A, A not necessarily a
friend of B)
– Not transitive (if A a friend of B, B a friend of C, A not
necessarily a friend of C)
 2000 Prentice Hall, Inc. All rights reserved.
22
7.4
friend Functions and friend Classes
• friend declarations
– To declare a friend function
• Type friend before the function prototype in the class that is
giving friendship
friend int myFunction( int x );
should appear in the class giving friendship
– To declare a friend class
– Type friend class Classname in the class that is
giving friendship
– if ClassOne is granting friendship to ClassTwo,
friend class ClassTwo;
–
should appear in ClassOne's definition
 2000 Prentice Hall, Inc. All rights reserved.
1 // Fig. 7.5: fig07_05.cpp
2 // Friends can access private members of a class.
3 #include <iostream>
4
5
6
7
8
9
10
using std::cout;
using std::endl;
23
Outline
setX a friend of class
Count (can access
private data).
// Modified Count class
class Count {
friend void setX( Count &, int ); // friend declaration
11 public:
12
Count() { x = 0; }
// constructor
13
void print() const { cout << x << endl; } // output
14 private:
15
int x; // data member
16 };
setX is defined
17
18
19
20
21
22
23
normally
and is not a member
function of Count.
Count
// Can modify private data of Count because
// setX is declared as a friend function of
void setX( Count &c, int val )
Changing private
{
c.x = val; // legal: setX is a friend of Count
}
24
25 int main()
26 {
27
Count counter;
28
29
cout << "counter.x after instantiation: ";
Prentice Hall, Inc. All rights reserved.
30  2000
counter.print();
variables allowed.
1. Class definition
1.1 Declare function a
friend
1.2 Function definition
1.3 Initialize Count
object
31
cout << "counter.x after call to setX friend function: ";
32
setX( counter, 8 );
33
counter.print();
34
return 0;
// set x with a friend
24
Outline
2. Modify object
3. Print results
35 }
counter.x after instantiation: 0
counter.x after call to setX friend function: 8
private data was changed.
 2000 Prentice Hall, Inc. All rights reserved.
Program Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Fig. 7.6: fig07_06.cpp
// Non-friend/non-member functions cannot access
// private data of a class.
#include <iostream>
using std::cout;
using std::endl;
cannotSetX is not a friend
of class Count. It cannot access
private data.
// Modified Count class
class Count {
public:
Count() { x = 0; }
// constructor
void print() const { cout << x << endl; } // output
private:
int x; // data member
};
// Function tries to modify private data of Count,
// but cannot because it is not a friend of Count.
void cannotSetX( Count &c, int val )
{
c.x = val; // ERROR: 'Count::x' is not accessible
}
int main()
{
Count counter;
cannotSetX tries to
modify a private
variable…
cannotSetX( counter, 3 ); // cannotSetX is not a friend
return 0;

} 2000 Prentice Hall, Inc. All rights reserved.
25
Outline
(Previous program
without friend
declared)
Compiling...
Fig07_06.cpp
D:\books\2000\cpphtp3\examples\Ch07\Fig07_06\Fig07_06.cpp(22) :
error C2248: 'x' : cannot access private member declared in
class 'Count'
D:\books\2000\cpphtp3\examples\Ch07\Fig07_06\
Fig07_06.cpp(15) : see declaration of 'x'
Error executing cl.exe.
test.exe - 1 error(s), 0 warning(s)
Expected compiler error - cannot
access private data
 2000 Prentice Hall, Inc. All rights reserved.
26
Outline
Program Output
27
7.5
Using the this Pointer
• this pointer
– Allows objects to access their own address
– Not part of the object itself
– Implicit first argument on non-static member function call to the
object
– Implicitly reference member data and functions
– The type of the this pointer depends upon the type of the object
and whether the member function using this is const
– In a non-const member function of Employee, this has type
Employee * const
• Constant pointer to an Employee object
– In a const member function of Employee, this has type
const Employee * const
• Constant pointer to a constant Employee object
 2000 Prentice Hall, Inc. All rights reserved.
28
7.5
Using the this Pointer
• Examples using this
– For a member function print data member x, either
this->x
or
( *this ).x
• Cascaded member function calls
– Function returns a reference pointer to the same object
{ return *this; }
– Other functions can operate on that pointer
– Functions that do not return references must be called last
 2000 Prentice Hall, Inc. All rights reserved.
29
7.5
Using the this Pointer
• Example of cascaded member function calls
– Member functions setHour, setMinute, and setSecond
all return *this (reference to an object)
– For object t, consider
t.setHour(1).setMinute(2).setSecond(3);
– Executes t.setHour(1), returns *this (reference to
object) and the expression becomes
t.setMinute(2).setSecond(3);
– Executes t.setMinute(2), returns reference and becomes
t.setSecond(3);
– Executes t.setSecond(3), returns reference and becomes
t;
– Has no effect
 2000 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Fig. 7.7: fig07_07.cpp
// Using the this pointer to refer to object members.
#include <iostream>
Test::Test( int a ) { x = a; }
Outline
1. Class definition
using std::cout;
using std::endl;
class Test {
public:
Test( int = 0 );
void print() const;
private:
int x;
};
30
1.1 Function definition
// default constructor
Printing x directly.
// constructor
1.2 Initialize object
2. Function call
Print x using the arrow -> operator
required
off the this pointer.
void Test::print() const
// ( ) around *this
{
cout << "
x = " << x
<< "\n this->x = " << this->x
<< "\n(*this).x = " << ( *this ).x << endl;
}
int main()
{
Test testObject( 12 );
testObject.print();
return 0;

} 2000 Prentice Hall, Inc. All rights reserved.
Printing x using the dot (.) operator. Parenthesis
required because dot operator has higher precedence
than *. Without, interpreted incorrectly as
*(this.x).
31
Outline
x = 12
this->x = 12
(*this).x = 12
Program Output
All three methods have
the same result.
 2000 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Fig. 7.8: time6.h
// Cascading member function calls.
32
Outline
// Declaration of class Time.
// Member functions defined in time6.cpp
#ifndef TIME6_H
#define TIME6_H
class Time {
public:
Time( int = 0, int = 0, int = 0 );
// default constructor
// set functions
Time &setTime( int, int,
Time &setHour( int );
Time &setMinute( int );
Time &setSecond( int );
int );
// set
// set
// set
int getMinute() const;
int getSecond() const;
// return minute
// return second
// set hour, minute, second
hour
minute
Notice the Time & - function
second
returns a reference to a Time
// get functions (normally declared const)
object. Specify object in
int getHour() const;
// return hour
function definition.
// print functions (normally declared const)
void printMilitary() const; // print military time
void printStandard() const; // print standard time
private:
int hour;
// 0 - 23
int minute;
// 0 - 59
int second;
// 0 - 59
};

2000 Prentice Hall, Inc. All rights reserved.
#endif
1. Class definition
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Fig. 7.8: time.cpp
// Member function definitions for Time class.
#include <iostream>
#include "time6.h"
// Constructor function to initialize private data.
// Calls member function setTime to set variables.
// Default values are 0 (see class definition).
Time::Time( int hr, int min, int sec )
{ setTime( hr, min, sec ); }
// Set the values of hour, minute, and second.
Time &Time::setTime( int h, int m, int s )
Returning *this enables
{
cascading function calls
setHour( h );
setMinute( m );
setSecond( s );
return *this;
// enables cascading
}
// Set the hour value
Time &Time::setHour( int h )
{
hour = ( h >= 0 && h < 24 ) ? h : 0;
}
Outline
1. Load header file
using std::cout;
return *this;
33
// enables cascading
 2000 Prentice Hall, Inc. All rights reserved.
1.1 Function
definitions
65 // Set the minute value
66 Time &Time::setMinute( int m )
67 {
68
minute = ( m >= 0 && m < 60 ) ? m : 0;
69
70
return *this;
// enables cascading
71 }
72
73 // Set the second value
74 Time &Time::setSecond( int s )
75 {
76
second = ( s >= 0 && s < 60 ) ? s : 0;
77
78
return *this;
// enables cascading
79 }
80
81 // Get the hour value
82
83
84
85
86
87
int Time::getHour() const { return hour; }
88
89
90
91
92
93
94
int Time::getSecond() const { return second; }
// Get the minute value
int Time::getMinute() const { return minute; }
// Get the second value
// Display military format time: HH:MM
void Time::printMilitary() const
{
cout << ( hour < 10 ? "0" : "" ) << hour << ":"
 2000 Prentice
Inc. All<rights
<< (Hall,
minute
10 reserved.
? "0" : "" ) << minute;
34
Outline
1.1 Function
definitions
Returning *this enables
cascading function calls
95 }
96
97 // Display standard format time: HH:MM:SS AM (or PM)
98 void Time::printStandard() const
1.1 Function
99 {
100
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
definitions
101
<< ":" << ( minute < 10 ? "0" : "" ) << minute
-------------------------102
<< ":" << ( second < 10 ? "0" : "" ) << second
1. Load header
103
<< ( hour < 12 ? " AM" : " PM" );
104 }
printStandard does
105 // Fig. 7.8: fig07_08.cpp
1.1 Initialize Time
106 // Cascading member function calls together not return a reference to
object
an object.
107 // with the this pointer
108 #include <iostream>
109
2. Function calls
110 using std::cout;
111 using std::endl;
112
3. Print
values
Notice cascading function
calls.
113 #include "time6.h"
114
115 int main()
Cascading function calls. printStandard must be
116 {
117
Time t;
called after setTime because printStandard does
118
not return a reference to an object.
119
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
t.printStandard().setTime(); would cause
120
cout << "Military time: ";
121
t.printMilitary();
an error.
122
cout << "\nStandard time: ";
123
t.printStandard();
124
125
cout << "\n\nNew standard time: ";

2000
Prentice Hall,20,
Inc. 20,
All rights
126
t.setTime(
20 reserved.
).printStandard();
Outline
35
127
cout << endl;
Outline
128
129
36
return 0;
130 }
Military time: 18:30
Standard time: 6:30:22 PM
New standard time: 8:20:20 PM
 2000 Prentice Hall, Inc. All rights reserved.
Program Output
37
7.6
Dynamic Memory Allocation with
Operators new and delete
• new and delete
– Used for dynamic memory allocation
• Superior to C’s malloc and free
– new
• Creates an object of the proper size, calls its constructor and returns a
pointer of the correct type
– delete
• Destroys object and frees space
– Examples of new
TypeName *typeNamePtr;
• Creates pointer to a TypeName object
typeNamePtr = new TypeName;
• new creates TypeName object, returns pointer (which
typeNamePtr is set equal to)
 2000 Prentice Hall, Inc. All rights reserved.
38
7.6
Dynamic Memory Allocation with
Operators new and delete
– Examples of delete
delete typeNamePtr;
• Calls destructor for TypeName object and frees memory
Delete [] arrayPtr;
• Used to dynamically delete an array
• Initializing objects
double *thingPtr = new double( 3.14159 );
– Initializes object of type double to 3.14159
int *arrayPtr = new int[ 10 ];
– Creates a ten element int array and assigns it to arrayPtr
 2000 Prentice Hall, Inc. All rights reserved.
39
7.7
static Class Members
• static class members
– Shared by all objects of a class
• Normally, each object gets its own copy of each variable
– Efficient when a single copy of data is enough
• Only the static variable has to be updated
– May seem like global variables, but have class scope
• only accessible to objects of same class
–
–
–
–
Initialized at file scope
Exist even if no instances (objects) of the class exist
Both variables and functions can be static
Can be public, private or protected
 2000 Prentice Hall, Inc. All rights reserved.
40
7.7
static Class Members
• static variables
– Static variables are accessible through any object of the class
– public static variables
• Can also be accessed using scope resolution operator(::)
Employee::count
– private static variables
• When no class member objects exist, can only be accessed via a
public static member function
– To call a public static member function combine the
class name, the :: operator and the function name
Employee::getCount()
 2000 Prentice Hall, Inc. All rights reserved.
41
7.7
static Class Members
• Static functions
– static member functions cannot access non-static data
or functions
– There is no this pointer for static functions, they exist
independent of objects
 2000 Prentice Hall, Inc. All rights reserved.
1
// Fig. 7.9: employ1.h
2
// An employee class
3
#ifndef EMPLOY1_H
4
#define EMPLOY1_H
1. Class definition
6
class Employee {
7
public:
1.1 Function
prototypes
42
Outline
5
8
Employee( const char*, const char* );
// constructor
9
~Employee();
// destructor
10
const char *getFirstName() const;
// return first name
11
const char *getLastName() const;
// return last name
12
13
// static member function
14
static int getCount();
// return # objects instantiated
15
static member
function and variable
declared.
16 private:
17
char *firstName;
18
char *lastName;
19
20
// static data member
21
static int count;
// number of objects instantiated
22 };
23
24 
#endif
2000 Prentice Hall, Inc. All rights reserved.
1.2 Declare variables
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Fig. 7.9: employ1.cpp
// Member function definitions for class Employee
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
#include <cassert>
#include "employ1.h"
43
Outline
1. Load header file
static data member count
and function getCount( )
1.1 Initialize static
initialized at file scope (required).
data members
1.2 Function
definitions
// Initialize the static data member
int Employee::count = 0;
// Define the static member function that
// returns the number of employee objects instantiated.
int Employee::getCount() { return count; }
// Constructor dynamically allocates space for the
Note the use of assert to test for memory
// first and last name and uses strcpy to copy allocation.
// the first and last names into the object
Employee::Employee( const char *first, const char *last )
{
firstName = new char[ strlen( first ) + 1 ];
assert( firstName != 0 );
// ensure memory allocated
strcpy( firstName, first );
static data member count changed
when a constructor/destructor called.
lastName = new char[ strlen( last ) + 1 ];
assert( lastName != 0 );
// ensure memory allocated
strcpy( lastName, last );
 2000
Prentice Hall,
All rights reserved.
++count;
//Inc.
increment
static count of employees
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}
44
Outline
1.2 Function
// Destructor deallocates dynamically allocated memory
definitions
Employee::~Employee()
{
static data member count changed
cout << "~Employee() called for " << firstName when a constructor/destructor called.
<< ' ' << lastName << endl;
delete [] firstName; // recapture memory
delete [] lastName;
// recapture memory
--count; // decrement static count of employees
}
// Return first name of employee
const char *Employee::getFirstName() const
{
// Const before return type prevents client from modifying
// private data. Client should copy returned string before
// destructor deletes storage to prevent undefined pointer.
return firstName;
}
// Return last name of employee
const char *Employee::getLastName() const
{
Count
decremented
// Const before return type prevents client from
modifying
becausebefore
of destructor
// private data. Client should copy returned string
calls from
delete.
// destructor deletes storage to prevent undefined
pointer.
return lastName;

} 2000 Prentice Hall, Inc. All rights reserved.
88 // Fig. 7.9: fig07_09.cpp
89 // Driver to test the employee class
90 #include <iostream>
If no Employee objects exist
91
1. Initialize objects
92 usingcount
std::cout;
incremented
getCount must be accessed
93 usingbecause
std::endl;
of constructor
using the class name and (::).
94
calls from new.
2. Function calls
95 #include "employ1.h"
96
Number of employees before instantiation is 0
97 int main()
3. Print data
98 {
99
cout << "Number of employees before instantiation
is "
e2Ptr->getCount()
or
100
<< Employee::getCount() << endl; Employee::getCount()
// use class name
would also work.
101
102
Employee *e1Ptr = new Employee( "Susan", "Baker" );
103
Employee *e2Ptr = new Employee( "Robert",
"Jones"
);
Number
of employees
after instantiation is 2
104
105
cout << "Number of employees after instantiation is "
106
<< e1Ptr->getCount();
Employee constructor for Susan Baker called.
107
Employee constructor for Robert Jones called.
108
cout << "\n\nEmployee 1: "
109
<< e1Ptr->getFirstName()
110
<< " " << e1Ptr->getLastName()
Employee 1: Susan Baker
111
<< "\nEmployee 2: "
Employee 2: Robert Jones
112
<< e2Ptr->getFirstName()
113
<< " " << e2Ptr->getLastName() << "\n\n";
114
~Employee() called for Susan Baker
115
delete e1Ptr;
// recapture memory
116
e1Ptr = 0;
~Employee() called for Robert Jones
117
delete e2Ptr;
// recapture memory
Prentice
Hall, Inc. All rights reserved.
118 2000
e2Ptr
= 0;
Outline
45
119
120
121
46
cout << "Number of employees after deletion is "
Outline
<< Employee::getCount() << endl;
122
123
return 0;
124 }
count back to zero.
Number of employees before instantiation is 0
Employee constructor for Susan Baker called.
Employee constructor for Robert Jones called.
Number of employees after instantiation is 2
Employee 1: Susan Baker
Employee 2: Robert Jones
~Employee() called for Susan Baker
~Employee() called for Robert Jones
Number of employees after deletion is 0
 2000 Prentice Hall, Inc. All rights reserved.
Program Output
47
7.8
Data Abstraction and Information
Hiding
• Information hiding
– Classes hide implementation details from clients
– Example: stack data structure
• Data elements added (pushed) onto the bottom and removed
(popped) from top
• Last-in, first-out (LIFO) data structure
• Client does not care how stack is implemented, only wants
LIFO data structure
• Abstract data types (ADTs)
– Model real world objects
• int, float are models for a numbers
• C++ is an extensible language
– Standard data types cannot be changed, but new data types
can be created
 2000 Prentice Hall, Inc. All rights reserved.
48
7.8.1 Example: Array Abstract Data Type
• Programmer can make an ADT array
– Could include
•
•
•
•
•
•
•
Subscript range checking
An arbitrary range of subscripts instead of having to start with 0
Array assignment
Array comparison
Array input/output
Arrays that know their sizes
Arrays that expand dynamically to accommodate more elements
 2000 Prentice Hall, Inc. All rights reserved.
49
7.8.2 Example: String Abstract Data Type
• Strings in C++
– C++ does not provide a built in string data type
• Maximizes performance
– Provides mechanisms for creating and implementing a string
abstract data type
– string class available in ANSI/ISO standard (Chapter 19)
 2000 Prentice Hall, Inc. All rights reserved.
50
7.8.3 Example: Queue Abstract Data Type
• Queue
– Like waiting in line
• FIFO — First in, first out
– Enqueue
• Put items in a queue one at a time, from the back
– Dequeue
• Remove items from a queue one at a time, from the front
– Implementation hidden from clients
• Queue ADT
– Clients may not manipulate data structure directly
– Only queue member functions can access internal data
 2000 Prentice Hall, Inc. All rights reserved.
51
7.9
Container Classes and Iterators
• Container classes (collection classes)
– Classes designed to hold collections of objects
– Provide services such as insertion, deletion, searching,
sorting, or testing an item
– Examples:
Arrays, stacks, queues, trees and linked lists
• Iterator objects (iterators)
– Object that returns the next item of a collection (or performs
some action on the next item)
– Can have several iterators per container
• Book with multiple bookmarks
– Each iterator maintains its own “position” information
– Discussed further in chapter 20
 2000 Prentice Hall, Inc. All rights reserved.
52
7.10 Proxy Classes
• Proxy class
– Used to hide implementation details of a class
– Class that knows only the public interface of the class being
hidden
– Enables clients to use class’s services without giving access to
class’s implementation
• Forward class declaration
–
–
–
–
Used when class definition only uses a pointer to another class
Prevents the need for including the header file
Declares a class before it is referenced
Format:
class ClassToLoad;
 2000 Prentice Hall, Inc. All rights reserved.
1
// Fig. 7.10: implementation.h
2
// Header file for class Implementation
53
Outline
3
4
class Implementation {
5
public:
6
Implementation( int v ) { value = v; }
7
void setValue( int v ) { value = v; }
8
int getValue() const { return value; }
Implementation has
private data we want to hide.
9
10
11
private:
int value;
Forward class declaration.
12 };
13 // Fig. 7.10: interface.h
14 // Header file for interface.cpp
15 class Implementation;
// forward class declaration
16
17 class Interface {
18
public:
Proxy class Interface has same
public interface as class
Implementation.
19
Interface( int );
20
void setValue( int );
// same public interface as
21
int getValue() const;
// Only
classuses
Implementation
a pointer to class
22
~Interface();
23
24
25
private:
Implementation *ptr;
Implementation. This allows us to
hide the implementation details.
// requires previous
// forward declaration
26 
};2000 Prentice Hall, Inc. All rights reserved.
1. Implementation
class definition
---------------------1. Forward class
declaration
1.1 Interface class
definition
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Fig. 7.10: interface.cpp
// Definition of class Interface
#include "interface.h"
#include "implementation.h"
54
Outline
Interface::Interface( int v )
: ptr ( new Implementation( v ) ) { }
1. Load header files
1.1 Function
definitions
------------------------contains member functions for proxy class
// call Implementation's getValue function
Interface. It is the only file that has 1. Load interface
int Interface::getValue() const { return ptr->getValue(); }
header file implementation.h, whichheader file
Interface::~Interface() { deletecontains
ptr; } class Implementation.
// Fig. 7.10: fig07_10.cpp
1.1 Initialize objects
// Hiding a class’s private data with a proxy class.
// call Implementation's setValue function
void Interface::setValue( int v Implementation
) { ptr->setValue(
v ); }
file interface.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "interface.h"
int main()
{
Interface i( 5 );
interface.cpp is precompiled2.and
given calls
Function
with the header file interface.h. The
client cannot see the interactions between the
3. Output
proxy class and the proprietary class.
Only the header Interface.h — no mention of class
Implementation. The client never sees the
contains:private
" << i.getValue()
data.
cout << "Interface
<< " before setValue" << endl;
i.setValue( 10 );
cout << "Interface contains: " << i.getValue()
<< " after setValue" << endl;
return 0;

} 2000 Prentice Hall, Inc. All rights reserved.
55
Interface contains: 5 before setVal
Interface contains: 10 after setVal
Outline
Program Output
 2000 Prentice Hall, Inc. All rights reserved.