Академический Документы
Профессиональный Документы
Культура Документы
Data Abstraction
Outline
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14
6.15
6.16
6.17
Introduction
Structure Definitions
Accessing Structure Members
Implementing a User-Defined Type Time with a struct
Implementing a Time Abstract Data Type with a class
Class Scope and Accessing Class Members
Separating Interface from Implementation
Controlling Access to Members
Access Functions and Utility Functions
Initializing Class Objects: Constructors
Using Default Arguments with Constructors
Destructors
When Constructors and Destructors Are Called
Using Set and Get Functions
Subtle Trap: Returning a Reference to a
private Data Member
Default Memberwise Assignment
Software Reusability
6.1 Introduction
Object-oriented programming (OOP)
Encapsulates data (attributes) and functions (behavior) into
packages called classes
Information hiding
Class objects communicate across well-defined interfaces
Implementation details hidden within classes themselves
Structure tag
Structure members
struct definition
Creates new data type used to declare variables
Structure variables declared like variables of other types
Examples:
Time
Time
Time
Time
timeObject;
timeArray[ 10 ];
*timePtr;
&timeRef = timeObject;
Parentheses required
* lower precedence than .
C-style structures
No interface
If implementation changes, all programs using that struct
must change accordingly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
fig06_01.cpp
(1 of 3)
using std::cout;
using std::endl;
#include <iomanip>
using std::setfill;
using std::setw;
// structure definition
struct Time {
int hour;
// 0-23 (24-hour clock format)
int minute;
// 0-59
int second;
// 0-59
}; // end struct Time
void printUniversal( const Time & );
void printStandard( const Time & );
// prototype
// prototype
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int main()
{
Time dinnerTime;
dinnerTime.hour = 18;
dinnerTime.minute = 30;
dinnerTime.second = 0;
fig06_01.cpp
(2 of 3)
Outline
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Outline
fig06_01.cpp
(3 of 3)
Use parameterizedfig06_01.cpp
stream
manipulator setfill.
output (1 of 1)
to access
private:
Accessible only to member functions of class
protected:
2003 Prentice Hall, Inc. All rights reserved.
10
No return type
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Outline
class Time {
Class Time
definition
(1 of 1)
class, private
Time, anddata
no return
members
type. accessible only to member
// 0 - 23 (24-hour
clock format)
functions.
Class
body
ends
with
terminatesright
with
// Definition
0 - 59
brace.
//
0 - 59
semicolon.
1
2
Example:
Time
Time
Time
Time
sunset;
arrayOfTimes[ 5 ];
*pointerToTime;
&dinnerTime = sunset;
//
//
//
//
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
fig06_03.cpp
(1 of 5)
using std::cout;
using std::endl;
#include <iomanip>
using std::setfill;
using std::setw;
1
5
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private:
int hour;
int minute;
int second;
Outline
fig06_03.cpp
(2 of 5)
members.
1
6
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Outline
fig06_03.cpp
(3 of 5)
No arguments (implicitly
know purpose is to print
data members); member
function calls more concise.
t to be
object of class Time.
1
7
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Outline
fig06_03.cpp
(4 of 5)
// change time
1
8
93
94
95
96
97
98
99
Outline
fig06_03.cpp
(5 of 5)
return 0;
} // end main
fig06_03.cpp
output (1 of 1)
1
9
No arguments
Cannot be overloaded
Performs termination housekeeping
20
Software reuse
Composition (aggregation)
Class objects included as members of other classes
Inheritance
New classes derived from old
21
File scope
Nonmember functions
22
23
Pointers
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
fig06_04.cpp
(1 of 2)
using std::cout;
using std::endl;
// class Count definition
class Count {
public:
int x;
void print()
{
cout << x << endl;
}
}; // end class Count
2
5
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Outline
int main()
{
Count counter;
// create counter object
Count *counterPtr = &counter; // create pointer to counter
Count &counterRef = counter; // Use
create
reference
to counter
dot member
selection
fig06_04.cpp
(2 of 2)
cout << "Assign 1 to x and print using the object's name: ";
counter.x = 1;
// assign 1 to data member x
dot member
counter.print();
// call memberUse
function
print selection
fig06_04.cpp
output (1 of 1)
selection
operator for counterPtr
pointer
to object.";
using
a pointer:
2
6
Disadvantage
Header files
Portions of implementation
Inline member functions
Hints about other implementation
private members
Can hide more with proxy class
27
File extension .h
Source-code files
Member function definitions
Same base name
Convention
28
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
Outline
code to prevent
multiple inclusions.
time1.h (1 of 1)
public:
Time();
// constructor
void setTime( int, int, int ); // set hour, minute, second
void printUniversal();
// print universal-time format
void printStandard();
// print standard-time format
private:
int hour;
int minute;
int second;
2
9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
time1.cpp (1 of 3)
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
Library.
3
0
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Outline
time1.cpp (2 of 3)
3
1
42
43
44
45
46
47
48
49
50
Outline
time1.cpp (3 of 3)
3
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
fig06_07.cpp
(1 of 2)
// change time
3
3
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Outline
fig06_07.cpp
(2 of 2)
fig06_07.cpp
output (1 of 1)
return 0;
} // end main
3
4
public
Accessible to any function in program with handle to class
object
protected
Chapter 9
35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
fig06_08.cpp
(1 of 1)
return 0;
} // end main
3
6
Outline
fig06_08.cpp
output (1 of 1)
Errors produced by attempting
to access private members.
3
7
Get function
Read private data
Set function
Modify private data
2003 Prentice Hall, Inc. All rights reserved.
38
private
Support operation of public member functions
Not intended for direct client use
39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
salesp.h (1 of 1)
public:
SalesPerson();
//
void getSalesFromUser();
// input sales from keyboard
void setSales( int, double ); // set sales for a month
private utility
void printAnnualSales();
// summarize and print sales
function.
private:
double totalAnnualSales();
double sales[ 12 ];
// utility function
// 12 monthly sales figures
4
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
salesp.cpp (1 of 3)
std::cout;
std::cin;
std::endl;
std::fixed;
#include <iomanip>
using std::setprecision;
// include SalesPerson class definition from salesp.h
#include "salesp.h"
// initialize elements of array sales to 0.0
SalesPerson::SalesPerson()
{
for ( int i = 0; i < 12; i++ )
sales[ i ] = 0.0;
} // end SalesPerson constructor
4
1
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Outline
salesp.cpp (2 of 3)
4
2
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Outline
salesp.cpp (3 of 3)
return total;
} // end function totalAnnualSales
4
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Outline
fig06_11.cpp
(1 of 1)
return 0;
} // end main
4
4
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
Outline
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
fig06_11.cpp
output (1 of 1)
4
5
Initializers
Passed as arguments to constructor
In parentheses to right of class name before semicolon
46
47
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
Outline
time2.h (1 of 1)
public:
Time( int = 0, int = 0, int = 0); // default constructor
void setTime( int, int, int ); // set hour, minute, second
void printUniversal();
// print universal-time format
void printStandard();
// print standard-time format
private:
int hour;
int minute;
int second;
4
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
time2.cpp (1 of 3)
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
// include definition of class Time from time2.h
#include "time2.h"
// Time constructor initializes each data member to zero;
// ensures all Time objects start in a consistent state
Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec ); // validate and set time
4
9
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Outline
time2.cpp (2 of 3)
5
0
42
43
44
45
46
47
48
49
50
Outline
time2.cpp (3 of 3)
5
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
fig06_14.cpp
(1 of 2)
using std::cout;
using std::endl;
// include definition of class Time from time2.h
#include "time2.h"
int main()
{
Time t1;
Time t2(
Time t3(
Time t4(
Time t5(
//
2 );
//
21, 34 );
//
12, 25, 42 ); //
27, 74, 99 ); //
Initialize Time
objects using
default arguments.
5
2
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
";
";
";
Outline
fig06_14.cpp
(2 of 2)
";
return 0;
} // end main
5
3
Outline
Constructed with:
all default arguments:
00:00:00
12:00:00 AM
fig06_14.cpp
output (1 of 1)
5
4
6.12 Destructors
Destructors
Special member function
Same name as class
Preceded with tilde (~)
No arguments
No return value
Cannot be overloaded
Performs termination housekeeping
Before system reclaims objects memory
Reuse memory for new objects
No explicit destructor
55
56
Constructors
When objects defined
Each time execution enters scope
Destructors
When objects leave scope
Execution exits block in which object defined
Not called if program ends with exit or abort
2003 Prentice Hall, Inc. All rights reserved.
57
Constructors
Exactly once
When execution reaches point where object defined
Destructors
When main terminates or exit function called
Not called if program ends with abort
58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Outline
create.h (1 of 1)
Constructor and destructor
member functions.
to show
order of constructor,
destructor function calls.
5
9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
create.cpp (1 of 2)
using std::cout;
using std::endl;
// include CreateAndDestroy class definition from create.h
#include "create.h"
// constructor
CreateAndDestroy::CreateAndDestroy(
int objectNumber, char *messagePtr )
{
objectID = objectNumber;
message = messagePtr;
cout << "Object " << objectID << "
<< message << endl;
Output message to
demonstrate timing of
constructor function calls.
constructor runs
"
6
0
23
24
25
26
27
28
29
30
31
32
Outline
// destructor
CreateAndDestroy::~CreateAndDestroy()
{
Output message to
// the following line is for pedagogic purposes only
cout << ( objectID == 1 || objectIDdemonstrate
== 6 ? "\n"timing
: "" of
);
create.cpp (2 of 2)
destructor runs
"
6
1
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
Outline
fig06_17.cpp
(1 of 3)
using std::cout;
using std::endl;
// include CreateAndDestroy class definition from create.h
#include "create.h"
void create( void );
// prototype
// global object
CreateAndDestroy first( 1, "(global before main)" );
int main()
Create local automatic
{
cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
object.
6
2
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
create();
Outline
fig06_17.cpp
(2 of 3)
} // end main
// function to create objects
void create( void )
Create local automatic object
{
in function.
cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestroy fifth(
);
CreateAndDestroy seventh(
7, "(local automatic in create)" );
6
3
51
52
53
Outline
fig06_17.cpp
(3 of 3)
6
4
Object 1
constructor runs
Outline
(local
(local
(local
(local
Object 1
destructor runs
automatic
automatic
static in
static in
fig06_17.cpp
output (1 of 1)
Local
static
object exists
Destructors
for local
Global
object
constructed
until
program
termination.
automatic
objects
in main
before
main
execution
and
Local
automatic
objects
called
in
reverse
order
of
Local
static
object
last. function
destroyed
after
constructors.
constructed
on first
function
execution ends
in reverse
call
after main
orderand
of destroyed
construction.
execution ends.
in main)
in main)
create)
main)
6
5
Get functions
Query functions
Control format of data returned
66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
time3.h (1 of 2)
// default constructor
// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int );
// set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
// get functions
int getHour();
int getMinute();
int getSecond();
Set functions.
Get functions.
// return hour
// return minute
// return second
6
7
25
26
27
28
29
30
31
32
33
34
35
Outline
time3.h (2 of 2)
6
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
time3.cpp (1 of 4)
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
// include definition of class Time from time3.h
#include "time3.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 );
} // end Time constructor
6
9
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Outline
time3.cpp (2 of 4)
7
0
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Outline
time3.cpp (3 of 4)
7
1
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Outline
time3.cpp (4 of 4)
Get function allows client to
read data.
7
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
fig06_20.cpp
(1 of 3)
using std::cout;
using std::endl;
// include definition of class Time from time3.h
#include "time3.h"
void incrementMinutes( Time &, const int );
int main()
{
Time t;
// prototype
7
3
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Outline
(2 of 3)
using
// set time
// increment t's minute by 3
return 0;
} // end main
7
4
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Outline
fig06_20.cpp
Using get functions
to 3)
read
(3 of
data and set functions to
modify data.
if ( tt.getMinute() == 0 )
tt.setHour( ( tt.getHour() + 1 ) % 24);
cout << "\nminute + 1: ";
tt.printStandard();
} // end for
cout << endl;
} // end function incrementMinutes
7
5
Outline
fig06_20.cpp
output (1 of 1)
7
6
Returning references
public member functions can return non-const
references to private data members
77
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
Outline
time4.h (1 of 1)
Function to demonstrate
effects of returning reference
to private data member.
private:
int hour;
int minute;
int second;
}; // end class Time
#endif
7
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
time4.cpp (1 of 2)
7
9
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Outline
time4.cpp (2 of 2)
8
0
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
Outline
fig06_23.cpp
(1 of 2)
using std::cout;
using std::endl;
// include definition of class Time from time4.h
#include "time4.h"
int main()
{
Time t;
// store in hourRef the reference returned by
int &hourRef = t.badSetHour( 20 );
badSetHour returns
reference to private data
member hour.
badSetHour
Reference
allows setting
cout << "Hour before modification:
" << hourRef;
// use hourRef to set invalid
hourRef = 30;
of
8
1
26
27
28
29
30
31
32
33
34
35
36
37
38
Outline
fig06_23.cpp
(2 of 2)
"\n\n*********************************\n"
Can use function call as
"POOR PROGRAMMING PRACTICE!!!!!!!!\n"
lvalue "to set invalid value.
"badSetHour as an lvalue, Hour:
t.getHour()
"\n*********************************" << endl;
fig06_23.cpp
output (1 of 1)
return 0;
} // end main
8
2
83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
fig06_24.cpp
(1 of 3)
8
4
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Outline
fig06_24.cpp
(2 of 3)
8
5
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// default
Outline
Default memberwise
assignment assigns each
member of date1
memberwise
assignment
individually
to each member
of date2.
fig06_24.cpp
(3 of 3)
fig06_24.cpp
output (1 of 1)
date1 = 7-4-2002
date2 = 1-1-1990
After default memberwise assignment, date2 = 7-4-2002
8
6
6.17
Software Reusability
Software reusability
Class libraries
Well-defined
Carefully tested
Well-documented
Portable
Widely available
Resulting problems
Cataloging schemes
Licensing schemes
Protection mechanisms
2003 Prentice Hall, Inc. All rights reserved.
87