?? 06章 類與數(shù)據(jù)抽象(一).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 }
輸出結(jié)果:
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調(diào)用構(gòu)造函數(shù)與析構(gòu)函數(shù)的順序
函數(shù)main聲明三個對象。對象second和fourth是局部自動對象,對象third是static局部對象。這些對象的構(gòu)造函數(shù)在程序執(zhí)行到對象定義時調(diào)用。對象fourth和second的析構(gòu)函數(shù)在到達main結(jié)尾時依次調(diào)用。由于對象third是static局部對象,因此到程序結(jié)束時才退出,在程序終止時刪除所有其他對象之后和調(diào)用first的析構(gòu)函數(shù)之前調(diào)用對象third的析構(gòu)函數(shù)。
函數(shù)create聲明三個對象。對象fifth和seventh是局部自動對象,對象sixth是static局部對象。對象seventh和fifth的析構(gòu)函數(shù)在到達刪k結(jié)尾時依次調(diào)用。由于對象sixth是static局部對象,因此到程序結(jié)束時才退出。sixth的析構(gòu)函數(shù)在程序終止時刪除所有其他對象之后和調(diào)用third和first的析構(gòu)函數(shù)之前調(diào)用。
6.14 使用數(shù)據(jù)成員和成員函數(shù)
員函數(shù)調(diào)整客戶的銀行借貸(例如BanLAccount類的private數(shù)據(jù)成員)。
類通常提供public成員函數(shù),讓類的客戶設(shè)置(寫入)或讀取(取得)private數(shù)據(jù)成員的值。這些函數(shù)通常稱為get和set。更具體地說,設(shè)置數(shù)據(jù)成員interestRate的成員函數(shù)通常稱為setInterestRate,讀取數(shù)據(jù)成員IntersetRate的值通常稱為getInterestRate。讀取函數(shù)也稱為“查詢”函數(shù)。
提供get和set函數(shù)與指定數(shù)據(jù)成員為public同樣重要,這是C++語言在軟件工程中的另一優(yōu)勢。如果數(shù)據(jù)成員為public,則程序中的任何函數(shù)可以隨意讀取和寫入這個數(shù)據(jù)成員。如果數(shù)據(jù)成員為private.則public get函數(shù)可以讓其他函數(shù)讀取數(shù)據(jù),而且數(shù)據(jù)的顯示和格式化也可以用get函數(shù)控制。public set函數(shù)通常用于檢查數(shù)據(jù)成員的修改,保證新值是適當?shù)臄?shù)據(jù)項目。例如,如果想把一個月的日期號數(shù)設(shè)置為37會被禁止,將人的身高設(shè)置為負值也會被禁止,將數(shù)字量設(shè)置為字母值也會被拒絕,將一個人的成績設(shè)置為185分(取百分制時)同樣也會被拒絕等等。
軟件工程視點6. 22
指定private數(shù)據(jù)成員并通過public成員函數(shù)控制這些數(shù)據(jù)成員的訪問(特別是寫入訪問)可以保證數(shù)據(jù)的完整性。
測試與調(diào)試提示6.5
指定private數(shù)據(jù)成員并不能自動保證數(shù)據(jù)完整性,程序員還要提供驗證檢查。但C++提供了讓程序員方便地設(shè)計更好的程序的框架。
編程技巧6.7
設(shè)置private數(shù)據(jù)值的成員函數(shù)應(yīng)驗證所要新值是否正確,如果不正確,則set數(shù)應(yīng)將Privte數(shù)據(jù)成員設(shè)置為相應(yīng)的一致狀態(tài)。
試圖要對數(shù)據(jù)成員指定無效值時,應(yīng)當提醒類客戶。類的set函數(shù)常寫成返回一個值,表示試圖對數(shù)據(jù)成員指定無效值。這樣就使類的客戶可以測試set函數(shù)的返回值,確定其操作的對象是否為有效對象,并在對象無效時采取相應(yīng)操作。
圖6.1O將Time類擴展成包括private數(shù)據(jù)成員hour、minute和second的get和set函數(shù)。set函數(shù)嚴格控制數(shù)據(jù)成員的設(shè)置。如果想把數(shù)據(jù)成員設(shè)置為無效值,則會把數(shù)據(jù)成員設(shè)置為0(從而使數(shù)據(jù)成員保持一致狀態(tài))。每個get函數(shù)只是返回相應(yīng)數(shù)據(jù)成員的值。程序首先用set函數(shù)設(shè)置Time對象t的private數(shù)據(jù)成員為有效值,接著用get函數(shù)讀取這個值以便輸出。然后set函數(shù)要將hour和second成員設(shè)置為無效值并將minute成員設(shè)置為有效值,并用get函數(shù)讀取這個值以便輸出。輸出表明,無效值使得數(shù)據(jù)成員設(shè)置為0。最后,程序?qū)r間設(shè)置為11:58:00并用函數(shù)incrementMinutes增加3分鐘。函數(shù)incrementMinutes是個非成員函數(shù),它調(diào)用get和set成員函數(shù)增加minute成員的值。盡管這樣的方法實現(xiàn)了所需的功能,但是多次函數(shù)調(diào)用降低了程序的性能。下一章將介紹用友元函數(shù)消除多次函數(shù)調(diào)用的性能負擔。
常見編程錯誤6.11
構(gòu)造函數(shù)可以調(diào)用類的其他成員函數(shù),如set和get函數(shù),但由于構(gòu)造函數(shù)初始化對象,因此數(shù)據(jù)成員可能還處于不一致狀態(tài)。數(shù)據(jù)成員在初始化之前使用可能造成邏輯錯誤。
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 }
輸出結(jié)果:
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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -