📄 06章 类与数据抽象(一).txt
字号:
4O {
41 cout <<" (global created before main)" << endl;
42
43 CreateAndDestroy second( 2 );
44 cout <<" (local automatic in main)" << endl;
45
46 static CreateAndDestroy third( 3 ); // local object
47 cout <<" (local static in main)" << endl;
48
49 create(); // call function to create objects
50
51 CreateAndDestroy fourth( 4 ); // local object
52 cout <<" (local automatic in main)" << endl;
53 return 0;
54 }
55
56 // Function to create objects
57 void create( void )
58 {
59 CreateAndDestroy fifth( 5 );
60 cout <<" (local automatic in create)" << endl;
61
62 static CreateAndDestroy sixth( 6 );
63 cout <<" (local static in create)" << endl;
64
65 CreateAndDestroy seventh( 7 );
66 cout <<" (local automatic in create)" << endl;
67 }
输出结果:
Object 1 constructor (global creasted before main)
Object 2 constructor (local automatic in main)
Object 3 constructor (local static in main)
Object 5 constructor (local automatic in create)
Object 6 constructor (local static in create)
Object 7 constructor (local automatic in create)
Object 7 destructor
Object 5 destructor
Object 4 constructor (local automatic in main}
Object 4 destructor
Object 2 destructor
Object 6 destructor
Object 3 destructor
Object 1 destructor
图6.9调用构造函数与析构函数的顺序
函数main声明三个对象。对象second和fourth是局部自动对象,对象third是static局部对象。这些对象的构造函数在程序执行到对象定义时调用。对象fourth和second的析构函数在到达main结尾时依次调用。由于对象third是static局部对象,因此到程序结束时才退出,在程序终止时删除所有其他对象之后和调用first的析构函数之前调用对象third的析构函数。
函数create声明三个对象。对象fifth和seventh是局部自动对象,对象sixth是static局部对象。对象seventh和fifth的析构函数在到达删k结尾时依次调用。由于对象sixth是static局部对象,因此到程序结束时才退出。sixth的析构函数在程序终止时删除所有其他对象之后和调用third和first的析构函数之前调用。
6.14 使用数据成员和成员函数
员函数调整客户的银行借贷(例如BanLAccount类的private数据成员)。
类通常提供public成员函数,让类的客户设置(写入)或读取(取得)private数据成员的值。这些函数通常称为get和set。更具体地说,设置数据成员interestRate的成员函数通常称为setInterestRate,读取数据成员IntersetRate的值通常称为getInterestRate。读取函数也称为“查询”函数。
提供get和set函数与指定数据成员为public同样重要,这是C++语言在软件工程中的另一优势。如果数据成员为public,则程序中的任何函数可以随意读取和写入这个数据成员。如果数据成员为private.则public get函数可以让其他函数读取数据,而且数据的显示和格式化也可以用get函数控制。public set函数通常用于检查数据成员的修改,保证新值是适当的数据项目。例如,如果想把一个月的日期号数设置为37会被禁止,将人的身高设置为负值也会被禁止,将数字量设置为字母值也会被拒绝,将一个人的成绩设置为185分(取百分制时)同样也会被拒绝等等。
软件工程视点6. 22
指定private数据成员并通过public成员函数控制这些数据成员的访问(特别是写入访问)可以保证数据的完整性。
测试与调试提示6.5
指定private数据成员并不能自动保证数据完整性,程序员还要提供验证检查。但C++提供了让程序员方便地设计更好的程序的框架。
编程技巧6.7
设置private数据值的成员函数应验证所要新值是否正确,如果不正确,则set数应将Privte数据成员设置为相应的一致状态。
试图要对数据成员指定无效值时,应当提醒类客户。类的set函数常写成返回一个值,表示试图对数据成员指定无效值。这样就使类的客户可以测试set函数的返回值,确定其操作的对象是否为有效对象,并在对象无效时采取相应操作。
图6.1O将Time类扩展成包括private数据成员hour、minute和second的get和set函数。set函数严格控制数据成员的设置。如果想把数据成员设置为无效值,则会把数据成员设置为0(从而使数据成员保持一致状态)。每个get函数只是返回相应数据成员的值。程序首先用set函数设置Time对象t的private数据成员为有效值,接着用get函数读取这个值以便输出。然后set函数要将hour和second成员设置为无效值并将minute成员设置为有效值,并用get函数读取这个值以便输出。输出表明,无效值使得数据成员设置为0。最后,程序将时间设置为11:58:00并用函数incrementMinutes增加3分钟。函数incrementMinutes是个非成员函数,它调用get和set成员函数增加minute成员的值。尽管这样的方法实现了所需的功能,但是多次函数调用降低了程序的性能。下一章将介绍用友元函数消除多次函数调用的性能负担。
常见编程错误6.11
构造函数可以调用类的其他成员函数,如set和get函数,但由于构造函数初始化对象,因此数据成员可能还处于不一致状态。数据成员在初始化之前使用可能造成逻辑错误。
1 // Fig. 6.10: time3.h
2 // Declaration of the Time class.
4
5 // preprocessor directives that
6 // prevent multiple inclusions of header file
7 #ifndef TIME3_H
8 #define TIME3_H
9
10 class Time {
11 public:
12 Time( int = 0, int = 0, int= 0 ); // constructor
13
14 // set functions
15 void setTime( int, int, int ); // set hour, minute, se
16 void setHour( iht ); // set hour
17 void setMinute( int ); // set minute
18 void setSecond( int ); // set second
19
20 // get functions
21 int getHourO; // return hour
22 int getMinute(); // return minute
23 int getSecond(); // return second
24
25 void printMilitary(); // output military time
26 void printStandard(); // output standard time
27
28 private:
29 int hour; // 0 - 23
30 int minute; // 0 - 59
31 int second; // 0 - 59
32 }
33
34 #endif
35 // Fig. 6.10: time3.cpp
36 // Member function defintions for Time class
37 #include "time3.h"
38 #include <iostream.h>
39
40 // Constructor function to initialize private data.
42 // Default values are 0 (see class definition).
43 Time::Time( int hr, int min, int sec )
44 { setTime( hr, min, sec ); }
45
46 // Set the values of hour, minute, and second.
47 void Time::setTime(int h,int m,int s)
48 {
49 setHour( h );
50 setMinute( m );
51 setSecond( s );
52 }
53
54 // Set the hour value
55 void Time::setHour(int h)
56 {hour = (h>0 && h <24 )? h: 0;}
57
58 // Set the minute value
59 void Time::setMinute( int m )
60 { minute = ( m >= 0 && m 60 ) ? m : 0; }
61
62 // Set the second value
63 void Time::setSecond( int s
64 { second = ( s >= 0 && s < 60 ) ? s : 0; }
65
66 // Get the hour value
67 int Time::getHour() { return hour;}
68
69 // Get the minute value
70 int Time::getMinute() { return minute; }
71
72 // Get the second value
73 int Time::getSecond() { return second; }
74
75 // Print time is military format
76 void Time::printMilitary()
77 {
78 cout << ( hour < 10 ? "0" : "" ) << hour << ":"
79 << ( minute < 10 ? "0" : "" ) << minute;
8O }
81
82 // Print time in standard format
83 void Time::printStandard{)
84 {
85 cout << ( { hour == 0 II hour == 12 ) ? 12 : hour % 12 )
86 << ":" << ( minute < 10 ? "0" : "" ) << minute
87 << ":" << ( second < 10 ? "0" : "" ) << second
88 << ( hour < 12 ? "AM" : "PM" );
89 }
90 // Fig. 6.10: fig06_lO.cpp
92 #include <iostream.h>
91 // Demonstrating the Time class ser and get functions
92 #include<iostream.h>
93 #include "time3.h"
94
95 void incrementMinutes( Time &, const iht );
96
97 int main()
98 {
99 Time t;
100
101 t.setHour{ 17 );
102 t.setMinute( 34 );
103 t.setSecond( 25 );
104
105 cout << "Result of setting all valid values:\n;
106 << our: << t.getHour()
107 << " Minute: " << t.getMinute()
108 <<" Second: "<< t.getSecond();
109
110 t.setHour( 234 ); // invalid hour set to 0
111 t.setMinute( 43 );
112 t.setSecond( 6373 ); // invalid second set to 0
113
114 cout << "\n\nResult of attempting to set invalid hour and"
115 << "second:\m Hour: "<< t.getHour()
116 <<" Minute: "<< t.getMinute()
117 <<" Second: "<< t.getSecond() << "\n\n";
118
119 t.setTime( 11, 58, 0 );
120 incrementMinutes( t, 3 );
121
122 return 0;
123 }
124
125 void incrementMinutes(Time &tt, const int count)
126 {
127 cout << "Incrementing minute" << count
128 << "times:\nStart time: ";
129 tt.priatStandard();
130
131 for (int i = 0; i < count; i++ ) {
132 tt.setMinute( (tt.getMinute() + 1 ) % 60);
133
134 if (tt.getMinute() == 0 )
135 tt.setHour( ( tt.getHour() + 1 ) % 24);
136
137 cout << "\nminute + 1: ";
138 tt.printStandard();
139 }
140
141 cout << endl;
142 }
输出结果:
Result of setting all valid values:
Hour: 17 Minute: 34 Second: 25
Result of attempting to set inv]id hour and second:
Hour: 0 Minute: 43 Second: 0
Incrementing minute 3 times:
Start time: 11:58:00 AM
mi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -