1 / 66

Chapter 7: 再論類別

Chapter 7: 再論類別. 目標 能夠動態地建構和解構物件 能夠指定 const ( 常數 ) 物件和和 const 成員函式 了解夥伴( friend ) 函式與夥伴類別的目的 了解如何使用 static 資料成員與成員函式 了解容器( container) 類別的概念 了解具有檢視容器類別元素功能的迭代( iterator) 類別概念 了解指標 this 的用途. Chapter 7: 再論類別. 本章綱要 7.1 簡介 Introduction 7.2 常數物件與常數成員函式 7.3 合成:將物件當作類別的成員

vivi
Download Presentation

Chapter 7: 再論類別

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7: 再論類別 • 目標 • 能夠動態地建構和解構物件 • 能夠指定const(常數)物件和和const成員函式 • 了解夥伴(friend)函式與夥伴類別的目的 • 了解如何使用 static資料成員與成員函式 • 了解容器(container)類別的概念 • 了解具有檢視容器類別元素功能的迭代(iterator)類別概念 • 了解指標 this的用途

  2. Chapter 7: 再論類別 本章綱要 7.1 簡介 Introduction 7.2常數物件與常數成員函式 7.3 合成:將物件當作類別的成員 7.4夥伴函式與夥伴類別 7.5 this 指標的使用 7.6 使用 new 與 delete 運算子的動態記憶體配置 7.7 static 類別成員 7.8 資料抽象化與資訊隱藏 範例:陣列、字串、佇列 7.9 容器類別和迭代子 7.10 代理類別

  3. 7.1 簡介 • 第六章到第八章討論的是屬於物件導向程式設計的基本觀念。 • 第九章與第十章討論的繼承(inheritance)與多型(polymorphism),這才算是真正物件導向程式設計。

  4. const Time noon(12,0,0) Time( 12, 0, 0 ); 12 hour minute 0 second 0 7.2 const (常數) 物件與 const 成員函式 • 最小開放權限原則 Principle of least privilege • 只給物件所需要的權限、不給過多的權力 • 關鍵字 const • 說明這個物件不可以被修改 • 企圖修改這種物件的話會造成語法上的錯誤。 • 範例 const Time noon( 12, 0, 0 ); //可以作初值化 • 宣告類別 Time 的常數物件 noon,並作初值化成 12 點。

  5. 7.2 const (常數) 物件與 const 成員函式 • const物件需要const函式 • 我們只能呼叫 const物件的 const函式。沒有修改資料成員的函式必須被宣告成 const才能被 const物件使用。 • 宣告成const的成員函式不可修改物件內容 • const必須被寫在函式原型與定義的地方 • 函式原型: ReturnType FunctionName(param1,param2…) const; • 定義: ReturnType FunctionName(param1,param2…) const { …} • 範例: int A::getValue() const { returnprivateDataMember }; • const函式可以傳回資料成員的值,但不能修改其內容。

  6. 7.2 const (常數) 物件與 const 成員函式 • 建構子/解構子不可宣告為const • 對變數作初值化、就會修改其內容 常見的程式設計錯誤 7.1-4 • 定義為const的成員函式若修改資料成員的內容,會造成語法錯誤。 • const的函式不可呼叫 non-const函式,否則就造成語法錯誤。 • 透過 const 物件來呼叫非 const 函式,是語法錯誤。 • 企圖將建構子或解構子宣告為const是語法錯誤。

  7. 1 // Fig. 7.1: time5.h 2 // Declaration of the class Time. 3 // Member functions defined in time5.cpp 4 #ifndef TIME5_H 5 #define TIME5_H 6 7 class Time { const 函式 8 public: 9 Time( int = 0, int = 0, int = 0 ); // default constructor 非 const 函式 10 11 // set functions 12 void setTime( int, int, int ); // set time 13 void setHour( int ); // set hour 14 void setMinute( int ); // set minute 15 void setSecond( int ); // set second 16 17 // get functions (normally declared const) 18 int getHour() const; // return hour 19 int getMinute() const; // return minute 20 int getSecond() const; // return second 21 22 // print functions (normally declared const) 23 void printMilitary() const; // print military time 24 void printStandard(); // print standard time 25 private: 26 int hour; // 0 - 23 27 int minute; // 0 - 59 28 int second; // 0 - 59 29 }; 30 31 #endif

  8. 32 // Fig. 7.1: time5.cpp 建構子是非 const但可以被const物件呼叫 33 // Member function definitions for Time class. 34 #include <iostream> 35 36 using std::cout; 37 38 #include "time5.h" 39 40 // Constructor function to initialize private data. 41 // Default values are 0 (see class definition). 42 Time::Time( int hr, int min, int sec ) 43 { setTime( hr, min, sec ); } 44 45 // Set the values of hour, minute, and second. 46 void Time::setTime( int h, int m, int s ) 47 { 48 setHour( h ); 49 setMinute( m ); 50 setSecond( s ); 51 } 52 53 // Set the hour value 54 void Time::setHour( int h ) 55 { hour = ( h >= 0 && h < 24 ) ? h : 0; } 56 57 // Set the minute value 58 void Time::setMinute( int m ) 59 { minute = ( m >= 0 && m < 60 ) ? m : 0; } 60 61 // Set the second value 62 void Time::setSecond( int s ) 63 { second = ( s >= 0 && s < 60 ) ? s : 0; }

  9. 64 65 // Get the hour value 關鍵字 const必須出現在函式定義與函式原型中 66 int Time::getHour() const { return hour; } 非 const函式不能被const物件使用,就算這些函式沒有修改資料內容也一樣。 (就像printStandard). 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 cout << ( hour < 10 ? "0" : "" ) << hour << ":" 78 << ( minute < 10 ? "0" : "" ) << minute; 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 }

  10. 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 } 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)

  11. 7.2 const (常數) 物件與 const 成員函式 • 軟體工程的觀點 7.3 • const成員函式可被重載為非 const的版本,編譯器會依據呼叫此函式的物件是否為const來決定所要呼叫的函式是哪一個。 • 良好的程式設計習慣 7.1 • 將所有不需要修改資料內容的成員函式宣告為const ,如此一來,就可以被所需要的const物件使用。 • 常數物件是在建構子設定完資料成員的內容後才成為不可改變的常數。 • 有些編譯器可以讓設定為常數的變數執行速度變得比較快。

  12. 7.2 const (常數) 物件與 const 成員函式 • 前面介紹的是const物件、const函式,下面討論 const資料成員,要怎麼作呢? • 一個 const變數只能在宣告時順便作初值化,宣告後就不能再改變它的內容了。但是類別中的資料成員在宣告時不能給初值,這怎麼辦呢? • 下面的例子中 Increment 這個類別中有一個 increment 的 const資料成員,其初值化必須在建構子作,寫法如下: • Increment的建構子寫法如下: Increment::Increment( int c, int i ) :increment( i ) { count = c; } • : increment( i )將 increment 的初值設為i

  13. 1 // Fig. 7.4: fig07_04.cpp 2 // Using a member initializer to initialize a 3 // constant of a built-in data type. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; Increment 8 Increment( 0,5 ): count(0), increment(1) 若用設定敘述式來作 increment 的初值化設定(例如increment = i )會造成語法錯誤 9 class Increment { 10 public: 0 count 11 Increment( int c = 0, int i = 1 ); increment 1 12 void addIncrement() { count += increment; } 13 void print() const; 14 15 private: 16 int count; 17 const int increment; // const data member 18 }; 19 20 // Constructor for class Increment 21 Increment::Increment( int c, int i ) 22 : increment( i ) // initializer for const member 23 { count = c; } 24 25 // Print the data 26 void Increment::print() const 27 { 28 cout << "count = " << count 29 << ", increment = " << increment << endl; 30 } 31 32 int main() 33 {

  14. 34 Increment value( 10, 5 ); 35 36 cout << "Before incrementing: "; 37 value.print(); 38 39 for ( int j = 0; j < 3; j++ ) { 40 value.addIncrement(); Increment value(10,5) 41 cout << "After increment " << j + 1 << ": "; Increment( 10,5 ): count(10), increment(5) 42 value.print(); 43 } 10 count 44 increment 5 45 return 0; 46 } 20 25 15 Before incrementing: count = 10, increment = 5 After increment 1: count = 15, increment = 5 After increment 2: count = 20, increment = 5 After increment 3: count = 25, increment = 5

  15. 1 // Fig. 7.5: fig07_05.cpp …… 9 class Increment { 11 public: …… 22 private: 23 int count; 24 const int increment; 26 }; 29 Increment::Increment( int c, int i ) { 31 count = c; 32 increment = i; // ERROR: Cannot modify a const object 34 } ……

  16. 7.2 const (常數) 物件與 const 成員函式 • 實際上每個資料成員都可以用這種方式(成員初值設定語法)來作初值化 • 而 const與參照必須(只能)用成員初值設定語法來作初值化。 • 如果有多個資料成員需要如此初值化時 • 可用逗號隔開 • 測試提醒:當某個成員函式並不需要改變 資料成員的內容時,就將它設為 const,這樣無論物件是否為 const都可以使用。 • 常見錯誤7.5:沒有提供const資料成員的初值,這是語法錯誤。

  17. 7.3 合成:將物件當作類別的成員 • 合成 composition • Class has objects of other classes as members,即有其他物件為成員的類別 • 這種方式是最自然地再使用的作法。 • 例如員工資料中,有生日、聘用日期等,這些日期資料可以直接使用「日期」的物件。 • 物件的建構 construction of objects • 成員物件按照宣告順序來建構(產生) • 而不是照建構子的成員初值設定順序 • Constructed before their enclosing class objects (host objects) ,小物件先建構(產生),這是物件的建構順序;而解構順序與此顛倒。

  18. 1 // Fig. 7.6: date1.h,這個範例共有五個檔案 Date 2 // Declaration of the Date class. 1 month 1 10 day 3 // Member functions defined in date1.cpp year 1900 4 #ifndef DATE1_H 檢查日期是否正確的函式 5 #define DATE1_H 6 7 class Date { 8 public: 9 Date( int = 1, int = 1, int = 1900 ); // default constructor 10 void print() const; // print date in month/day/year format 11 ~Date(); // 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

  19. 22 // Fig. 7.4: date1.cpp 23 // Member function definitions for Date class. 24 #include <iostream> 25 當建構子被呼叫時會輸出一行文字 26 using std::cout; 27 using std::endl; 28 29 #include "date1.h" 30 31 // Constructor: Confirm proper value for month; 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 ) // validate the month 37 month = mn; 38 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 45 46 cout << "Date object constructor for date "; 47 print(); // interesting: a print with no arguments 48 cout << endl; 49 } 50

  20. 51 // Print Date object in form month/day/year 52 void Date::print() const 53 { cout << month << '/' << day << '/' << year; } 54 解構子被呼叫時、也會輸出一行文字 這是檢查日期是否合法的函式 55 // Destructor: provided to confirm destruction order 56 Date::~Date() 57 { 58 cout << "Date object destructor for date "; 59 print(); 60 cout << endl; 61 } 62 63 // Utility function to confirm proper day value 64 // based on month and year. 65 // Is the year 2000 a leap year? 66 int Date::checkDay( int testDay ) 67 { 68 static const int daysPerMonth[ 13 ] = 69 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 70 71 if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) 72 return testDay; 73 74 if ( month == 2 && // February: Check for leap year 75 testDay == 29 && 76 ( year % 400 == 0 || 77 ( year % 4 == 0 && year % 100 != 0 ) ) ) 78 return testDay; 79 80 cout << "Day " << testDay << " invalid. Set to day 1.\n"; 81 82 return 1; // leave object in consistent state if bad value 83 }

  21. 84 // Fig. 7.4: emply1.h Date hire Date birth 85 // Declaration of the Employee class. 1 1 month month 1 1 10 10 day day Employee 86 // Member functions defined in emply1.cpp 1900 1900 year year 87 #ifndef EMPLY1_H firstname lastname 88 #define EMPLY1_H 89 90 #include "date1.h" 91 合成:有其他類別物件的成員,且是常數物件 物件建構的順序與宣告的順序相同。 92 class Employee { 93 public: 94 Employee( char *, char *, int, int, int, int, int, int ); 95 void print() const; 96 ~Employee(); // provided to confirm destruction order 97 private: 98 char firstName[ 25 ]; 99 char lastName[ 25 ]; 100 const Date birthDate; 101 const Date hireDate; 102 }; 103 104 #endif

  22. 105 // Fig. 7.4: emply1.cpp 106 // Member function definitions for Employee class. 107 #include <iostream> 108 建構子被呼叫時會輸出一行文字 要將兩個類別的 介面檔案都 include 進來 109 using std::cout; 成員初值設定串列,兩個常數物件的初值設定 110 using std::endl; 111 112 #include <cstring> 113 #include "emply1.h" 114 #include "date1.h" 115 116 Employee::Employee( char *fname, char *lname, 117 int bmonth, int bday, int byear, 118 int hmonth, int hday, int hyear ) 119 : birthDate( bmonth, bday, byear ), 120 hireDate( hmonth, hday, hyear ) 121 { 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 ); 131 strncpy( lastName, lname, length ); 132 lastName[ length ] = '\0'; 133 134 cout << "Employee object constructor: " 135 << firstName << ' ' << lastName << endl; 136 }

  23. 137 138 void Employee::print() const 139 { 140 cout << lastName << ", " << firstName << "\nHired: "; print函式是const且當 Date 物件被建立或解構時會執行,因為它是 const 函式,所以可以輸出const物件。 Print不需要引數,它隱含地連結到呼叫它的物件 解構子被呼叫時會輸出一行文字 141 hireDate.print(); 142 cout << " Birth date: "; 143 birthDate.print(); 144 cout << endl; 145 } 146 147 // Destructor: provided to confirm destruction order 148 Employee::~Employee() 149 { 150 cout << "Employee object destructor: " 151 << lastName << ", " << firstName << endl; 152 }

  24. 153 // Fig. 7.4: fig07_04.cpp Employee manager( "Bob", "Jones", birth, hire ) Date hire(3,12,1988) Date birth(7,24,1949) 154 // Demonstrating composition: an object with member objects. 3 7 Bob firstname month month 24 12 10 10 day day lastname Jones 155 #include <iostream> 1988 1949 year year birth hire 156 只有emply.h需要被載入,該檔案就有載入date.h. 157 using std::cout; 158 using std::endl; 159 160 #include "emply1.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 ); // invalid Date values 171 cout << endl; 172 return 0; 173 }

  25. 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 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 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 注意內部的物件先被建立、且較慢被解構

  26. 7.3 合成:將物件當作類別的成員 • 當類別中有其他類別的成員時,一定要有預設的建構子。 • 兩個類別的 print() 函式都是 const,不需要改變資料成員的函式最好都設為 const。且這兩個 print 都沒有 augments。 • firstName 與 lastName 兩個字串長度都是 25,比較浪費空間,而且若輸入長度超過 25 的字串時,會被切掉。後面使用動態記憶體管理時,可以改善這些情形。

  27. 7.4 夥伴(friend)函式和夥伴類別 • friend函式與friend類別 • 可以存取其他類別宣告在private或 protected裡面的資料 • friend函式不是類別的成員函式,所以存取其 資料成員的方式不同。 • 定義在類別範圍之外 • 朋友關係的性質 Properties of friendship • 朋友關係只能他人授權、而非自行取得 • 非對稱 (若 B 是 A 的朋友,A 未必就是 B 的朋友) • 沒有遞移性(若A是 B 的朋友、B是 C 的朋友,A 未必是 C 的朋友)

  28. 7.4 夥伴函式和夥伴類別 • friend宣告 • 宣告夥伴(friend)函式 • 將friend寫在要授權的類別中之函式原型的前面,如: friend int myFunction( int x ); 這一行應出現在要授權的類別裡面 • 宣告夥伴(friend)類別 若 Classtwo要成為 Classone的夥伴類別,必須在 Classone中宣告 friend class ClassTwo;

  29. 1 // Fig. 7.5: fig07_05.cpp 這裡宣告 setX是Count的friend (可存取private資料). 2 // Friends can access private members of a class. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; setX被正常定義,且不是 Count 的成員函式Count. 7 8 // Modified Count class 9 class Count { 可以改變Count 的private變數,但存取方式與Count 的成員函式不同 10 friendvoid 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 }; 17 18 // Can modify private data of Count because 19 // setX is declared as a friend function of Count 20 void setX( Count &c, int val ) 21 { 22 c.x = val; // legal: setX is a friend of Count 23 } 24 25 int main() 26 { 27 Count counter; 28 29 cout << "counter.x after instantiation: "; 30 counter.print();

  30. 31 cout << "counter.x after call to setX friend function: "; 32 setX( counter, 8 ); // set x with a friend,一般涵數 33 counter.print(); 34 return 0; 35 } private資料已被改變。 counter.x after instantiation: 0 counter.x after call to setX friend function: 8 • 軟體工程的觀點 7.9 • 雖然夥伴函式的原型出現在類別的定義裡,但它仍然不是類別的成員函式。

  31. 1 // Fig. 7.6: fig07_06.cpp 2 // Non-friend/non-member functions cannot access 3 // private data of a class. cannotSetX不是 Count 的friend,不能存取其private資料 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 // Modified Count class cannotSetX試著去修改private變數 x 10 class Count { 11 public: 12 Count() { x = 0; } // constructor 13 void print() const { cout << x << endl; } // output 14 private: 15 int x; // data member 16 }; 17 18 // Function tries to modify private data of Count, 19 // but cannot because it is not a friend of Count. 20 void cannotSetX( Count &c, int val ) 21 { 22 c.x = val; // ERROR: 'Count::x' is not accessible 23 } 24 25 int main() 26 { 27 Count counter; 28 29 cannotSetX( counter, 3 ); // cannotSetX is not a friend 30 return 0; 31 }

  32. 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) 因此產生語法錯誤 -不可存取 private 資料 • 軟體工程的觀點 7.10 • 夥伴關係的宣告與private、protected、public的符號無關,因此夥伴關係的宣告可置於類別定義裡的任何位置。

  33. 7.5this指標的使用 • this指標 • 是指向物件本身的指標,允許物件存取自己的位置 • 不是物件本身的一部份,不會反應在sizeof中 • 呼叫物件的 non-static成員函式時,隱含的第一個參數。 • Implicitly reference member data and functions,呼叫本身的成員時沒寫物件名稱,就等於是加上 this-> 的意思。 • this指標的型態是根據該物件的型態與成員函式是否為 const而定。

  34. Employee Employee * const this Employee const Employee * const this 7.5this指標的使用 • 對於 Employee 的 non-const成員函式,this的型態 Employee * const • 指向Employee物件的常數指標 • 在Employee的const成員函式,this的型態為 const Employee * const • 指向常數物件Employee的常數指標

  35. 7.5this指標的使用 • 使用this 的範例 • 成員函式中存取資料成員x,有以下三種寫法 X, this->x, 或 (*this).x • 一連串的成員函式呼叫 • 函式傳回一個指向相同物件的指標 { return *this; } • 其他函式可以使用這個指標繼續作運算 • 沒有傳回這種指標參照的函必須最後被呼叫

  36. 1 // Fig. 7.7: fig07_07.cpp 2 // Using the this pointer to refer to object members. 3 #include <iostream> 4 5 using std::cout; 直接輸出x 6 using std::endl; Test 7 使用 this 指標與箭頭(->)運算子來輸出 x x 12 8 class Test { this 9 public: 用點(.)運算子輸出x。括號是必須的,因為點運算子的優先順序比 * 還高,沒寫括號的話就變成 *(this.x)的意思,造成語法錯誤。 10 Test( int = 0 ); // default constructor 11 void print() const; 12 private: 13 int x; 14 }; 15 16 Test::Test( int a ) { x = a; } // constructor 17 18 void Test::print() const// ( ) around *this required 19 { 20 cout << " x = " << x 21 << "\n this->x = " << this->x 22 << "\n(*this).x = " << ( *this ).x << endl; 23 } 24 25 int main() 26 { 27 Test testObject( 12 ); 28 29 testObject.print(); 30 31 return 0; 32 } 1. Class definition 1.1 Function definition 1.2 Initialize object 2. Function call

  37. x = 12 this->x = 12 (*this).x = 12 三個方法輸出的結果都相同

  38. 7.5this指標的使用 • 連續呼叫成員函式的例子 • 成員函式setHour, setMinute, 與setSecond都傳回*this (指向物件) • 對物件t來說,考慮下列呼叫 • t.setHour(1).setMinute(2).setSecond(3); • 先執行 t.setHour(1), 傳回*this (指向物件)成為 • t.setMinute(2).setSecond(3); • 再執行 t.setMinute(2), 傳回 *this 成為 • t.setSecond(3); • 最後執行t.setSecond(3), 傳回 *this成為 • t; • 不會再產生其他結果

  39. 1 // Fig. 7.8: time6.h 2 // Cascading member function calls. 3 4 // Declaration of class Time. 5 // Member functions defined in time6.cpp 6 #ifndef TIME6_H 注意函式傳回的資料型態是Time &- 指向 Time物件的參照. 7 #define TIME6_H 8 9 class Time { 10 public: 11 Time( int = 0, int = 0, int = 0 ); // default constructor 12 13 // set functions 14 Time &setTime( int, int, int ); // set hour, minute, second 15 Time &setHour( int ); // set hour 16 Time &setMinute( int ); // set minute 17 Time &setSecond( int ); // set second 18 19 // get functions (normally declared const) 20 int getHour() const; // return hour 21 int getMinute() const; // return minute 22 int getSecond() const; // return second 23 24 // print functions (normally declared const) 25 void printMilitary() const; // print military time 26 void printStandard() const; // print standard time 27 private: 28 int hour; // 0 - 23 29 int minute; // 0 - 59 30 int second; // 0 - 59 31 }; 32 33 #endif

  40. 34 // Fig. 7.8: time.cpp 傳回*this因此可以作連續的函式呼叫 35 // Member function definitions for Time class. 36 #include <iostream> 37 38 using std::cout; 39 40 #include "time6.h" 41 42 // Constructor function to initialize private data. 43 // Calls member function setTime to set variables. 44 // Default values are 0 (see class definition). 45 Time::Time( int hr, int min, int sec ) 46 { setTime( hr, min, sec ); } 47 48 // Set the values of hour, minute, and second. 49 Time &Time::setTime( int h, int m, int s ) 50 { 51 setHour( h ); 52 setMinute( m ); 53 setSecond( s ); 54 return *this; // enables cascading 55 } 56 57 // Set the hour value 58 Time &Time::setHour( int h ) 59 { 60 hour = ( h >= 0 && h < 24 ) ? h : 0; 61 62 return *this; // enables cascading 63 } 64

  41. 65 // Set the minute value 傳回*this因此可以作連續的函式呼叫 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 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 cout << ( hour < 10 ? "0" : "" ) << hour << ":" 94 << ( minute < 10 ? "0" : "" ) << minute;

  42. 95 } 105 // Fig. 7.8: fig07_08.cpp 96 106 // Cascading member function calls together 97 // Display standard format time: HH:MM:SS AM (or PM) 107 // with the this pointer 98 void Time::printStandard() const 108 #include <iostream> printStandard沒有傳回指向物件的參照 99 { 109 100 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) 110 using std::cout; 101 << ":" << ( minute < 10 ? "0" : "" ) << minute 111 using std::endl; 注意這裡作連續函式呼叫 102 << ":" << ( second < 10 ? "0" : "" ) << second 112 連續函式呼叫, printStandard必須在setTime之後再呼叫,因為printStandard並沒有傳回指向物件的參照。 t.printStandard().setTime();若如此呼叫會造成語法錯誤。 113 #include "time6.h" 103 << ( hour < 12 ? " AM" : " PM" ); 104 } 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();

  43. 127 cout << endl; 128 129 return 0; 130 } Military time: 18:30 Standard time: 6:30:22 PM New standard time: 8:20:20 PM

  44. 7.6 使用new和delete運算子的動態記憶體配置 • new和 delete • 作動態記憶體配置之用,C 語言中也有類似功能的函式呼叫- malloc 與 free,C++ 的 new與delete 比較好。 • new • 建立適當大小的物件,呼叫其建構子並傳回正確型態的指標 • delete • 解構物件並釋放記憶體空間 • delete的範例 delete typeNamePtr; • 呼叫TypeName物件的解構子並釋放記憶體 delete [] arrayPtr; • 用來動態地殺掉一個陣列

  45. timePtr timePtr Time( 0, 0, 0 ); Time( 12, 0, 0 ); 0 12 hour hour minute minute 0 0 0 0 second second ptr 3.14259 7.6 使用new和delete運算子的動態記憶體配置 • new的範例 TypeName *typeNamePtr; • 建立一個指向TypeName物件的指標 typeNamePtr = new TypeName; • new建立 TypeName物件,傳回指標(存到typeNamePtr裡面) Time *timePtr; timePtr = new Time; • 對物件作初值化 double *ptr = new double( 3.14159 ); Time *timePtr = new Time( 12, 0, 0 ); • c.f. : double v = 3.14159; ptr = &v;

  46. gradesArray 0 0 0 0 0 0 0 0 0 0 7.6 使用new和delete運算子的動態記憶體配置 • 對物件作初值化 • 建立一10個元素的int陣列並設定給arrayPtr int *gradesArray = new int[ 10 ]; • 用new建立物件時,會自動呼叫建構子;而 delete會自動呼叫解構子。

  47. 7.6 使用new和delete運算子的動態記憶體配置 • 常見錯誤7.8: 將new-和-delete-型式的動態記憶體配置與malloc-和-free-型式的動態記憶體配置相混雜是邏輯錯誤,也就是用malloc配置的空間不可用delete來釋放;而用new建立的物件不可用free來刪除。 • 常見錯誤7.9: 對陣列使用delete來取代delete[]會引起執行時的邏輯錯誤;謹記:動態建立的陣列空間必須用delete[]運算子刪除,而動態建立的個別元素就用delete運算子刪除。 • 好的習慣 7.3: C++ 都可以使用 C 的敘述式,所以在 C++ 中也可以使用 malloc與 free。但最好單單使用 new與delete不要用 malloc與 free。

  48. 7.7static類別成員 • static類別成員 • 記錄類別的資料,同一個類別的static資料成員只記錄一份,而不是每個物件都記錄一份。 • 一般說來,每個物件會記錄一份資料變數的內容 • 例如:要記錄所建構的物件個數時,使用 static類別成員比較有效率、又可節省空間 • 只需改變static變數內容,而不需改每個物件內容 • 可視為類別範圍的全域變數 • 只能被同一個類別的物件存取 • 用檔案範圍的方式作初值化 • 就算沒有任何物件被宣告,此種成員也存在、可使用 • 變數與函式都可被宣告為static • 可設成public, private或 protected

  49. class C { int x; static int s; }; C c1, c2; x x C::s c1 c2 7.7static類別成員 • static變數 • 可以透過類別名稱來存取 static成員的值。 • publicstatic變數 • 可以用範圍解析運算子(::)來存取 Employee::count • privatestatic變數 • 當沒有該類別的物件存在時,必須透過 publicstatic成員函式來存取。 • 用類別名稱加範圍解析運算子呼叫publicstatic成員函式 Employee::getCount() • 一般成員函式或資料成員只能透過物件來呼叫,但是static資料成員或成員函式可以透過類別名稱來使用。

  50. 7.7static類別成員 • static函式 • static成員函式不可存取非 static的資料或呼叫非static的函式。 • static函式沒有this指標,他們的存在與物件無關。

More Related