Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機程式 第十四單元 Polymorphism 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,

Similar presentations


Presentation on theme: "計算機程式 第十四單元 Polymorphism 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,"— Presentation transcript:

1 計算機程式 第十四單元 Polymorphism 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. © 2010 。 本 講義僅引用部分內容,請讀者自行準備。 1 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法 第 46 、 52 、 65 條合理使用。 Microsoft 服務合約

2 An Overview Polymorphism o To associate many meanings to one function name by means of the late binding (runtime) mechanism o Program in general, execute in specific Must work with class hierarchy Must work off base-class pointer or reference handles, not off name handles o Polymorphism: virtual function + late binding (i.e., run-time)/ dynamic binding 2

3 Virtual function To make a function virtual is to tell the complier that “I don’t know how the function is implemented. Wait until it is used in a program, and then get the implementation from the object instance.” e.g., virtual double bill() const; Pure virtual function o virtual double bill() const =0; 3

4 More on Class Hierarchy Upcasting is ok, o e.g., vpet = vdog; Downcasting is not permitted unless dynamic_cast (it only works for pointer type) is explicitly used. e.g., (downcasting) Pet *ppet; ppet=new Dog; Dog *pdog = dynamic_cast (ppet); 4

5 Virtual Destructor Make destructor in the base class virtual (then, the destructor for the derived class is called), otherwise, only the destructor for the base class is called. o e.g., SomeCass *p= new SomeClass; delete p; o e.g., Base *pBase = new Derived; delete pBase; 5

6 版權聲明 6 頁碼作品版權圖示來源 / 作者 1-6 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。


Download ppt "計算機程式 第十四單元 Polymorphism 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,"

Similar presentations


Ads by Google