1 / 23

類別樣板 Class Template

類別樣板 Class Template. 類似函式樣板 由類別樣板產生的類別稱為類別樣版的實體 (instance) 此建立過程稱為 instantiating. template <class T> Class Cexample { T m_value; }. Class Cexample { int m_value; } Class Cexample { CBox m_value; }. Example. T 是一個變數用來設定型態 , 根據不同型態建立新類別. 定義類別樣板 p350. 說明. 參數 T 是變數型態

carys
Télécharger la présentation

類別樣板 Class Template

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. 類別樣板 Class Template • 類似函式樣板 • 由類別樣板產生的類別稱為類別樣版的實體(instance) • 此建立過程稱為instantiating

  2. template <class T> Class Cexample { T m_value; } Class Cexample { int m_value; } Class Cexample { CBox m_value; } Example T是一個變數用來設定型態, 根據不同型態建立新類別

  3. 定義類別樣板 p350

  4. 說明 • 參數T是變數型態 • 宣到類別物件時, T會被指定的型態取代

  5. 如何放在定義外 template <class T> T Csamples<T>::Max() const { T theMax = m_Values[0]; for (int i=1; i<m_Free, i++) if…. } 樣版的成員函式 用來辨別函式屬於哪個樣版所產生

  6. 建構子定義在類別樣板之外 template<class T> Csamples<T>::Csamples(T values[], int count) { m_Free = count < 100 ? Cpunt:100; for(int i=0; i<m_Free; i++) m_Values[i]=Values[i]; }

  7. 從類別樣板建立物件 • Csamples <double> myData(10.0) • Example 9_07.cpp

  8. 多個參數的類別樣板 template<class T1, class T2> class Cexample { private: T1 m_value1; T2 m_Value2; }

  9. Chapter 10 類別繼承

  10. 目標 • 由已存在類別定義新的類別 • 類別成員存取屬性關鍵字protected • 夥伴類別 • 虛擬函式 • 純虛擬函式 • 抽象類別 • 虛擬建構子 • 多重繼承

  11. 類別 • 用來表示某種抽象事物 • 真實世界的物件的資料型態定義

  12. Example p384 • Box • Length, Breadth, Height • Carton, CandyBox, Crate, BeerCrate

  13. 類別繼承 • 衍生類別(derived) • 基礎類別(base class) • 類別以一個或多個類別為基礎來定義 • 衍生類別繼承(inherit)基礎類別的資料成員和函式成員

  14. 類別繼承 (cont.) • 解構子、建構子、覆載的指定運算子等不會被繼承 • 衍生類別有自己的解構子及建構子

  15. 何為基礎類別 • 放在另一個類別定義中的類別 • Ex: B類別的定義中含A類別 • A是B的直接基礎類別(direct base class) • Ex: p384 • CBox是CBeerCrate的間接基礎類別(indirect base class)

  16. Figure p385

  17. class Cbox { public: double m_Length; double m_Breadth; double m_Height; Cbox(…..) } class CCandyBox: CBox { public: char* m_Contents; CCandyBox(char* str = "Candy") { m_Contents = new char[ strlen(str) + 1 ]; strcpy(m_Contents, str);} ~CCandyBox() { delete[] m_Contents; }; }; Example

  18. Ex10_01.cpp

  19. 說明 • CBox:24bytes • CCandyBox:32bytes • 被繼承的基礎類別內定存取屬性private • class CCandyBox: private CBox • class CCandyBox: public CBox • 基礎類別一定要有存取屬性, 用來決定被衍生類別繼承後的成員存取狀態 • 若省略, 則編譯器假設為private

  20. 繼承下的存取控制 • 基礎類別的private資料成員也是衍生類別的成員 • 在衍生類別中將維持private屬性 • p389

  21. 存取基礎類別的private成員 • Ex10_02.cpp class CBox { public: CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0): m_Length(lv), m_Breadth(bv), m_Height(hv){} //Function to calculate the volume of a CBox object double Volume() const { return m_Length*m_Breadth*m_Height; } private: double m_Length; double m_Breadth; double m_Height; };

  22. 衍生類別建構子的運作 • 建立衍生類別的基礎類別部份, 會呼叫基礎類別的建構子而非衍生類別的建構子 • Ex10_03.cpp • 從衍生類別的建構子去呼叫基礎類別的某個建構子, 即可利用基礎類別的建構子將衍生類別的資料成員初始化 • 或用衍生類別建構子的資料去改變基礎類別建構子的內容

  23. 呼叫基礎類別的建構子 class CCandyBox: public CBox { public: char* m_Contents; // Constructor to set dimensions and contents // with explicit call of CBox constructor CCandyBox(double lv, double bv, double hv, char* str= "Candy") :CBox(lv, bv, hv) { cout << endl <<"CCandyBox constructor2 called"; m_Contents = new char[ strlen(str) + 1 ]; strcpy(m_Contents, str); } // Constructor to set contents // calls default CBox constructor automatically CCandyBox(char* str= "Candy") { cout << endl << "CCandyBox constructor1 called"; m_Contents = new char[ strlen(str) + 1 ]; strcpy(m_Contents, str); } ~CCandyBox() // Destructor { delete[] m_Contents; } };

More Related