第十章 虛擬函數 (Virtual Functions)

Similar documents
運算子多載 Operator Overloading

Strings

Strings

運算子多載 Operator Overloading

untitled

Microsoft Word - chap10.doc

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Microsoft Word cppFinalSolution.doc

Microsoft Word - 物件導向編程精要.doc

untitled

第七章 繼承

PowerPoint Presentation

c_cpp

L L

新版 明解C++入門編

ebook39-5

提问袁小兵:

FY.DOC

運算子多載 Operator Overloading

ebook55-13

Microsoft PowerPoint - P766Ch06.ppt

untitled

全国计算机技术与软件专业技术资格(水平)考试

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Chapter12 Derived Classes

Template

第七讲 继承与多态

ebook39-6

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft Word - ch04三校.doc

(procedure-oriented)?? 2

Microsoft Word - 01.DOC

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

untitled

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F


untitled

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

Microsoft PowerPoint - 13_ClassAndObj.ppt

概述

Chapter 9: Objects and Classes

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

ebook14-4

NethersoleJO89(8).indd

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

untitled

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

Microsoft PowerPoint - plan08.ppt

《大话设计模式》第一章

中北大学常规事项财务报销操作指南

Microsoft PowerPoint - Class5.pptx

Open topic Bellman-Ford算法与负环

CC213

epub83-1

【主持人】:给大家介绍一下,这次的培训是我们画刊部的第三次培训,当然今天特别有幸请来著吊的摄影家李少白老师给我们讲课


Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien

95年9月1日 圖書館林主任工作報告:

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Microsoft PowerPoint - 11_Templates.ppt

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

Microsoft PowerPoint - string_kruse [兼容模式]

文档 1

6寸PDF生成工具

Chapter 9: Objects and Classes

<4D F736F F D B0D3B77EC3FEA7DEC3C0C476C1C9A5BFA6A1B8D5C3442DB57BA6A1B35DAD702DBEC7ACEC2E646F6378>

CC213

1.5招募说明书(草案)

csg(1_29)cs.p65

第二章 簡介類別

EJB-Programming-3.PDF

untitled

团 学 要 闻 我 校 召 开 共 青 团 五 届 九 次 全 委 ( 扩 大 ) 会 议 3 月 17 日, 我 校 共 青 团 五 届 九 次 全 委 ( 扩 大 ) 会 议 在 行 政 办 公 楼 五 楼 会 议 室 举 行, 校 团 委 委 员 各 院 ( 系 ) 团 委 书 记 校 学 生

1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity

建模与图形思考

國立屏東教育大學化學生物系

<4D F736F F D20C540A468BAC2BFEFB3F8A657B6B7AABE2E646F63>

untitled

相 应 功 能 (5) 再 将 Boy 类 作 为 Girl 类 的 友 元 类, 在 Boy 类 的 某 成 员 函 数 VisitGirl(Girl & ) 中 访 问 Girl 类 的 私 有 成 员, 观 察 编 译 器 给 出 的 信 息 ( 6 ) 删 除 两 个 类 中 的 函 数 V

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Microsoft Word - 11.doc

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

C++ 程式設計

The Embedded computing platform

2



untitled

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

投影片 1

EJB-Programming-4-cn.doc

Microsoft Word 箕æ−¥ï¼‹å®ı稿;

98年度即測即評學科測試與即測即評即發證技術士技能檢定簡章

Microsoft Word - 新1-12.doc

Transcription:

繼承的優點 程式碼再使用 (code reuse) 抽象概念再使用 類別階層化 澄清物件間的關係

繼承與 Code Reuse( 被動 ) 主計劃 子計劃 1 子計劃 2 子計劃 3 ( 你所在的組 ) 類別庫函式庫 (.lib.dll)

繼承與 Code Reuse class List { void insert() { void delete() { 重新改寫??? (1) 原始碼在哪? (2) 還有其他使用者, 是否都同意改寫? 如果你對 class list list 感到 (1) 功能不足 (2) 原有功能不佳 使用繼承來改善

繼承與 Code Reuse class List {.. insert(int n) ;. delete(int pos);; class List1: public List { node& operator[](int index) ; // 新增 void insert(int n) ; // 修改 : 更高效率的實作方式 ; void main() { List1 L;.. ; L.insert(15) ; cout << L[3] ;

可能結果 主計劃 子計劃 1 (List1) 子計劃 2 (List2) 子計劃 3 ( 沿用 List) 類別庫 (List,Stack )

繼承與 Code Reuse( 主動 ) Q: 設計一物件導向資料庫 : 儲存學校學生資料 [ 先修生 ] 學號系級高中校名 [ 僑生 ] 學號系級 e-mail 國籍 [ 交換學生 ] 學號系級 e-mail 國籍 [ 一般生 ] 學號系級 e-mail 選課 () 註冊 () 選課 () 註冊 () 工讀 () 選課 () 註冊 () 定期約談 () 選課 () 註冊 () 工讀 ()

作業 : list stack struct node {int info; node* next; ; class list { node *head, *tail ; int node_no ; public: list() ; list(const node& n) ; list(const list& L) ; ~list() ; int getsize() ; void insert(int pos, int value) ; // 0: first, node_no:last void delete(int pos) ; // 刪除第 pos 個元素 void show(string msg) ; // 印出串列內容 list& operator=(const list& L) ; friend list operator+(const list& L1, const list& L2) ; // 聯集 friend list operator*(const list& L1, const list& L2) ; // 交集 friend list operator-(const list&l1, const list& L2) ; // 差集

使用 list 產生 stack class stack: public list { stack() ; ~stack() ; operator=(const stack& s) ; void push(int x) ; // 加在 list 的最前端 int pop() ; // 刪除 list 的第一個元素 // list 中的 operator+, -, * 是否也被繼承?

測試 class list void main() { list L1, L2, L3 ; for (int i = 101; i<=108; i++) L1.insert(L1.getSize(),i) ; for (int j = 110; j>=105; j--) L2.insert(L2.getSize(), j) ; L1.show( L1= ); L2.show( L2= ) ; L3 = L1 + L2 ; L3.show( L3=L1+L2= ) ; L3 = L1 * L2; L3.show( L3=L1*L2= ) ; L3 = L1 L2; L3.show( L3=L1-L2= ) ; L3.delete(1) ; L3.delete(2) ; L3.show( after 2 delete, L3= ) ; stack s1, s2 ; for (int k=1; k<=10; k++) { if (k%3==0) s1.pop(); s1.push(k) ; s1.show( s1= ) ; s2 = s1; s2.show( s2= ) ;

第十章多型與虛擬函數 (Polymorphism & Virtual Functions) 10-1 衍生類別的指標 10-2 簡介虛擬函數 10-3 虛擬函數的細節 10-4 應用多型

多型 編譯時期多型 ( 靜態多型 ) function overloading 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型

衍生類別與基底類別物件間的 指定 (assignment) class base { int x ; public: setx(int n) { x=n; class derived: public base { int y ; public: setx(int n) { base::setx(3*n); sety(int n) { y = n; void main() { base b ; derived d ; b = d ; // 可乎? b.setx(5) ; // 哪個 setx() b.sety(10); //? d = b ; //? d.setx(5) ; d.sety(8); //?

結論 base Obj = derived Obj ( 可 ) derived Obj = base Obj ( 否 ) void main() { base b ; derived d ; b = d ; // 可 b.setx(5) ; // 哪個 setx() b.sety(10); //? 否 //? 可 d = b ; //? 否 d.setx(5) ; d.sety(8);

10-1 衍生類別的指標 Case 1 void main() { base *pb ; base b; derived d ; pb = &b ; // Sure! pb->setx(5) ; pb = &d ; // 可乎? pb->setx(5) ; // 哪個? pb->sety(10); //? 否 case 2 void main() { derived *pd ; base b; derived d ; pd = &b ; //??? 否 pd->setx(5) ; //? 否

衍生類別的參考 (reference) Case 1 void main() { base b; derived d ; base &refb1 = b ; // sure refb1.setx(5) ; // 哪個? base base& refb2 = d ; //?? 可 refb2.setx(5) ; refb2.sety(10); //?? 否 case 2 void main() { base b; derived d ; derived &refd1 = b ; //? 否 refb1.setx(5) ;

結論 : 自己寫 base-pointer = &derived-obj ( 可 ) base-reference = derived-obj ( 可 ) derived-pointer = &base-obj ( 否 ) derived-reference = base-obj ( 否 )

10-2 多型與虛擬函數 甚麼是執行時期的多型? 是否能呼叫到正確的 move() 與 stop(); class car { move() ; stop(); class Benz: public car { move() ; stop(); class Volvo: public car { move() ; stop(); class Civic: public car { move() ; stop(); void main() { Benz b; Volvo v; Civic c ; demo(b); demo(v); demo(c) ; void demo(car& c) { c.move() ; c.stop() ;

不使用虛擬函數 class car { public:void move() { cout << car move ; class Benz: public car { public:void move() { cout << Benz move ; class Volvo: public car { public:void move() { cout << Volvo move ; void demo(car& c) { c.move() ; void main() { Benz b; Volvo v; demo(b); demo(v) ; 實際 try! 輸出結果為何?

甚麼是虛擬函數? 是一種宣告在基底類別中的成員函數 提供執行時期多型的機制 使用 virtual 保留字 通常衍生類別會 override 它

使用虛擬函數 ( 配合 reference) class car { 實際 try! virtual void move() { cout << car move ; 輸出結果為何? class Benz: public car { void move() { cout << Benz move ; class Volvo: public car { void move() { cout << Volvo move ; void demo(car& c) { c.move() ; void main() { Benz b ;Volvo v; demo(b); demo(v) ;

使用虛擬函數 ( 配合 pointer) class car { virtual void move() { cout << car move ; class Benz: public car { void move() { cout << Benz move ; class Volvo: public car { void move() { cout << Benz move ; void demo(car *pc) { pc->move() ; void main() { Benz b ;Volvo v; demo(&b); demo(&v) ; 實際 try! 輸出結果為何?

使用虛擬函數 ( 配合物件傳遞 ) class car { virtual void move() { cout << car move ; class Benz: public car { void move() { cout << Benz move ; class Volvo: public car { void move() { cout << Benz move ; void demo(car c) { c.move() ; void main() { Benz b ;Volvo v; demo(b); demo(v) ; 實際 try! 輸出結果為何?

不使用多型可以嗎? 多型 : 一個介面多種用法 void move(car& c) { c.move() ;. 不多型 : void move(void *p, int type) { switch(type){ case 1: ((Benz *)p)->move(); break ; case 2: ((Volvo *)p)->move(); break ;

練習 class plane { virtual void fly() { takeoff(); onair(); landing(); void onair() { void takeoff() { void landing() { // 你不滿意 takeoff 的行為該如何?

Plane class plane{ public: virtual void fly() { takeoff(); onair(); landing(); void onair() {cout<<"onair"<<endl; virtual ; void takeoff() {cout<<"takeoff"<<end l; void landing() {cout<<"landing"<<end l; class F16:public plane{ public: // void fly(){cout<<"f16 fly"<<endl; void takeoff(){cout<<"f16 takeoff"<<endl; ; class B747:public plane{ public: // void fly(){cout<<"b747 fly"<<endl; void takeoff(){cout<<"b747 takeoff"<<endl; ; void demo(plane &p){p.fly(); void main() { B747 b; F16 f; demo(b); demo(f);

Case Study: p. 10-19 list head store(x); retrieve(); #1 #2 #n 如何利用 list 來模擬 ( 實作 ) stack 與 queue stack queue retrieve #1 store retrieve #1 #2 #n store #n

繼承示意圖 class list list { virtual void store(int i) i) ; virtual int intretrieve() ; class stack: public list list { void store(int i) i) ; int intretrieve() ; class queue: public list list { void store(int i) i) ; int intretrieve() ;

10-3 更多虛擬函數的細節 純粹虛擬函數 (pure virtual function) class printer { string filename ; public: void reset() { virtual void print(int m)=0 ; 抽象類別 (abstract class) 當類別至少含有一個純粹虛擬函數時 不能用來產生物件 e.g. printer p ; //XX

純粹虛擬函數的內容 純粹虛擬函數 (pure virtual function) class printer { string filename ; public: virtual void reset()=0; virtual void print(int mode)=0 ;

純粹虛擬函數的特性 衍生類別一定要 override 基底類別中所有的純粹虛擬函數 class printer {...; class HPLaserJet6L: public printer { ; void reset() { 自己的版本 void print(int mode) { 自己的版本

抽象類別的用途 ( 一 ) 設計共同的使用介面的類別 ( 衍生類別負責實作 ) class shape { string name ; public: virtual void draw(char b[][80])=0 ; // 不必有實作 virtual void clear()=0 ; // 不必有實作 ; class triangle: public shape { class circle: public shape { ;

抽象類別的用途 ( 二 ) 防止使用者產生不允許存在的物件 class shape { string name ; public: virtual void draw()=0 ; ; class triangle: public shape { class circle: public shape { ; void main() { shape s ; /* what??? */..

10-4 應用多型 早期繫結 (early binding) 或編譯時期繫結 (compiling time binding) 一般函數 超載函數 夥伴函數 非虛擬之成員函數 晚期繫結 (late binding) 或執行時期繫結 (run-time binding) 虛擬函數 ( 效率較差 )

例子 early binding void fun(int x) { cout << x; void main() { fun(5) ; // early binding late binding // 承前例 void move(car& c) { c.move() ; // late binding void main() { Benz b; Volvo v ; int x ; cin >> x ; if (x%2) b.move();..

繼承與 Code Reuse( 主動 ) Q: 設計一物件導向資料庫 : 儲存學校學生資料 [ 先修生 ] 學號系級高中校名 [ 僑生 ] 學號系級 e-mail 國籍 [ 交換學生 ] 學號系級 e-mail 國籍 [ 一般生 ] 學號系級 e-mail 選課 () 註冊 () 選課 () 註冊 () 工讀 () 選課 () 註冊 () 定期約談 () 選課 () 註冊 () 工讀 ()

練習 class student { protected: string studid, name, email ; public: void filldata()=0 ; void getid() { return ID; void show()=0 ; class LocalStudent: public student { string ID ; class AbroadStudent: public student { string passportid; string country ;

續 class IMStudents { const int MAX_STUD ; student *stud[720] ; public: students(string filename= ):MAX_STUD(720) { void adddata(string ID) ; int search(string ID) ; void main() { IMStudents ims( Imdata.txt ) ; while (true) {