Microsoft PowerPoint - 09_Inheritance.ppt

Similar documents
Microsoft PowerPoint - 10_Polymophism_1.ppt

Microsoft Word - chap10.doc

Strings

Microsoft PowerPoint - 11_Templates.ppt

Strings

Microsoft PowerPoint - L17_Inheritance_v4.pptx

第七讲 继承与多态

運算子多載 Operator Overloading

c_cpp

Microsoft PowerPoint - P766Ch06.ppt

untitled

Microsoft Word - ch04三校.doc

新版 明解C++入門編

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

Chapter 9: Objects and Classes

untitled

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

Microsoft PowerPoint - 13_Exception.ppt

Chapter12 Derived Classes

FY.DOC

第七章 繼承

PowerPoint Presentation

運算子多載 Operator Overloading

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("%

第3章.doc

Microsoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式]

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Microsoft Word - 第7章 类与对象.docx

The Embedded computing platform

Adobe® Flash® 的 Adobe® ActionScript® 3.0 程式設計

沒有投影片標題

第十章 虛擬函數 (Virtual Functions)

C/C++语言 - C/C++数据

提问袁小兵:

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

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

Microsoft Word - 投影片ch11

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

<4D F736F F D D313032ABFCA6D2A4BDA5C1BB50AAC0B77CB8D5C34428A977BD5A29>

Factory Methods

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

C++ 程式設計

運算子多載 Operator Overloading

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft Word - JAVA Programming Language Homework I ans

untitled

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

Microsoft PowerPoint - Class5.pptx

untitled

《二十一世紀》網絡版

C 1

Microsoft Word - 0.5bh.doc

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

山东建筑大学学分制管理规定(试行)

untitled

Microsoft Word cppFinalSolution.doc

Microsoft PowerPoint - 13_ClassAndObj.ppt

软件工程文档编制

ebook55-13

Microsoft PowerPoint - plan08.ppt

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

投影片 1

ebook39-6

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

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

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

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

Microsoft Word - HKU Talk doc

1.5招募说明书(草案)

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

ebook39-5

踏出C++的第一步

Microsoft PowerPoint - ch_12 [唯讀]

點 線 圓 本節性質與公式摘要 1 圓的切線 : 兩圓位置關係與公切線數量 : O 1 r 1 O 2 r 2 r 1 r 2 O 1 O 2 r 1 r 2 O 1 O 2 r 1 r O 1 O 2 r 1 r r 1 r 2 O 1 O 2 r

第二章 簡介類別

PowerPoint Presentation

(procedure-oriented)?? 2

CC213

投影片 1

CC213

mvc

Microsoft PowerPoint - 10 模板 Template.pptx

Microsoft PowerPoint - chap08.ppt

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

Chapter 8 - Operator Overloading

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

Microsoft Word - chap12.doc

北 京 师 范 大 学 庆 祝 抗 日 战 争 胜 利 70 周 年 暨 纪 念 12.9 运 动 80 周 年 歌 咏 比 赛 掠 影 党 委 书 记 刘 川 生 指 挥 唱 校 歌 党 委 书 记 刘 川 生 分 别 为 获 得 一 等 奖 和 获 得 精 神 风 貌 奖 单 位 颁 奖 党 委

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

Template

第1章

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

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

Microsoft PowerPoint - 14Threads.ppt

文档 3

bingdian001.com

untitled

用户大会 论文集2.2.doc

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

Transcription:

1 第九章 - 繼承 (Inheritance) 9.1 簡介 9.2 基本類別與衍生類別 9.3 受保護的成員 9.4 基本類別與衍生類別的關係 9.5 案例研究 : 三層繼承架構 9.6 衍生類別的建構子與解構子 9.8 public, protected, 與 private 繼承關係 9.9 繼承的優點 2 9.1 簡介 不同類別之間的關係 合成關係與繼承關係 合成關係 ( has-a ) 物件內含一或多個其它類別的物件 例如 : 車子有多個輪子 輪子屬於輪子物件 1

3 9.1 簡介 不同類別之間的關係 合成關係與繼承關係 繼承關係 ( is-a ) 衍生類別 is-a 基礎類別 例如 : 猴子是一種動物 可以把猴子看成動物 猴子具有動物的屬性與行為 4 繼承 (Inheritance) 軟體重複使用 9.1 簡介 使用舊的類別建構新類別 站在現有的基礎上 可擁有現有類別的資料 (Data) 與行為 (Behavior) 可改寫現有類別的資料 (Data) 與行為 (Behavior) 可外加新的資料 (Data) 與行為 (Behavior) 2

5 9.1 簡介 衍生類別 (Derived class) 繼承自基礎類別 (base class) 可原封不動繼承基礎類別的行為 可修改現有行為 可加入新的行為 基礎類別 車子 衍生類別 汽車 6 類別繼承關係 9.1 簡介 直接 (Direct) 繼承 間接 (Indirect) 繼承 單一繼承 (Single inheritance) 繼承自單一個基礎類別 多重繼承 (Multiple inheritance) 繼承自多個基礎類別 學生 直接繼承車子 間接繼承車子 員工 車子 汽車 卡車 工讀生 3

7 9.1 簡介 三種繼承關係 (9.8 有詳細介紹 ) public 每個衍生類別的物件也是基礎類別的物件動物 例如 : 猴子的基礎類別是動物猴子 猴子是一種動物, 但動物不是一種猴子 衍生類別可存取基礎類別的非 private 成員 要存取 Private 成員須透過所繼承的非 Private 成員函式 private 只能使用基礎類別的 public 成員 protected 8 9.2 基礎類別與衍生類別 被繼承的類別稱為基礎類別 (Base Class) 繼承別人的類別稱為衍生類別 (Derived class) 衍生類別的物件也是基礎類別的物件 例如 : 蘋果類別繼承自類別繼承自水果 蘋果的物件也是水果的物件 水果是基礎類別 蘋果是衍生類別 水果 蘋果 4

9 9.2 基礎類別與衍生類別 基礎類別包含的範圍比衍生類別廣 例如 : 基礎類別 : 載具 (Vehicle) 汽車, 卡車, 船, 腳踏車, 衍生類別 : 汽車 (Car) 雙門轎車, 四門轎車, 跑車,... 10 繼承的例子 9.2 基礎類別與衍生類別 基礎類別學生型狀貸款員工帳戶 衍生類別 研究生大學生 圓型三角型四邊型 汽車貸款房屋改善貸款房屋貸款 教員職員支票帳戶活儲帳戶 5

11 9.2 基礎類別與衍生類別 繼承階層圖 (Inheritance hierarchy) 繼承關係 : 樹狀階層圖 每個類別可以是 基礎類別 供其它類別繼承 提供資料與行為給其衍生類別 衍生類別 繼承其它類別 可使用其基礎類別的資料與行為 12 Fig. 9.2 大學社群繼承階層圖 CommunityMember( 社區成員 ) Employee( 員工 ) Student( 學生 ) Alumnus( 校友 ) 單一繼承 Faculty( 教職員 ) Staff( 工作人員 ) 單一繼承 Administrator( 行政人員 ) Teacher( 老師 ) 單一繼承 AdministratorTeacher( 行政教師 ) 多重繼承 6

13 Fig. 9.3 型狀繼承階層圖 Shape( 圖型 ) TwoDimensionalShape(2 維圖型 ) ThreeDimensionalShape (3 維圖型 ) Circle( 圓 ) Square( 正方型 ) Triangle( 三角型 ) Sphere( 球 ) Cube( 立方體 ) Tetrahedron( 四面體 ) 14 9.2 基礎類別與衍生類別 public 繼承關係 語法 : Class TwoDimensionalShape : public Shape 類別 TwoDimensionalShape 繼承類別 Shape 使用 public 繼承關係 7

15 9.2 基礎類別與衍生類別 public 繼承關係 基礎類別的 public 與 protected 成員 有繼承, 可直接存取 基礎類別的 private 成員 有繼承, 無法直接存取 必須透過成員函式存取 friend 函式 沒有繼承 16 9.3 受保護的 (Protected) 成員 protected 保護效果介於 public 與 private protected 成員可被 類別本身的成員函式存取 類別的 friend 存取 衍生類別成員函式存取 這點跟 private 不同 衍生類別的 friend 存取 8

17 9.4 基礎類別與衍生類別的關係 範例程式 : 點與圓 (Point/circle) 的繼承關係 Point x 座標 y 座標 Circle x 座標 y 座標 Radius ( 半徑 ) 1 // Fig. 9.4: point.h 2 // Point 類別定義, 可供存放 x,y 座標 3 #ifndef POINT_H 4 #define POINT_H 5 6 class Point { 7 8 public: 9 Point( int = 0, int = 0 ); // 內定建構子 10 11 void setx( int ); // 設定 x 座標的值 12 int getx() const; // 傳回 x 座標的值 13 14 void sety( int ); // 設定 y 座標的值 15 int gety() const; // 傳回 y 座標的值 16 17 void print() const; // 印出 Point 物件 18 19 private: 20 int x; // x 座標 21 int y; // y 座標 22 23 }; // end class Point 24 25 #endif point.h 18 9

1 // Fig. 9.5: point.cpp 2 // Point 類別成員函式實作 3 #include <iostream> 4 5 using std::cout; 6 7 #include point.h // 引用 Point 類別的定義 8 9 // 定義 Point 的建構子 10 Point::Point( int xvalue, int yvalue ) 11 { 12 x = xvalue; 13 y = yvalue; 14 15 } // end Point constructor 16 17 // 設定 x 座標 18 void Point::setX( int xvalue ) 19 { 20 x = xvalue; // 將 x 座標的值設定成 xvalue, 沒有檢查動作 21 22 } // end function setx 23 19 point.cpp (1 of 2) 24 // 取得 x 座標的值 25 int Point::getX() const 26 { 27 return x; 28 29 } // end function getx 30 31 // 設定 y 座標的值 32 void Point::setY( int yvalue ) 33 { 34 y = yvalue; // 將 y 座標的值設定成 yvalue, 沒有檢查動作 35 36 } // end function sety 37 38 // 傳回 y 座標的值 39 int Point::getY() const 40 { 41 return y; 42 43 } // end function gety 44 45 // 印出 Point 物件 46 void Point::print() const 47 { 48 cout << '[' << x << ", " << y << ']'; 49 50 } // end function print 20 point.cpp (2 of 2) 10

1 // Fig. 9.6: pointtest.cpp 2 // 測試 Point 類別. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include point.h // 引用 Point 類別的定義 9 10 int main() 11 { 12 Point point( 72, 115 ); // 建立一個 Point 物件 13 14 // 顯示點的座標 15 cout << "X coordinate is " << point.getx() 16 << "\ny coordinate is " << point.gety(); 17 18 point.setx( 10 ); // 設定 x 座標 19 point.sety( 10 ); // 設定 y 座標 20 21 // 顯示目前的點座標 22 cout << "\n\nthe new location of point is "; 23 point.print(); 24 cout << endl; 25 pointtest.cpp (1 of 2) 21 26 return 0; // 程式正常結束 27 28 } // end main X coordinate is 72 Y coordinate is 115 The new location of point is [10, 10] pointtest.cpp (2 of 2) pointtest.cpp output (1 of 1) 22 11

1 // Fig. 9.7: circle.h 2 // Circle 類別的定義 3 #ifndef CIRCLE_H 4 #define CIRCLE_H 5 6 class Circle { 7 8 public: 9 10 // 內定建構子 11 Circle( int = 0, int = 0, double = 0.0 ); 12 13 void setx( int ); // 設定圓心的 x 座標 14 int getx() const; // 傳回圓心的 x 座標 15 16 void sety( int ); // 設定圓心的 y 座標 17 int gety() const; // 傳回圓心的 y 座標 18 19 void setradius( double ); // 設定半徑 20 double getradius() const; // 傳回半徑 21 22 double getdiameter() const; // 傳回直徑 23 double getcircumference() const; // 傳回圓周長度 24 double getarea() const; // 傳回面積 25 circle.h (1 of 2) 23 26 void print() const; // 印出 Circle 內容 27 28 private: 29 int x; // 圓心的 x 座標 30 int y; // 圓心的 x 座標 31 double radius; // 半徑 32 33 }; // end class Circle 34 35 #endif circle.h (2 of 2) 24 12

1 // Fig. 9.8: circle.cpp 2 // Circle 類別的成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include circle.h // 引用 Circle 類別定義 8 9 // 內定建構子 10 Circle::Circle( int xvalue, int yvalue, double radiusvalue ) 11 { 12 x = xvalue; 13 y = yvalue; 14 setradius( radiusvalue ); 15 16 } // end Circle constructor 17 18 // 設定圓心的 x 座標 19 void Circle::setX( int xvalue ) 20 { 21 x = xvalue; // 設定圓心的 x 座標為 xvalue, 沒有檢查動作 22 23 } // end function setx 24 25 circle.cpp (1 of 4) 25 // 取得圓心的 x 座標 26 int Circle::getX() const 27 { 28 return x; 29 30 } // end function getx 31 32 // 設定圓心的 y 座標 33 void Circle::setY( int yvalue ) 34 { 35 y = yvalue; // 設定圓心的 y 座標為 yvalue, 沒有檢查動作 36 37 } // end function sety 38 39 // 取得圓心的 y 座標 40 int Circle::getY() const 41 { 42 return y; 43 44 } // end function gety 45 26 circle.cpp (2 of 4) 13

46 // 設定半徑 47 void Circle::setRadius( double radiusvalue ) 48 { // 檢查半徑, 如果半徑小於 0 則設定半徑為 0 49 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 50 51 } // end function setradius 52 53 // 傳回半徑 return radius 54 double Circle::getRadius() const 55 { 56 return radius; 57 58 } // end function getradius 59 60 // 計算及傳回直徑 61 double Circle::getDiameter() const 62 { 63 return 2 * radius; 64 65 } // end function getdiameter 66 27 circle.cpp (3 of 4) 67 // 計算及傳回圓周長度 68 double Circle::getCircumference() const 69 { 70 return 3.14159 * getdiameter(); //2πr 71 72 } // end function getcircumference 73 74 // 計算及傳回面積 75 double Circle::getArea() const 76 { 77 return 3.14159 * radius * radius; //πr 2 78 79 } // end function getarea 80 81 // 印出圓的內容 82 void Circle::print() const 83 { 84 cout << "Center = [" << x << ", " << y << ']' 85 << "; Radius = " << radius; 86 87 } // end function print 28 circle.cpp (4 of 4) 14

1 // Fig. 9.9: circletest.cpp 2 // Circle 類別測試程式. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> 10 11 using std::setprecision; 12 13 #include circle.h // Circle 類別定義 14 15 int main() 16 { 17 Circle circle( 37, 43, 2.5 ); // 建立一個 Circle 物件 18 19 // 印出圓心座標與半徑 20 cout << "X coordinate is " << circle.getx() 21 << "\ny coordinate is " << circle.gety() 22 << "\nradius is " << circle.getradius(); 23 circletest.cpp (1 of 2) 29 24 circle.setx( 2 ); // 設定新的圓心 x 座標 25 circle.sety( 2 ); // 設定新的圓心 y 座標 26 circle.setradius( 4.25 ); // 設定新的半徑 27 28 // 使用 print() 函式印出圓的內容 29 cout << "\n\nthe new location and radius of circle are\n"; 30 circle.print(); 31 32 // 設定印出數值的精確度到小數以下第 2 位 33 cout << fixed << setprecision( 2 ); 34 35 // 印出圓的直徑 36 cout << "\ndiameter is " << circle.getdiameter(); 37 38 // 印出圓周長度 39 cout << "\ncircumference is " << circle.getcircumference(); 40 41 // 印出圓的面積 42 cout << "\narea is " << circle.getarea(); 43 44 cout << endl; 45 46 return 0; // 程式正常結束 47 48 } // end main circletest.cpp (2 of 2) 30 15

// 印出圓心座標及半徑 X coordinate is 37 Y coordinate is 43 Radius is 2.5 // 使用 print() 函式印出圓的內容 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 circletest.cpp output (1 of 1) 31 // 印出圓的直徑 Diameter is 8.50 // 印出圓周長度 Circumference is 26.70 // 印出圓的面積 Area is 56.74 32 使用繼承 Point 的方式實作 Circle 類別 錯誤版本 16

1 // Fig. 9.10: circle2.h 2 // Circle2 類別定義 3 #ifndef CIRCLE2_H 4 #define CIRCLE2_H 5 6 #include point.h // 引用 Point 類別定義 7 8 class Circle2 : public Point { 9 10 public: 11 12 // 內定建構子 13 Circle2( int = 0, int = 0, double = 0.0 ); 14 15 void setradius( double ); // 設定半徑 16 double getradius() const; // 取得半徑 17 18 double getdiameter() const; // 取得直徑 19 double getcircumference() const; // 取得圓周長度 20 double getarea() const; // 取得圓面積 21 22 void print() const; // 印出 Circle2 物件內容 23 24 private: 25 double radius; // Circle2 半徑 26 27 }; // end class Circle2 28 29 #endif circle2.h 33 1 // Fig. 9.11: circle2.cpp 2 // Circle2 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include circle2.h // Circle2 類別定義 8 9 // 內定建構子 10 Circle2::Circle2(int xvalue,int yvalue,double radiusvalue) 11 { 12 x = xvalue; // 設定 x 座標值, 不能直接設定 Point 物件的 private 成員 13 y = yvalue; // 設定 y 座標值, 不能直接設定 Point 物件的 private 成員 14 setradius( radiusvalue ); 15 16 } // end Circle2 constructor 17 18 // 設定半徑 19 void Circle2::setRadius( double radiusvalue ) 20 { 21 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 22 23 } // end function setradius circle2.cpp (1 of 3) 34 17

24 25 // 取得半徑 26 double Circle2::getRadius() const 27 { 28 return radius; 29 30 } // end function getradius 31 32 // 計算及傳回直徑 33 double Circle2::getDiameter() const 34 { 35 return 2 * radius; 36 37 } // end function getdiameter 38 39 // 計算及傳回圓周長度 40 double Circle2::getCircumference() const 41 { 42 return 3.14159 * getdiameter(); 43 44 } // end function getcircumference circle2.cpp (2 of 3) 35 45 46 // 計算及傳回圓面積 47 double Circle2::getArea() const 48 { 49 return 3.14159 * radius * radius; 50 51 } // end function getarea 52 53 // 印出 Circle2 物件 54 void Circle2::print() const 55 { // 不能直徑存取 Point 物件的 Private 成員 56 cout << "Center = [" << x << ", " << y << ']' 57 << "; Radius = " << radius; 58 59 } // end function print circle2.cpp (3 of 3) 36 18

C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(12) : error C2248: 'x' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(20) : see declaration of 'x' 12 x = xvalue; circle2.cpp output (1 of 1) 37 C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(13) : error C2248: 'y' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(21) : see declaration of 'y' 13 y = yvalue; C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(56) : error C2248: 'x' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(20) : see declaration of 'x' 56 cout << "Center = [" << x << ", " << y << ']' C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(56) : error C2248: 'y' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(21) : see declaration of 'y' 56 cout << "Center = [" << x << ", " << y << ']' 38 使用繼承 Point 的方式實作 Circle 類別 正確版本 - 使用 Protected 19

1 // Fig. 9.12: point2.h 2 // Point2 類別定義 3 #ifndef POINT2_H 4 #define POINT2_H 5 6 class Point2 { 7 8 public: 9 Point2( int = 0, int = 0 ); // 內定建構子 10 11 void setx( int ); // 設定 x 座標 12 int getx() const; // 取得 x 座標 13 14 void sety( int ); // 設定 y 座標 15 int gety() const; // 取得 y 座標 16 17 void print() const; // 印出 Point2 物件內容 18 19 protected: 20 int x; // x 座標 21 int y; // y 座標 22 23 }; // end class Point2 24 25 #endif point2.h (1 of 1) 39 1 // Fig. 9.13: point2.cpp 2 // Point2 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include point2.h // 引用 Point2 類別定義 8 9 // 內定建構子 10 Point2::Point2( int xvalue, int yvalue ) 11 { 12 x = xvalue; 13 y = yvalue; 14 15 } // end Point2 constructor 16 17 // 設定 x 座標 18 void Point2::setX( int xvalue ) 19 { 20 x = xvalue; 21 22 } // end function setx 23 point2.cpp (1 of 2) 40 20

24 // 取得 x 座標的值 25 int Point2::getX() const 26 { 27 return x; 28 29 } // end function getx 30 31 // 設定 y 座標 32 void Point2::setY( int yvalue ) 33 { 34 y = yvalue; 35 36 } // end function sety 37 38 // 取得 y 座標的值 39 int Point2::getY() const 40 { 41 return y; 42 43 } // end function gety 44 45 // 印出 Point2 物件內容 46 void Point2::print() const 47 { 48 cout << '[' << x << ", " << y << ']'; 49 50 } // end function print point2.cpp (2 of 2) 41 1 // Fig. 9.14: circle3.h 2 // Circle3 類別定義 3 #ifndef CIRCLE3_H 4 #define CIRCLE3_H 5 6 #include point2.h // 引用 Point2 類別定義 7 8 class Circle3 : public Point2 { 9 10 public: 11 12 // 內定建構子 13 Circle3( int = 0, int = 0, double = 0.0 ); 14 15 void setradius( double ); // 設定半徑 16 double getradius() const; // 取得半徑 17 18 double getdiameter() const; // 傳回直徑 19 double getcircumference() const; // 傳回圓周長 20 double getarea() const; // 傳回面積 21 22 void print() const; // 印出 Circle3 物件 23 26 27 }; // end class Circle3 28 24 private: 25 double radius; // 半徑 注意 :Circle3 沒有定義 1.x,y 2.setX, sety, getx, gety 29 #endif circle3.h 42 21

1 // Fig. 9.15: circle3.cpp 2 // Circle3 類別員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include circle3.h // 引用 Circle3 類別定義 8 9 // 內定建構子 10 Circle3::Circle3(int xvalue,int yvalue,double radiusvalue) 11 { 12 x = xvalue; 13 y = yvalue; 14 setradius( radiusvalue ); 15 16 } // end Circle3 constructor 17 18 // 設定半徑 19 void Circle3::setRadius( double radiusvalue ) 20 { 21 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 22 23 } // end function setradius 24 25 // 傳回半徑 26 double Circle3::getRadius() const 27 { 28 return radius; 29 30 } // end function getradius circle3.cpp (1 of 2) 43 31 32 // 計算及傳回直徑 33 double Circle3::getDiameter() const 34 { 35 return 2 * radius; 36 37 } // end function getdiameter 38 39 // 計算及傳回圓周長 40 double Circle3::getCircumference() const 41 { 42 return 3.14159 * getdiameter(); 43 44 } // end function getcircumference 45 46 // 計算及傳回面積 47 double Circle3::getArea() const 48 { 49 return 3.14159 * radius * radius; 50 51 } // end function getarea 52 53 // 印出 Circle3 物件內容 54 void Circle3::print() const 55 { // 可直接存取 Point 的 x,y 成員 56 cout << "Center = [" << x << ", " << y << ']' 57 << "; Radius = " << radius; 58 59 } // end function print circle3.cpp (2 of 2) 44 22

1 // Fig. 9.16: circletest3.cpp 2 // Testing class Circle3 測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> 10 11 using std::setprecision; 12 13 #include circle3.h // 引用 Circle3 類別定義 14 15 int main() 16 { 17 Circle3 circle( 37, 43, 2.5 ); // 建構一個 Circle3 物件 18 19 // 印出 circle 內容 20 cout << "X coordinate is " << circle.getx() 21 << "\ny coordinate is " << circle.gety() 22 << "\nradius is " << circle.getradius(); 23 24 circle.setx( 2 ); // 設定圓心 x 座標, 使用 Point 的成員 25 circle.sety( 2 ); // 設定圓心 y 座標, 使用 Point 的成員 26 circle.setradius( 4.25 ); // 設定半徑 27 circletest3.cpp (1 of 2) 45 28 // 印出 circle 的內容 29 cout << "\n\nthe new location and radius of circle are\n"; 30 circle.print(); 31 32 // 設定輸出精確度 33 cout << fixed << setprecision( 2 ); 34 35 // 輸出直徑 36 cout << "\ndiameter is " << circle.getdiameter(); 37 38 // 輸出圓周長 39 cout << "\ncircumference is " << circle.getcircumference(); 40 41 // 輸出圓面積 42 cout << "\narea is " << circle.getarea(); 43 44 cout << endl; 45 46 return 0; // 程式正常結束 47 48 } // end main circletest3.cpp (2 of 2) 46 23

// 印出 circle 內容 X coordinate is 37 Y coordinate is 43 Radius is 2.5 // 印出 circle 的內容 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 circletest3.cpp output (1 of 1) 47 // 輸出直徑 Diameter is 8.50 // 輸出圓周長 Circumference is 26.70 // 輸出圓面積 Area is 56.74 48 9.4 基礎類別與衍生類別的關係 protected 資料成員的優缺點 優點 (Advantages) 衍生類別可直接存取 程式執行效率稍高 因為沒有使用 set/get 函式呼叫 24

49 9.4 基礎類別與衍生類別的關係 protected 資料成員的優缺點 缺點 (Disadvantages) 數值沒有經過 set/get 函式驗證 衍生類別直接存取可能指派不合法的值 實作相關性高 衍生類別的成員函式與基礎類別的實作相關性較高 基礎類別的實作方式改變, 衍生類別必須跟著變 50 使用繼承 Point 的方式實作 Circle 類別 正確版本 - 不使用 Protected 25

1 // Fig. 9.17: point3.h 2 // Point3 類別定義 3 #ifndef POINT3_H 4 #define POINT3_H 5 6 class Point3 { 7 8 public: 9 Point3( int = 0, int = 0 ); // 預設建構子 10 11 void setx( int ); // 設定 x 座標 12 int getx() const; // 取得 x 座標 13 14 void sety( int ); // 設定 y 座標 15 int gety() const; // 取得 y 座標 16 17 void print() const; // 印出 Point3 物件內容 18 19 private: 20 int x; // x 座標 21 int y; // y 座標 22 23 }; // end class Point3 24 25 #endif point3.h (1 of 1) 51 1 // Fig. 9.18: point3.cpp 2 // Point3 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include point3.h // Point3 類別定義 8 9 // 內定建構子 10 Point3::Point3( int xvalue, int yvalue ) 11 : x( xvalue ), y( yvalue ) 12 { 13 // 空的 14 15 } // end Point3 constructor 16 17 // 設定 x 座標 18 void Point3::setX( int xvalue ) 19 { 20 x = xvalue; 21 22 } // end function setx 23 24 // 傳回 x 座標 25 int Point3::getX() const 26 { 27 return x; 28 29 } // end function getx point3.cpp (1 of 2) 52 26

30 31 // 設定 y 座標 32 void Point3::setY( int yvalue ) 33 { 34 y = yvalue; 35 36 } // end function sety 37 38 // 傳回 y 座標 39 int Point3::getY() const 40 { 41 return y; 42 43 } // end function gety 44 45 // 印出 Point3 物件 46 void Point3::print() const 47 { 48 cout << '[' << getx() << ", " << gety() << ']'; 49 50 } // end function print point3.cpp (2 of 2) 53 1 // Fig. 9.19: circle4.h 2 // Circle4 類別定義 3 #ifndef CIRCLE4_H 4 #define CIRCLE4_H 5 6 #include point3.h // 引用 Point3 類別定義 7 8 class Circle4 : public Point3 { 9 10 public: 11 12 // 內定建構子 13 Circle4( int = 0, int = 0, double = 0.0 ); 14 15 void setradius( double ); // 設定半徑 16 double getradius() const; // 傳回半徑 17 18 double getdiameter() const; // 傳回直徑 19 double getcircumference() const; // 傳回圓周 20 double getarea() const; // 傳回面積 21 22 void print() const; // 印出 Circle4 物件內容 23 24 private: 25 double radius; // 半徑 26 27 }; // end class Circle4 28 29 #endif circle4.h 54 27

1 // Fig. 9.20: circle4.cpp 2 // Circle4 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include circle4.h // 引用 Circle4 類別定義 8 9 // 內定建構子 10 Circle4::Circle4(int xvalue,int yvalue,double radiusvalue) 11 : Point3( xvalue, yvalue ) // 呼叫基礎類別的建構子 12 { 13 setradius( radiusvalue ); 14 15 } // end Circle4 constructor 16 17 // 設定半徑 18 void Circle4::setRadius( double radiusvalue ) 19 { 20 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 21 22 } // end function setradius 23 24 // 傳回半徑 25 double Circle4::getRadius() const 26 { 27 return radius; 28 29 } // end function getradius circle4.cpp (1 of 2) 55 30 31 // 計算及傳回直徑 32 double Circle4::getDiameter() const 33 { 34 return 2 * getradius(); // 呼叫 getradius() 函式較好 35 36 } // end function getdiameter 37 38 // 計算及傳回圓周長 39 double Circle4::getCircumference() const 40 { 41 return 3.14159 * getdiameter(); 42 43 } // end function getcircumference 44 45 // 計算及傳回面積 46 double Circle4::getArea() const 47 { 48 return 3.14159 * getradius() * getradius(); 49 50 } // end function getarea 51 52 // 印出 Circle4 物件內容 53 void Circle4::print() const 54 { 55 cout << "Center = "; 56 Point3::print(); // 呼叫 Point3 的 print 函式 57 cout << "; Radius = " << getradius(); 58 59 } // end function print circle4.cpp (2 of 2) 56 28

1 // Fig. 9.21: circletest4.cpp 2 // 類別 Circle4 測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> 10 11 using std::setprecision; 12 13 #include circle4.h // Circle4 類別定義 14 15 int main() 16 { 17 Circle4 circle( 37, 43, 2.5 ); // 建構 Circle4 物件 18 19 // 印出圓的內容 20 cout << X coordinate is << circle.getx() //point 的函式 21 << "\ny coordinate is " << circle.gety() //point 的函式 22 << "\nradius is " << circle.getradius(); 23 57 circletest4.cpp (1 of 2) 24 circle.setx( 2 ); // 設定 x 座標,point 函式 25 circle.sety( 2 ); // 設定 y 座標,point 函式 26 circle.setradius( 4.25 ); // 設定圓心 27 28 // 印出圓的內容 29 cout << "\n\nthe new location and radius of circle are\n"; 30 circle.print(); 31 32 // 設定輸出精確度 33 cout << fixed << setprecision( 2 ); 34 35 // 印出 Circle4 的直徑 36 cout << "\ndiameter is " << circle.getdiameter(); 37 38 // 印出 Circle4 的圓周 39 cout << "\ncircumference is " << circle.getcircumference(); 40 41 // 印出 Circle4 的面積 42 cout << "\narea is " << circle.getarea(); 43 44 cout << endl; 45 46 return 0; // 正常結束程式 47 48 } // end main circletest4.cpp (2 of 2) 58 29

// 印出圓的內容 X coordinate is 37 Y coordinate is 43 Radius is 2.5 // 印出圓的內容 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 circletest4.cpp output 59 // 印出 Circle4 的直徑 Diameter is 8.50 // 印出 Circle4 的圓周 Circumference is 26.70 // 印出 Circle4 的面積 Area is 56.74 60 9.5 案例研究 : 三層繼承 三層繼承 point/circle/cylinder Point x,y 座標 Circle x,y 座標 Radius Cylinder x,y 座標 Radius Height 30

1 // Fig. 9.22: cylinder.h 2 // Cylinder 類別定義 3 #ifndef CYLINDER_H 4 #define CYLINDER_H 5 6 #include circle4.h // 引用 Circle4 類別定義 7 8 class Cylinder : public Circle4 { 9 10 public: 11 12 // 內定建構子 13 Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); 14 15 void setheight( double ); // 設定 Cylinder 高度 16 double getheight() const; // 傳回 Cylinder 高度 17 18 double getarea() const; // 傳回 Cylinder 面積 19 double getvolume() const; // 傳回 Cylinder 體積 20 void print() const; // 印出 Cylinder 物件內容 21 22 private: 23 double height; // Cylinder 高度 24 25 }; // end class Cylinder 26 27 #endif cylinder.h 61 1 // Fig. 9.23: cylinder.cpp 2 // Cylinder class inherits from class Circle4. 3 #include <iostream> 4 5 using std::cout; 6 7 #include cylinder.h // Cylinder 類別定義 8 9 // 內定建構子 10 Cylinder::Cylinder(int xvalue,int yvalue,double radiusvalue, 11 double heightvalue ) 12 : Circle4( xvalue, yvalue, radiusvalue ) 13 { 14 setheight( heightvalue ); 15 16 } // end Cylinder constructor 17 18 // 設定 Cylinder 高度 19 void Cylinder::setHeight( double heightvalue ) 20 { 21 height = ( heightvalue < 0.0? 0.0 : heightvalue ); 22 23 } // end function setheight 24 25 // 取得 Cylinder 高度 26 double Cylinder::getHeight() const 27 { 28 return height; 29 30 } // end function getheight cylinder.cpp (1 of 2) 62 31

31 32 // 重新定義 Circle4 的 getarea 函式, 計算 Cylinder 面積 33 double Cylinder::getArea() const 34 { 35 return 2 * Circle4::getArea() + 36 getcircumference() * getheight(); 37 38 } // end function getarea 39 40 // 計算 Cylinder 體積 41 double Cylinder::getVolume() const 42 { // 呼叫 Circle4 的 getarea 函式 43 return Circle4::getArea() * getheight(); 44 45 } // end function getvolume 46 47 // 輸出 Cylinder 物件 48 void Cylinder::print() const 49 { 50 Circle4::print();// 呼叫 Circle4 的 print 函式 51 cout << "; Height = " << getheight(); 52 53 } // end function print cylinder.cpp (2 of 2) 63 1 // Fig. 9.24: cylindertest.cpp 2 // Cylinder 測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> 10 11 using std::setprecision; 12 13 #include cylinder.h // 引用 Cylinder 類別定義 14 15 int main() 16 { 17 // 建構 Cylinder 物件 18 Cylinder cylinder( 12, 23, 2.5, 5.7 ); 19 20 // 印出 cylinder 內容 21 cout << "X coordinate is " << cylinder.getx() 22 << "\ny coordinate is " << cylinder.gety() 23 << "\nradius is " << cylinder.getradius() 24 << "\nheight is " << cylinder.getheight(); 25 26 cylinder.setx( 2 ); // 設定新的 x 座標 27 cylinder.sety( 2 ); // 設定新的 y 座標 28 cylinder.setradius( 4.25 ); // 設定新的半徑 29 cylinder.setheight( 10 ); // 設定新的高度 cylindertest.cpp (1 of 2) 64 32

30 31 // 印出新的 cylinder 內容 32 cout << "\n\nthe new location and radius of circle are\n"; 33 cylinder.print(); 34 35 // 設定輸出精確度 36 cout << fixed << setprecision( 2 ); 37 38 // 印出 cylinder 直徑 39 cout << "\n\ndiameter is " << cylinder.getdiameter(); 40 41 // 印出 cylinder 圓周長度 42 cout << "\ncircumference is " 43 << cylinder.getcircumference(); 44 45 // 印出 cylinder 面積 46 cout << "\narea is " << cylinder.getarea(); 47 48 // 印出 cylinder 體積 49 cout << "\nvolume is " << cylinder.getvolume(); 50 51 cout << endl; 52 53 return 0; // 程式正常結束 54 55 } // end main cylindertest.cpp (2 of 2) 65 // 印出 cylinder 內容 X coordinate is 12 Y coordinate is 23 Radius is 2.5 Height is 5.7 // 印出新的 cylinder 內容 The new location and radius of circle are Center = [2, 2]; Radius = 4.25; Height = 10 // 印出 cylinder 直徑 Diameter is 8.50 // 印出 cylinder 圓周長度 Circumference is 26.70 // 印出 cylinder 面積 Area is 380.53 // 印出 cylinder 體積 Volume is 567.45 cylindertest.cpp output 66 33

67 9.6 衍生類別的建構子與解構子 初始化衍生類別物件 導致一連串的建構子呼叫 衍生類別建構子會呼叫基礎類別建構子 例如 : Point3/Circle4/Cylinder 建構子呼叫順序 : Cylinder - Circle4 - Point3 建構子完成順序 : Point3 - Circle4 - Cylinder 資料成員 每個基礎類別的建構子都會初始化資料成員 提供衍生類別繼承 68 9.6 衍生類別的建構子與解構子 解構衍生類別物件 導致一連串的解構子呼叫 例如 : Point3/Circle4/Cylinder 解構子呼叫順序 : Cylinder - Circle4 - Point3 每層解構完才釋放記憶體 34

69 9.6 衍生類別的建構子與解構子 基礎類別的建構子, 解構子, 與指派運算子 (=) 不會被衍生類別繼承 衍生類別的建構子, 解構子, 與指派運算子 (=) 可呼叫 基礎類別的建構子 基礎類別的指派運算子 1 // Fig. 9.25: point4.h 2 // Point4 類別定義 3 #ifndef POINT4_H 4 #define POINT4_H 5 6 class Point4 { 7 8 public: 9 Point4( int = 0, int = 0 ); // 內定建構子 10 ~Point4(); // 解構子 11 12 void setx( int ); // 設定 x 座標 13 int getx() const; // 傳回 x 座標 14 15 void sety( int ); // 設定 y 座標 16 int gety() const; // 傳回 y 座標 17 18 void print() const; // 印出 Point3 物件內容 19 20 private: 21 int x; // x 座標 22 int y; // y 座標 23 24 }; // end class Point4 25 26 #endif point4.h (1 of 1) 70 35

1 // Fig. 9.26: point4.cpp 2 // Point4 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include point4.h // 引用 Point4 類別定義 9 10 // 內定建構子 11 Point4::Point4( int xvalue, int yvalue ) 12 : x( xvalue ), y( yvalue ) 13 { 14 cout << "Point4 constructor: "; 15 print(); 16 cout << endl; 17 18 } // end Point4 constructor 19 20 // 解構子 21 Point4::~Point4() 22 { 23 cout << "Point4 destructor: "; 24 print(); 25 cout << endl; 26 27 } // end Point4 destructor 28 point4.cpp (1 of 3) 71 29 // 設定 x 座標 30 void Point4::setX( int xvalue ) 31 { 32 x = xvalue; 33 34 } // end function setx 35 36 // 傳回 x 座標 37 int Point4::getX() const 38 { 39 return x; 40 41 } // end function getx 42 43 // 設定 y 座標 44 void Point4::setY( int yvalue ) 45 { 46 y = yvalue; 47 48 } // end function sety 49 point4.cpp (2 of 3) 72 36

50 // 傳回 y 座標 51 int Point4::getY() const 52 { 53 return y; 54 55 } // end function gety 56 57 // 印出 Point4 物件內容 58 void Point4::print() const 59 { 60 cout << '[' << getx() << ", " << gety() << ']'; 61 62 } // end function print point4.cpp (3 of 3) 73 1 // Fig. 9.27: circle5.h 2 // Circle5 類別定義 3 #ifndef CIRCLE5_H 4 #define CIRCLE5_H 5 6 #include point4.h // 引用 Point4 類別定義 7 8 class Circle5 : public Point4 { 9 10 public: 11 12 // 內定建構子 13 Circle5( int = 0, int = 0, double = 0.0 ); 14 15 ~Circle5(); // 解構子 16 void setradius( double ); // 設定半徑 17 double getradius() const; // 傳回半徑 18 19 double getdiameter() const; // 傳回直徑 20 double getcircumference() const; // 傳回圓周長度 21 double getarea() const; // 傳回面積 22 23 void print() const; // 印出 Circle5 物件內容 24 25 private: 26 double radius; // Circle5 的半徑 27 28 }; // end class Circle5 29 30 #endif circle5.h (1 of 2) 74 37

1 // Fig. 9.28: circle5.cpp 2 // Circle5 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include circle5.h // 引用 Circle5 類別定義 9 10 // 內定建構子 11 Circle5::Circle5(int xvalue, int yvalue, double radiusvalue) 12 : Point4( xvalue, yvalue ) // 呼叫基礎類別建構子 13 { 14 setradius( radiusvalue ); 15 16 cout << "Circle5 constructor: "; 17 print(); 18 cout << endl; 19 20 } // end Circle5 constructor 21 22 // 解構子 23 Circle5::~Circle5() 24 { 25 cout << "Circle5 destructor: "; 26 print(); 27 cout << endl; 28 29 } // end Circle5 destructor 30 circle5.cpp (1 of 3) 75 31 // 設定半徑 32 void Circle5::setRadius( double radiusvalue ) 33 { 34 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 35 36 } // end function setradius 37 38 // 傳回半徑 39 double Circle5::getRadius() const 40 { 41 return radius; 42 43 } // end function getradius 44 45 // 計算及傳回直徑 46 double Circle5::getDiameter() const 47 { 48 return 2 * getradius(); 49 50 } // end function getdiameter 51 52 // 計算及傳回圓周長度 53 double Circle5::getCircumference() const 54 { 55 return 3.14159 * getdiameter(); 56 57 } // end function getcircumference 58 circle5.cpp (2 of 3) 76 38

59 // 計算及傳回面積 60 double Circle5::getArea() const 61 { 62 return 3.14159 * getradius() * getradius(); 63 64 } // end function getarea 65 66 // 印出 Circle5 物件 67 void Circle5::print() const 68 { 69 cout << "Center = "; 70 Point4::print(); // 呼叫 Point4 的 print 函式 71 cout << "; Radius = " << getradius(); 72 73 } // end function print circle5.cpp (3 of 3) 77 1 // Fig. 9.29: fig09_29.cpp 2 // 基礎類別與衍生類別建構子與解構子呼叫順序測試程式 3 // 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 #include circle5.h // 引用 Circle5 類別定義 10 11 int main() 12 { 13 { // 建構 Point4 物件 14 15 Point4 point( 11, 22 ); 16 17 } // 解構 Point4 物件 18 19 cout << endl; 20 Circle5 circle1( 72, 29, 4.5 ); // 建構 Circle5 物件 21 22 cout << endl; 23 Circle5 circle2( 5, 5, 10 ); // 建構 Circle5 物件 24 25 cout << endl; 26 27 return 0; 28 29 } // end main fig09_29.cpp 78 39

Point4 constructor: [11, 22] // 建構 Point4 物件 Point4 destructor: [11, 22] // 解構 Point4 物件 // 建構 circle1 Point4 constructor: [72, 29] Circle5 constructor: Center = [72, 29]; Radius = 4.5 fig09_29.cpp output 79 // 建構 circle2 Point4 constructor: [5, 5] Circle5 constructor: Center = [5, 5]; Radius = 10 // 解構 circle2 Circle5 destructor: Center = [5, 5]; Radius = 10 Point4 destructor: [5, 5] // 解構 circle1 Circle5 destructor: Center = [72, 29]; Radius = 4.5 Point4 destructor: [72, 29] 80 9.8 public, protected 與 private 繼承關係 衍生類別 Public Protected Private 基礎類別 Public Protected Private Public Protected Private Protected Protected Private Hidden Hidden Hidden 40

81 9.9 繼承的優點 可利用繼承現有類別的方式 重新定義基礎類別的成員函式 加入額外資料或行為 間接存取基礎類別的程式碼 Link 到原程式的 object code 82 個別的軟體廠商 9.9 繼承的優點 可開發與販售自有程式 可提供 object-code 型式 使用者可使用衍生類別的方式 不需要使用到原程式碼 41