Classes and Objects
Classes and Objects
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
// default constructor
// 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
// print functions (normally declared const)
void printMilitary() const; // print military time
void printStandard();
// print standard time
private:
int hour;
// 0 - 23
int minute;
// 0 - 59
int second;
// 0 - 59
};
#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
#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 )
{ second = ( s >= 0 && s < 60 ) ? s : 0; }
64
65 // Get the hour value
66 int Time::getHour() const { return hour; }
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; }
73
74 // Display military format time: HH:MM
75 void Time::printMilitary() const
76 {
77
78
79 }
80
81 // Display standard format time: HH:MM:SS AM (or PM)
82 void Time::printStandard()
// should be const
83 {
84
85
86
87
88 }
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
34
35
36
37
value.print();
38
39
40
value.addIncrement();
41
42
value.print();
43
44
45
return 0;
46 }
= 10, increment
15, increment =
20, increment =
25, increment =
= 5
5
5
5
Construction of objects
Member objects constructed in order declared
Not in order of constructors member initializer list
#ifndef DATE1_H
#define DATE1_H
6
7
class Date {
public:
10
11
~Date();
12 private:
13
int month;
// 1-12
14
int day;
15
int year;
// any year
16
17
18
19 };
20
21 #endif
37
38
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 );
45
46
47
print();
48
49 }
50
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
95
96
~Employee();
97 private:
98
char firstName[ 25 ];
99
char lastName[ 25 ];
100
101
102 };
103
104 #endif
Composition - including
objects of other classes.
137
138 void Employee::print() const
139 {
140
cout << lastName << ", " << firstName << "\nHired: ";
141
hireDate.print();
142
143
birthDate.print();
144
145 }
146
Destructor will
print a line when
called.
163 {
164
165
166
167
e.print();
168
169
170
171
172
return 0;
173 }
Program Output
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)
1
2
3
4
5
6
7
using std::cout;
using std::endl;
};
// Can modify private data of Count because
// setX is declared as a friend function of Count
void setX( Count &c, int val )
21 {
22
23 }
24
c.x = val;
25 int main()
26 {
27
Count counter;
28
29
30
31
32
setX( counter, 8 );
33
counter.print();
34
return 0;
35 }
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
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)
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
32
// default constructor
// constructor
x = 12
this->x = 12
(*this).x = 12
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
int );
// set
// set
// set
// default constructor
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
// enables cascading
// enables cascading
72
73 // Set the second value
74 Time &Time::setSecond( int s )
75 {
76
77
78
79 }
// enables cascading
80
81 // Get the hour value
82 int Time::getHour() const { return hour; }
83
84 // Get the minute value
85 int Time::getMinute() const { return minute; }
86
87 // Get the second value
88 int Time::getSecond() const { return second; }
89
90 // Display military format time: HH:MM
91 void Time::printMilitary() const
92 {
93
94
cout << ( hour < 10 ? "0" : "" ) << hour << ":"
<< ( minute < 10 ? "0" : "" ) << minute;
95 }
96
97 // Display standard format time: HH:MM:SS AM (or PM)
98 void Time::printStandard() const
99 {
100
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
101
<< ":" << ( minute < 10 ? "0" : "" ) << minute
102
<< ":" << ( second < 10 ? "0" : "" ) << second
103
<< ( hour < 12 ? " AM" : " PM" );
104 }
105 // Fig. 7.8: fig07_08.cpp
106 // Cascading member function calls together
107 // with the this pointer
108 #include <iostream>
109
110 using std::cout;
111 using std::endl;
112
Notice cascading
113 #include "time6.h"
114
115 int main()
116 {
117
Time t;
118
119
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
120
cout << "Military time: ";
121
t.printMilitary();
122
cout << "\nStandard time: ";
123
t.printStandard();
124
125
cout << "\n\nNew standard time: ";
126
t.setTime( 20, 20, 20 ).printStandard();
function calls.
127
128
129
return 0;
130 }
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;
Initializing objects
double *thingPtr = new double( 3.14159 );
// constructor
~Employee();
// destructor
10
11
12
13
14
15
16 private:
17
char *firstName;
18
char *lastName;
19
20
21
22 };
23
24 #endif
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
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
119
120
121
122
123
return 0;
124 }
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 classs services without giving access
to classs implementation
public:
9
10
11
private:
int value;
12 };
13 // Fig. 7.10: interface.h
14 // Header file for interface.cpp
15 class Implementation;
16
17 class Interface {
18
public:
19
Interface( int );
20
21
// class Implementation
22
~Interface();
23
24
25
26 };
private:
Implementation *ptr;
// requires previous
// forward declaration
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
Program Output