Microsoft PowerPoint - 10_Polymophism_1.ppt
|
|
- 允 求
- 6 years ago
- Views:
Transcription
1 1 第十章 - 多型 (Polymorphism) 10.1 簡介 10.2 物件在繼承階層中的關係 10.3 多型範例程式 10.4 Type 欄位與 switch 結構 10.5 抽象類別 (Abstract Class) 10.6 案例研讀 : 繼承介面與實作 10.7 多型, 虛擬函式與動態繫結 10.8 虛擬建構子 10.9 案例研究 : 使用多型實作薪資系統 簡介 多型 (Polymorphism) 將衍生類別物件當成基礎類別處理 使用虛擬函式 (Virtual functions) 與動態繫結 (dynamic binding) 讓程式具可擴充性 新的類別很容易加入, 而且能夠被處理 本章將解釋多型如何運作 1
2 簡介 本章將使用兩個例子說明多型的用法 1. 使用抽象 (abstract) 基礎類別 Shape 定義共用介面 (functionality) 衍生類別 :Point, Circle 與 Cylinder 2. 使用 Employee 類別 衍生類別 : Salaried ( 固定薪水 ) Hourly ( 時薪 ) Commission ( 按件抽成 ) Base-plus-commission ( 底薪 + 抽成 ) 物件在繼承階層中的關係 物件指標的用法 基礎指標指到基礎類別 衍生指標指到衍生物件 基礎指標指到衍生物件 Circle 是一種 Point 可叫用 Circle 的基礎類別函式 函式呼叫與指標種類有關 2
3 物件在繼承階層中的關係 範例程式 1. 基礎類別指標指到基礎類別 2. 衍生類別指標指到衍生類別 3. 基礎類別指標指到衍生類別 1 // Fig. 10.1: point.h 2 // Point 類別定義 3 #ifndef POINT_H 4 #define POINT_H 5 6 class Point { 7 8 public: 9 Point( int = 0, int = 0 ); // 內定建構子 void setx( int ); // 設定 X 軸座標 12 int getx() const; // 傳回 X 軸座標 void sety( int ); // 設定 y 軸座標 15 int gety() const; // 傳回 y 軸座標 void print() const; // 輸出 Point 物件內容 private: 20 int x; // 存放 x 軸的值 21 int y; // 存放 y 軸的值 }; // end class Point #endif point.h (1 of 1) 6 3
4 1 // Fig. 10.2: point.cpp 2 // Point 類別成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include point.h // Point 類別定義 8 9 // 內容建構子 10 Point::Point( int xvalue, int yvalue ) 11 : x( xvalue ), y( yvalue ) 12 { } // end Point constructor // 設定 x 座標 18 void Point::setX( int xvalue ) 19 { 20 x = xvalue; } // end function setx 23 point.cpp (1 of 2) 7 24 // 傳回 x 座標 25 int Point::getX() const 26 { 27 return x; } // end function getx // 設定 y 座標 32 void Point::setY( int yvalue ) 33 { 34 y = yvalue; } // end function sety // 傳回 y 座標 39 int Point::getY() const 40 { 41 return y; } // end function gety // 印出 Point 物件內容 46 void Point::print() const 47 { 48 cout << '[' << getx() << ", " << gety() << ']'; } // end function print point.cpp (2 of 2) 8 4
5 1 // Fig. 10.3: circle.h 2 // Circle 類別定義 3 #ifndef CIRCLE_H 4 #define CIRCLE_H 5 6 #include point.h // 引用 Point 類別定義 7 8 class Circle : public Point { 9 10 public: // 內定建構子 13 Circle( int = 0, int = 0, double = 0.0 ); void setradius( double ); // 設定半徑 16 double getradius() const; // 傳回半徑 double getdiameter() const; // 傳回直徑 19 double getcircumference() const; // 傳回圓周長 20 double getarea() const; // 傳回面積 void print() const; // 印出 Circle 物件 private: 25 double radius; // 存放 Circle 圓心 }; // end class Circle #endif circle.h (1 of 1) 9 1 // Fig. 10.4: 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 : Point( xvalue, yvalue ) // 呼叫基礎類別建構子 12 { 13 setradius( radiusvalue ); } // end Circle constructor // 設定半徑 18 void Circle::setRadius( double radiusvalue ) 19 { 20 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); } // end function setradius circle.cpp (1 of 3) 5
6 24 // 傳回半徑 25 double Circle::getRadius() const 26 { 27 return radius; } // end function getradius // 計算及傳回直徑 32 double Circle::getDiameter() const 33 { 34 return 2 * getradius(); } // end function getdiameter // 計算及傳回圓周長 39 double Circle::getCircumference() const 40 { 41 return * getdiameter(); } // end function getcircumference // 計算及傳回面積 46 double Circle::getArea() const 47 { 48 return * getradius() * getradius(); } // end function getarea 11 circle.cpp (2 of 3) // 印出 Circle 物件內容 53 void Circle::print() const 54 { 55 cout << "center = "; 56 Point::print(); // 呼叫 Point 的列印函式 57 cout << "; radius = " << getradius(); } // end function print 12 circle.cpp (3 of 3) 6
7 1 // Fig. 10.5: fig10_05.cpp 2 // 使用基礎類別與衍生類別指標 3 // 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 using std::fixed; 9 10 #include <iomanip> using std::setprecision; #include point.h // 引用 Point 類別定義 15 #include circle.h // 引用 Circle 類別定義 int main() 18 { 19 Point point( 30, 50 ); 20 Point *pointptr = 0; // 基礎類別指標 Circle circle( 120, 89, 2.7 ); 23 Circle *circleptr = 0; // 衍生類別指標 24 fig10_05.cpp (1 of 3) // 設定浮點數格式 26 cout << fixed << setprecision( 2 ); // 印出 point 與 circle 物件內容 29 cout << "Print point and circle objects:" 30 << "\npoint: "; 31 point.print(); // 呼叫 Point 的 print 函式 32 cout << "\ncircle: "; 33 circle.print(); // 呼叫 Circle 的 print 函式 // 將 pointptr 指標指到 point 物件, 並印出 point 內容 36 pointptr = &point; 37 cout << "\n\ncalling print with base-class pointer to " 38 << "\nbase-class object invokes base-class print " 39 << "function:\n"; 40 pointptr->print(); // 呼叫 Point 的 print 函式 // 將 circleptr 指標指到 circle 物件, 並印出 circle 內容 43 // 44 circleptr = &circle; 45 cout << "\n\ncalling print with derived-class pointer to " 46 << "\nderived-class object invokes derived-class " 47 << "print function:\n"; 48 circleptr->print(); // 呼叫 Circle 的 print 函式 49 fig10_05.cpp (2 of 3) 14 7
8 50 // 將 pointptr 指標指到 circle 物件, 並印出內容 51 pointptr = &circle; 52 cout << "\n\ncalling print with base-class pointer to " 53 << "derived-class object\ninvokes base-class print " 54 << "function on that derived-class object:\n"; 55 pointptr->print(); // 呼叫 Point 的 print 函式 56 cout << endl; return 0; } // end main fig10_05.cpp (3 of 3) 15 // 印出 point 與 circle 物件內容 Print point and circle objects: Point: [30, 50] Circle: center = [120, 89]; radius = 2.70 // 將 pointptr 指標指到 point 物件, 並印出 point 內容 Calling print with base-class pointer to base-class object invokes base-class print function: [30, 50] fig10_05.cpp output (1 of 1) 16 // 將 circleptr 指標指到 circle 物件, 並印出 circle 內容 Calling print with derived-class pointer to derived-class object invokes derived-class print function: center = [120, 89]; radius = 2.70 // 將 pointptr 指標指到 circle 物件, 並印出內容 Calling print with base-class pointer to derived-class object invokes base-class print function on that derived-class object: [120, 89] 8
9 物件在繼承階層中的關係 將衍生類別看成基礎類別 例如 : 將 Circle 看成 Point 將基礎類別看成衍生類別 編譯器錯誤 Point 不是一種 Circle Circle 含有一些 Point 沒有的東西 如 Radius, setradius() 可利用 dynamic_cast(10.9 節介紹 ) 1 // Fig. 10.6: fig10_06.cpp 2 // 將衍生類別的指標的指到基礎類別的物件 3 #include "point.h" // 引用 Point 類別定義 4 #include "circle.h" // 引用 Circle 類別定義 5 6 int main() 7 { 8 Point point( 30, 50 ); 9 Circle *circleptr = 0; // 將衍生類別的指標的指到基礎類別的物件 12 circleptr = &point; // 錯誤 : Point 不是 Circle return 0; } // end main fig10_06.cpp (1 of 1) fig10_06.cpp output (1 of 1) 18 C:\cpphtp4\examples\ch10\fig10_06\Fig10_06.cpp(12) : error C2440: '=' : cannot convert from 'class Point *' to 'class Circle *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 9
10 物件在繼承階層中的關係 透過基礎類別的指標呼叫衍生類別的成員函式 基礎類別的指標可指到衍生類別 但只能呼叫基礎類別的成員函式 呼叫衍生類別函式會產生編譯錯誤 因為函式沒有定義在基礎類別中 資料型態決定所能呼叫的函式 1 // Fig. 10.7: fig10_07.cpp 2 // 企圖透過基礎類別指標呼叫衍生類別的成員函式 3 // 4 #include "point.h" // 引用 Point 類別的定義 5 #include "circle.h" // 引用 Circle 類別的定義 6 7 int main() 8 { 9 Point *pointptr = 0; 10 Circle circle( 120, 89, 2.7 ); // 將 pointptr 指標指到 circle 物件 13 pointptr = &circle; // 透過 pointptr 指標呼叫 circle 類別的 point 函式 16 // 17 int x = pointptr->getx(); 18 int y = pointptr->gety(); 19 pointptr->setx( 10 ); 20 pointptr->sety( 10 ); 21 pointptr->print(); 22 fig10_07.cpp (1 of 2) 20 10
11 23 // 企圖透過基礎類別指標呼叫衍生類別的成員函式 24 // 25 double radius = pointptr->getradius(); 26 pointptr->setradius( ); 27 double diameter = pointptr->getdiameter(); 28 double circumference = pointptr->getcircumference(); 29 double area = pointptr->getarea(); return 0; } // end main fig10_07.cpp (2 of 2) 21 C:\cpphtp4\examples\ch10\fig10_07\fig10_07.cpp(25) : error C2039: 'getradius' : is not a member of 'Point' C:\cpphtp4\examples\ch10\fig10_07\point.h(6) : see declaration of 'Point' C:\cpphtp4\examples\ch10\fig10_07\fig10_07.cpp(26) : error C2039: 'setradius' : is not a member of 'Point' C:\cpphtp4\examples\ch10\fig10_07\point.h(6) : see declaration of 'Point' fig10_07.cpp output (1 of 1) 22 C:\cpphtp4\examples\ch10\fig10_07\fig10_07.cpp(27) : error C2039: 'getdiameter' : is not a member of 'Point' C:\cpphtp4\examples\ch10\fig10_07\point.h(6) : see declaration of 'Point' C:\cpphtp4\examples\ch10\fig10_07\fig10_07.cpp(28) : error C2039: 'getcircumference' : is not a member of 'Point' C:\cpphtp4\examples\ch10\fig10_07\point.h(6) : see declaration of 'Point' C:\cpphtp4\examples\ch10\fig10_07\fig10_07.cpp(29) : error C2039: 'getarea' : is not a member of 'Point' C:\cpphtp4\examples\ch10\fig10_07\point.h(6) : see declaration of 'Point' 11
12 23 一般情況以指標種類決定函式呼叫 虛擬函式 (Virtual Functions) 依物件種類決定函式呼叫 有什麼用? 虛擬函式 例如 :Circle, Triangle, Rectangle 均衍生自 Shape 每個類別都必須有自己的 draw 函式 將 draw 函式定義成 shape 的虛擬函式 可使用 shape 指標呼叫其衍生類別的 draw 函式 程式執行時期才確定所要呼叫的 draw 函式 24 虛擬函式 1. 在基礎類別宣稱虛擬函式 例如 : 將 draw 函式宣稱為 shape 的虛擬函式 virtual void draw() const; 2. 在每個衍生類別多載虛擬函式 每個多載的虛擬函式要有相同的 signature 例如 : 在 Circle, Triangle, Rectangle 多載 draw 函式 3. 動態繫結 (Dynamic binding) 當使用指標呼叫虛擬函式時 程式執行時自動選擇適當函式 12
13 25 虛擬函式 範例程式 使用虛擬函式重做 Point, Circle 範例 使用基礎類別指標指到衍生類別 可呼叫衍生類別的函式 1 // Fig. 10.8: point.h 2 // Point 類別定義 3 #ifndef POINT_H 4 #define POINT_H 5 6 class Point { 7 8 public: 9 Point( int = 0, int = 0 ); // 預設建構子 void setx( int ); // 設定 x 軸座標 12 int getx() const; // 傳回 x 軸座標 void sety( int ); // 設定 y 軸座標 宣稱為虛擬函式, 則此函式在所 15 int gety() const; // 傳回 y 軸座標 有衍生類別中都是虛擬函式 virtual void print() const; // 印出 Point 物件內容 private: 20 int x; // 存放 x 軸座標 21 int y; // 存放 y 軸座標 }; // end class Point #endif point.h (1 of 1) 26 13
14 1 // Fig. 10.9: circle.h 2 // Circle 類別定義 3 #ifndef CIRCLE_H 4 #define CIRCLE_H 5 6 #include "point.h" // 引用 Point 類別 7 8 class Circle : public Point { 9 10 public: // 預設建構于 13 Circle( int = 0, int = 0, double = 0.0 ); void setradius( double ); // 設定半徑 16 double getradius() const; // 傳回半徑 double getdiameter() const; // 傳回直徑 19 double getcircumference() const; // 傳回圓周長 20 double getarea() const; // 傳回面積 virtual void print() const; // 印出 Circle 物件內容 private: 25 double radius; // 存放 Circle 半徑 }; // end class Circle #endif circle.h (1 of 1) 27 1 // Fig : fig10_10.cpp 2 // 測試程式 : 介紹多型, 虛擬函式, 與動態繫結 3 // 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 using std::fixed; 9 10 #include <iomanip> using std::setprecision; #include "point.h" // 引用 Point 類別定義 15 #include "circle.h" // 引用 Circle 類別定義 int main() 18 { 19 Point point( 30, 50 ); 20 Point *pointptr = 0; Circle circle( 120, 89, 2.7 ); 23 Circle *circleptr = 0; 24 fig10_10.cpp (1 of 3) 28 14
15 25 // 設定浮點數顯示格式 26 cout << fixed << setprecision( 2 ); // 印出物件 point 與 circle- 使用靜態繫結 29 cout << "Invoking print function on point and circle " 30 << "\nobjects with static binding " 31 << "\n\npoint: "; 32 point.print(); // 靜態繫結 33 cout << "\ncircle: "; 34 circle.print(); // 靜態繫結 // 印出物件 point 與 circle- 使用動態繫結 37 cout << "\n\ninvoking print function on point and circle " 38 << "\nobjects with dynamic binding"; // 將 pointptr 指標指到 point 物件 41 pointptr = &point; 42 cout << "\n\ncalling virtual function print with base-class" 43 << "\npointer to base-class object" 44 << "\ninvokes base-class print function:\n"; 45 pointptr->print(); // 動態繫結 46 fig10_10.cpp (2 of 3) // 將 circleptr 指標指到 circle 物件並印出物件內容 48 // 49 circleptr = &circle; 50 cout << "\n\ncalling virtual function print with " 51 << "\nderived-class pointer to derived-class object " 52 << "\ninvokes derived-class print function:\n"; 53 circleptr->print(); // 動態繫結 // 將 pointptr 指標指到 circle 物件 56 pointptr = &circle; 57 cout << "\n\ncalling virtual function print with base-class" 58 << "\npointer to derived-class object " 59 << "\ninvokes derived-class print function:\n"; 60 pointptr->print(); // 多型 (polymorphism): 呼叫 circle 的 print 61 cout << endl; return 0; } // end main fig10_10.cpp (3 of 3) 30 15
16 // 印出物件 point 與 circle- 使用靜態繫結 Invoking print function on point and circle objects with static binding Point: [30, 50] Circle: Center = [120, 89]; Radius = 2.70 fig10_10.cpp output (1 of 1) 31 // 印出物件 point 與 circle- 使用動態繫結 Invoking print function on point and circle objects with dynamic binding // 將 pointptr 指標指到 point 物件 Calling virtual function print with base-class pointer to base-class object invokes base-class print function: [30, 50] // 將 circleptr 指標指到 circle 物件並印出物件內容 Calling virtual function print with derived-class pointer to derived-class object invokes derived-class print function: Center = [120, 89]; Radius = 2.70 // 將 pointptr 指標指到 circle 物件 Calling virtual function print with base-class pointer to derived-class object invokes derived-class print function: Center = [120, 89]; Radius = 虛擬函式 多型 (Polymorphism) 透過基礎類別存取衍生類別的相同函式 相同函式 print, 依類別不同有不同作法 16
17 33 摘要 虛擬函式 基礎指標指到基礎類別 衍生指標指到衍生類別 基礎指標指到衍生指標 只能呼叫基礎類別的函式 衍生指標指到基礎類別 編譯錯誤 34 例一 10.3 多型的例子 假設長方型衍生自四邊型 四邊型有的特性長方型也有 對四邊型的操作也可用於長方型 如, 周長, 面積計算函式 17
18 35 例二 10.3 多型的例子 假設要設計一個電玩遊戲 基礎類別太空物件 衍生類別 : 火星人 太空船 雷射光束 基礎函式畫圖 多型的例子 要更新螢幕畫面 螢幕管理程式 管理多個指到衍生類別物件的基礎類別指標 使用基礎類別指標呼叫每個物件的畫圖函式 不需要宣稱許多衍生類別指標, 再呼叫衍生類別的畫圖函式 18
19 37 假設要擴充電玩遊戲 加一個水星人 10.3 多型的例子 只要繼承太空物件 提供自己的畫圖函式 螢幕管理程式不需要改變任何程式碼 便可呼叫水星人的畫圖函式 多型的例子 多型 (Polymorphism) 只要知道物件的基礎函式 不需知道物件的類別 擴充程式方便 需要動到的程式比較少 19
20 Type 欄位與 switch 結構 要知道物件的類別 給基礎類別一個屬性 例如在 Shape 類別中定義一個 shapetype 針對不同衍生類別要有不同的列印功能 可使用 switch(shapetype) 呼叫適當的 print 函式 Type 欄位與 switch 結構 會有許多問題 可能忘了將所有類別加入 switch 內 如果要加入或移除一個類別, 必須更新 switch 程式 耗時且易出錯 使用多型 減少使用條件判斷指令, 減化程式設計工作, 減少出錯機率 20
21 41 抽象類別 10.5 抽象 (Abstract) 類別 主要目的 : 做為基礎類別 ( 稱為抽象基礎類別 ) 不完整的類別 - 部份函式未定義 由衍生類別填滿缺少的部份 不能被用來建構物件 但可使用指標及參考 完整類別 可以用來建構物件 實作所定義的所有函式 抽象 (Abstract) 類別 不一定需要抽象類別, 依程式設計方便 定義抽象類別 需要一或多個 "pure" 虛擬函式 (virtual functions) virtual void draw() const = 0; 一般的虛擬函式 有實作, 可被衍生類別多載 (override) Pure virtual functions 沒有實作, 必須被衍生類別多載 抽象類別也可以有資料與完成的函式成員 21
22 抽象 (Abstract) 類別 抽象基礎類別指標 -for polymorphism 範例 抽象類別 :Shape 定義 draw 函式為 pure virtual function 衍生類別 :Circle, Triangle, Rectangle 每個衍生類別必須實作 draw 函式 忘記實作便無法建構物件 每個物件都有自己的 draw 函式 可使用基礎物件指標呼叫衍生類別 draw 函式 案例研讀 : 繼承介面與實作 建立一個抽象基礎類別 Shape 兩個 Pure virtual functions getname print 兩個虛擬函式 getarea getvolume 衍生類別 :Point, Circle, Cylinder 22
23 案例研讀 : 繼承介面與實作 getarea getvolume getname print Shape = 0 = 0 Point "Point" [x,y] Circle πr "Circle" center=[x,y]; radius=r Cylinder 2πr 2 +2πrh πr 2 h "Cylinder" center=[x,y]; radius=r; height=h 1 // Fig : shape.h 2 // Shape 類別定義 3 #ifndef SHAPE_H 4 #define SHAPE_H 5 6 #include <string> // 引用 C++ 標準 string 類別 7 8 using std::string; 9 10 class Shape { public: // 虛擬函式 : 傳回面積 15 virtual double getarea() const; // 虛擬函式 : 傳回體積 18 virtual double getvolume() const; // pure virtual functions; 必須被衍生類別實作 21 virtual string getname() const = 0; // 傳回 shape 名稱 22 virtual void print() const = 0; // 印出 shape 物件內容 }; // end class Shape #endif shape.h (1 of 1) 46 23
24 1 // Fig : shape.cpp 2 // 實作 Shape 類別成員函式 3 #include <iostream> 4 5 using std::cout; 6 7 #include "shape.h" // 引用 Shape 類別定義 8 9 // 傳回 shape 面積 ; 預設值是 0 10 double getarea() const 11 { 12 return 0.0; } // end function getarea // 傳回 shape 體積 ; 預設值傳回 double getvolume() const 18 { 19 return 0.0; } // end function getvolume 47 shape.cpp (1 of 1) 1 // Fig : point.h 2 // Point 類別定義 3 #ifndef POINT_H 4 #define POINT_H 5 6 #include "shape.h" // 引用 Shape 類別定義 7 8 class Point : public Shape { 9 10 public: 11 Point( int = 0, int = 0 ); // 預設建構子 void setx( int ); // 設定 x 座標值 14 int getx() const; // 傳回 x 座標值 void sety( int ); // 設定 y 座標值 17 int gety() const; // 傳回 y 座標值 // 傳回名字 (i.e., "Point" ) 20 virtual string getname() const; virtual void print() const; // 印出 Point 物件內容 23 沒有定義 getarea() 與 getvolume() point.h (1 of 2) 48 24
25 24 private: 25 int x; // 存放 x 座標值 26 int y; // 存放 y 座標值 }; // end class Point #endif point.h (2 of 2) 49 1 // Fig : point.cpp 2 // 實作 Point 類別成員函式 3 #include <iostream> 4 5 using std::cout; 6 7 #include "point.h" // 引用 Point 類別定義 8 9 // 預設建構子 10 Point::Point( int xvalue, int yvalue ) 11 : x( xvalue ), y( yvalue ) 12 { } // end Point constructor // 設定 x 座標值 18 void Point::setX( int xvalue ) 19 { 20 x = xvalue; } // end function setx point.cpp (1 of 3) 25
26 24 // 傳回 x 座標值 25 int Point::getX() const 26 { 27 return x; } // end function getx // 設定 y 座標值 32 void Point::setY( int yvalue ) 33 { 34 y = yvalue; } // end function sety // 傳回 y 座標值 39 int Point::getY() const 40 { 41 return y; } // end function gety point.cpp (2 of 3) 45 // 多載 pure virtual function getname: 傳回 Point 名字 46 string Point::getName() const 47 { 48 return "Point"; } // end function getname // 多載 pure virtual function print: 印出 Point 物件內容 53 void Point::print() const 54 { 55 cout << '[' << getx() << ", " << gety() << ']'; } // end function print 52 point.cpp (3 of 3) 26
27 1 // Fig : circle.h 2 // Circle 類別定義 3 #ifndef CIRCLE_H 4 #define CIRCLE_H 5 6 #include "point.h" // 引用 Point 類別定義 7 8 class Circle : public Point { 9 10 public: // 預設建構子 13 Circle( int = 0, int = 0, double = 0.0 ); void setradius( double ); // 設定半徑 16 double getradius() const; // 傳回半徑 double getdiameter() const; // 傳回直徑 19 double getcircumference() const; // 傳回圓周長 20 virtual double getarea() const; // 傳回面積 // 傳回名字 (i.e., "Circle") 23 virtual string getname() const; virtual void print() const; // 印出 Circle 物件內容 circle.h (1 of 2) private: 28 double radius; // 存放 Circle 半徑 }; // end class Circle #endif circle.h (2 of 2) 54 27
28 1 // Fig : 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 : Point( xvalue, yvalue ) // 呼叫基礎類別的建構子 12 { 13 setradius( radiusvalue ); } // end Circle constructor // 設定半徑 18 void Circle::setRadius( double radiusvalue ) 19 { 20 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); } // end function setradius circle.cpp (1 of 3) 24 // 傳回半徑 25 double Circle::getRadius() const 26 { 27 return radius; } // end function getradius // 計算及傳回直徑 32 double Circle::getDiameter() const 33 { 34 return 2 * getradius(); } // end function getdiameter // 計算及傳回圓周長 39 double Circle::getCircumference() const 40 { 41 return * getdiameter(); } // end function getcircumference circle.cpp (2 of 3) 28
29 45 // 多載虛擬函式 getarea: 傳回圓面積 46 double Circle::getArea() const 47 { 48 return * getradius() * getradius(); } // end function getarea // 多載虛擬函式 getname: 傳回名字 53 string Circle::getName() const 54 { 55 return "Circle"; } // end function getname // 多載虛擬函式 print: 印出 Circle 物件內容 60 void Circle::print() const 61 { 62 cout << "center is "; 63 Point::print(); // 呼叫 Point 的 print 函式 64 cout << "; radius is " << getradius(); } // end function print 57 circle.cpp (3 of 3) 1 // Fig : cylinder.h 2 // Cylinder 類別定義 3 #ifndef CYLINDER_H 4 #define CYLINDER_H 5 6 #include "circle.h" // 引用 Circle 類別定義 7 8 class Cylinder : public Circle { 9 10 public: // 預設建構子 13 Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); void setheight( double ); // 設定 Cylinder 高度 16 double getheight() const; // 傳回 Cylinder 高度 virtual double getarea() const; // 傳回 Cylinder 面積 19 virtual double getvolume() const; // 傳回 Cylinder 體積 cylinder.h (1 of 2) 29
30 21 // 傳回名字 (i.e., "Cylinder" ) 22 virtual string getname() const; virtual void print() const; // 印出 Cylinder 物件內容 private: 27 double height; // 存放 Cylinder 高度 }; // end class Cylinder #endif 59 cylinder.h (2 of 2) 1 // Fig : cylinder.cpp 2 // 實作 Cylinder 類別成員函式 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 : Circle( xvalue, yvalue, radiusvalue ) 13 { 14 setheight( heightvalue ); } // end Cylinder constructor // 設定 Cylinder 高度 19 void Cylinder::setHeight( double heightvalue ) 20 { 21 height = ( heightvalue < 0.0? 0.0 : heightvalue ); } // end function setheight cylinder.cpp (1 of 3) 60 30
31 24 25 // 取得 Cylinder 高度 26 double Cylinder::getHeight() const 27 { 28 return height; } // end function getheight // 多載虛擬函式 getarea: 傳回 Cylinder 面積 33 double Cylinder::getArea() const 34 { 35 return 2 * Circle::getArea() + // 程式碼重覆使用 36 getcircumference() * getheight(); } // end function getarea // 多載虛擬函式 getvolume: 傳回 Cylinder 體積 41 double Cylinder::getVolume() const 42 { 43 return Circle::getArea() * getheight(); // 程式碼重覆使用 } // end function getvolume 46 cylinder.cpp (2 of 3) // 多載虛擬函式 getname: 傳回名字 48 string Cylinder::getName() const 49 { 50 return "Cylinder"; } // end function getname // 印出 Cylinder 物件內容 55 void Cylinder::print() const 56 { 57 Circle::print(); // 程式碼重覆使用 58 cout << "; height is " << getheight(); } // end function print cylinder.cpp (3 of 3) 62 31
32 1 // Fig : fig10_20.cpp 2 // 測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> using std::setprecision; #include <vector> using std::vector; #include "shape.h" // 引用 Shape 類別定義 18 #include "point.h" // 引用 Point 類別定義 19 #include "circle.h" // 引用 Circle 類別定義 20 #include "cylinder.h" // 引用 Cylinder 類別定義 void virtualviapointer( const Shape * ); 23 void virtualviareference( const Shape & ); 24 fig10_20.cpp (1 of 5) int main() 26 { 27 // 設定浮點數格式 28 cout << fixed << setprecision( 2 ); Point point( 7, 11 ); // 建構 Point 物件 31 Circle circle( 22, 8, 3.5 ); // 建構 Circle 物件 32 Cylinder cylinder( 10, 10, 3.3, 10 ); // 建構 Cylinder 物件 cout << point.getname() << ": "; // 靜態繫結 35 point.print(); // 靜態繫結 36 cout << '\n'; cout << circle.getname() << ": "; // 靜態繫結 39 circle.print(); // 靜態繫結 40 cout << '\n'; cout << cylinder.getname() << ": "; // 靜態繫結 43 cylinder.print(); // 靜態繫結 44 cout << "\n\n"; 45 fig10_20.cpp (2 of 5) 64 32
33 46 // 建立一個內含 3 個 Shape 指標的 vector 47 vector< Shape * > shapevector( 3 ); // 將 shapevector[0] 指到 Point 物件 50 shapevector[ 0 ] = &point; // 將 shapevector[1] 指到 Circle 物件 53 shapevector[ 1 ] = &circle; // 將 shapevector[2] 指到 Cylinder 物件 56 shapevector[ 2 ] = &cylinder; // 利用 shapevector 呼叫每個物件的虛擬函式 59 // 印出物件名字, 屬性, 面積, 與體積 - 動態繫結 (dynamic binding) 60 // 使用傳指標方式 61 cout << "\nvirtual function calls made off " 62 << "base-class pointers:\n\n"; for ( int i = 0; i < shapevector.size(); i++ ) 65 virtualviapointer( shapevector[ i ] ); 66 fig10_20.cpp (3 of 5) // 利用 shapevector 呼叫每個物件的虛擬函式 68 // 印出物件名字, 屬性, 面積, 與體積 - 動態繫結 (dynamic binding) 69 // 使用傳參考方式 70 cout << "\nvirtual function calls made off " 71 << "base-class references:\n\n"; for ( int j = 0; j < shapevector.size(); j++ ) 74 virtualviareference( *shapevector[ j ] ); return 0; } // end main // 利用基礎類別指標呼叫虛擬函式 - 動態繫結 81 // 使用傳指標方式 82 void virtualviapointer( const Shape *baseclassptr ) 83 { 84 cout << baseclassptr->getname() << ": "; baseclassptr->print(); cout << "\narea is " << baseclassptr->getarea() 89 << "\nvolume is " << baseclassptr->getvolume() 90 << "\n\n"; } // end function virtualviapointer 93 fig10_20.cpp (4 of 5) 66 33
34 94 // 利用基礎類別參考呼叫虛擬函式 - 動態繫結 95 // 使用傳參考方式 96 void virtualviareference( const Shape &baseclassref ) 97 { 98 cout << baseclassref.getname() << ": "; baseclassref.print(); cout << "\narea is " << baseclassref.getarea() 103 << "\nvolume is " << baseclassref.getvolume() << "\n\n"; } // end function virtualviareference fig10_20.cpp (5 of 5) 67 // 靜態繫結 Point: [7, 11] Circle: center is [22, 8]; radius is 3.50 Cylinder: center is [10, 10]; radius is 3.30; height is // 動態繫結 - 傳指標 Virtual function calls made off base-class pointers: fig10_20.cpp output (1 of 2) 68 Point: [7, 11] area is 0.00 volume is 0.00 Circle: center is [22, 8]; radius is 3.50 area is volume is 0.00 Cylinder: center is [10, 10]; radius is 3.30; height is area is volume is
35 // 動態繫結 - 傳參考 Virtual function calls made off base-class references: Point: [7, 11] area is 0.00 volume is 0.00 fig10_20.cpp output (2 of 2) 69 Circle: center is [22, 8]; radius is 3.50 area is volume is 0.00 Cylinder: center is [10, 10]; radius is 3.30; height is area is volume is 多型, 虛擬函式, 與動態繫結 多型較浪費資源 標準樣版函式庫 (STL) 沒有使用多型 效能考量 使用虛擬函式表 (vtable) 每個具虛擬函式的類別有一個 vtable 每個虛擬函式在 vtable 中都有一個指到適當函式的指標 假如衍生類別跟基礎類別有相同函式 函式指標會指到基礎類別 35
36 虛擬解構子 基礎類別指標指到衍生物件 如果用 delete 刪除, 行為不可預測 修正方法 宣稱基礎類別的解構子為虛擬函式 可讓衍生類別的解構子變成虛擬函式 可用 delete 刪除基礎類別指標指到的衍生物件 虛擬解構子 衍生類別物件何時被解構 衍生類別的解構子先執行 基礎類別的解構子後執行 建構子不能是虛擬函式 36
37 案例研究 : 使用多型實作薪資系統 建立一個薪資系統 使用虛擬函式與多型 問題描述 四種員工, 每周付薪水 Salaried ( 固定薪水 ) Hourly ( 時薪, 超時 [>40 小時 ] 部份 1.5 倍 ) Commission ( 按件抽成 ) Base-plus-commission ( 底薪 + 抽成 ) 老闆要加底薪 1 成 案例研究 : 使用多型實作薪資系統 基礎類別 Employee Pure virtual function : earnings ( 傳回薪水 ) 因為必須依員工種類決定薪水算法 Employee SalariedEmployee CommissionEmployee HourlyEmployee BasePlusCommissionEmployee 37
38 案例研究 : 使用多型實作薪資系統 Downcasting( 向下轉型 ) dynamic_cast 運算子 可在執行時期確定物件型態 型態不對傳回 0 Employee *empptr=new Employee; SalariedEmployee *ptr; *ptr = dynamic_cast < SalariedEmployee *> empptr; 案例研究 : 使用多型實作薪資系統 關鍵字 :typeid 標頭檔 <typeinfo> 用法 : typeid(object) 傳回 type_info 物件 內含運算元的類別名稱資訊 typeid(object).name() 可用來比較兩型態是否相同 typeid(object1)==typeid(object2) typeid(object1)!=typeid(object2) 38
39 1 // Fig : employee.h 2 // Employee 抽象基礎類別定義 3 #ifndef EMPLOYEE_H 4 #define EMPLOYEE_H 5 6 #include <string> // C++ 標準字串類別 7 8 using std::string; 9 10 class Employee { public: 13 Employee(const string &, const string &, const string &); void setfirstname( const string & ); 16 string getfirstname() const; void setlastname( const string & ); 19 string getlastname() const; void setsocialsecuritynumber( const string & ); 22 string getsocialsecuritynumber() const; 23 employee.h (1 of 2) // 虛擬函式 25 virtual double earnings() const = 0; // pure virtual 26 virtual void print() const; // virtual private: 29 string firstname; 30 string lastname; 31 string socialsecuritynumber; }; // end class Employee #endif // EMPLOYEE_H employee.h (2 of 2) 78 39
40 1 // Fig : employee.cpp 2 // 實作抽象基礎類別 Employee 3 // 注意 :pure virtual functions 不需實作 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 #include "employee.h" // 引用 Employee 類別定義 // 建構子 12 Employee::Employee( const string &first, const string &last, 13 const string &SSN ) 14 : firstname( first ), 15 lastname( last ), 16 socialsecuritynumber( SSN ) 17 { } // end Employee constructor 21 employee.cpp (1 of 3) // 傳回 first name 23 string Employee::getFirstName() const 24 { 25 return firstname; } // end function getfirstname // 傳回 last name 30 string Employee::getLastName() const 31 { 32 return lastname; } // end function getlastname // 傳回身分證號 37 string Employee::getSocialSecurityNumber() const 38 { 39 return socialsecuritynumber; } // end function getsocialsecuritynumber // 設定 first name 44 void Employee::setFirstName( const string &first ) 45 { 46 firstname = first; } // end function setfirstname 49 employee.cpp (2 of 3) 80 40
41 50 // 設定 last name 51 void Employee::setLastName( const string &last ) 52 { 53 lastname = last; } // end function setlastname // 設定身分證號 58 void Employee::setSocialSecurityNumber(const string &number) 59 { 60 socialsecuritynumber = number; } // end function setsocialsecuritynumber // 印出 Employee 物件內容 65 void Employee::print() const 66 { 67 cout << getfirstname() << ' ' << getlastname() 68 << "\nsocial security number: " 69 << getsocialsecuritynumber() << endl; } // end function print employee.cpp (3 of 3) 81 1 // Fig : salaried.h 2 // SalariedEmployee( 固定薪水員工 ) 類別定義 3 #ifndef SALARIED_H 4 #define SALARIED_H 5 6 #include "employee.h" // 引用 Employee 類別定義 7 8 class SalariedEmployee : public Employee { 9 10 public: 11 SalariedEmployee( const string &, const string &, 12 const string &, double = 0.0 ); void setweeklysalary( double ); 15 double getweeklysalary() const; virtual double earnings() const; // 一定要實作 18 virtual void print() const; // 印出 "salaried employee:" private: 21 double weeklysalary; }; // end class SalariedEmployee #endif // SALARIED_H 82 salaried.h (1 of 1) 41
42 1 // Fig : salaried.cpp 2 // 實作 SalariedEmployee( 固定薪水員工 ) 類別成員函式 3 #include <iostream> 4 5 using std::cout; 6 7 #include "salaried.h" // 引用 SalariedEmployee 類別定義 8 9 // SalariedEmployee 建構子 10 SalariedEmployee::SalariedEmployee( const string &first, 11 const string &last, const string &socialsecuritynumber, 12 double salary ) 13 : Employee( first, last, socialsecuritynumber ) 14 { 15 setweeklysalary( salary ); } // end SalariedEmployee constructor // 設定固定薪水員工薪水 20 void SalariedEmployee::setWeeklySalary( double salary ) 21 { 22 weeklysalary = salary < 0.0? 0.0 : salary; } // end function setweeklysalary 25 salaried.cpp (1 of 2) // 計算固定薪水員工薪水 27 double SalariedEmployee::earnings() const 28 { 29 return getweeklysalary(); } // end function earnings // 傳回固定薪水員工薪水 34 double SalariedEmployee::getWeeklySalary() const 35 { 36 return weeklysalary; } // end function getweeklysalary // 印出固定薪水員工名字 41 void SalariedEmployee::print() const 42 { 43 cout << "\nsalaried employee: "; 44 Employee::print(); // 呼叫 Employee 的 print 函式 } // end function print salaried.cpp (2 of 2) 84 42
43 1 // Fig : hourly.h 2 // HourlyEmployee( 時薪員工 ) 類別定義 3 #ifndef HOURLY_H 4 #define HOURLY_H 5 6 #include "employee.h" // 引用 Employee 類別定義 7 8 class HourlyEmployee : public Employee { 9 10 public: 11 HourlyEmployee( const string &, const string &, 12 const string &, double = 0.0, double = 0.0 ); void setwage( double ); // 設定工資 15 double getwage() const; // 讀取工資 void sethours( double ); // 設定工作時數 18 double gethours() const; // 讀取工作時數 virtual double earnings() const; // 計算薪水 21 virtual void print() const; // 印出時薪員工內容 private: 24 double wage; // 每小時工資 25 double hours; // 員工工作時數 }; // end class HourlyEmployee #endif // HOURLY_H hourly.h (1 of 1) 85 1 // Fig : hourly.cpp 2 // 實作 HourlyEmployee( 時薪員工 ) 成員函式 3 #include <iostream> 4 5 using std::cout; 6 7 #include "hourly.h" 8 9 // HourlyEmployee 建構子 10 HourlyEmployee::HourlyEmployee( const string &first, 11 const string &last, const string &socialsecuritynumber, 12 double hourlywage, double hoursworked ) 13 : Employee( first, last, socialsecuritynumber ) 14 { 15 setwage( hourlywage ); 16 sethours( hoursworked ); } // end HourlyEmployee constructor // 設定每小時工資 21 void HourlyEmployee::setWage( double wageamount ) 22 { 23 wage = wageamount < 0.0? 0.0 : wageamount; } // end function setwage hourly.cpp (1 of 3) 86 43
44 26 27 // 設定工作時數 28 void HourlyEmployee::setHours( double hoursworked ) 29 { 30 hours = ( hoursworked >= 0.0 && hoursworked <= )? 31 hoursworked : 0.0; } // end function sethours // 傳回工作時數 36 double HourlyEmployee::getHours() const 37 { 38 return hours; } // end function gethours // 傳回每小時工資 43 double HourlyEmployee::getWage() const 44 { 45 return wage; } // end function getwage 48 hourly.cpp (2 of 3) // 計算時薪員工薪水 50 double HourlyEmployee::earnings() const 51 { 52 if ( hours <= 40 ) // 沒有超過基本工時 53 return wage * hours; 54 else // 超過基本工時, 超過部份等於每小時工資 * return 40 * wage + ( hours - 40 ) * wage * 1.5; } // end function earnings // 印出時薪員工內容 60 void HourlyEmployee::print() const 61 { 62 cout << "\nhourly employee: "; 63 Employee::print(); // 呼叫 Employee 的 print 函式 } // end function print hourly.cpp (3 of 3) 88 44
45 1 // Fig : commission.h 2 // CommissionEmployee( 抽成員工 ) 類別定義 3 #ifndef COMMISSION_H 4 #define COMMISSION_H 5 6 #include "employee.h" // 引用 Employee 類別定義 7 8 class CommissionEmployee : public Employee { 9 10 public: 11 CommissionEmployee( const string &, const string &, 12 const string &, double = 0.0, double = 0.0 ); void setcommissionrate( double ); 15 double getcommissionrate() const; void setgrosssales( double ); 18 double getgrosssales() const; virtual double earnings() const; 21 virtual void print() const; private: 24 double grosssales; // 每周銷售總額 25 double commissionrate; // 傭金比例 }; // end class CommissionEmployee #endif // COMMISSION_H commission.h (1 of 1) 89 1 // Fig : commission.cpp 2 // 實作 CommissionEmployee( 抽成員工 ) 成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 7 #include "commission.h" // 引用 Commission 類別 8 9 // CommissionEmployee 建構子 10 CommissionEmployee::CommissionEmployee( const string &first, 11 const string &last, const string &socialsecuritynumber, 12 double grossweeklysales, double percent ) 13 : Employee( first, last, socialsecuritynumber ) 14 { 15 setgrosssales( grossweeklysales ); 16 setcommissionrate( percent ); } // end CommissionEmployee constructor // 傳回抽成員工抽成比率 21 double CommissionEmployee::getCommissionRate() const 22 { 23 return commissionrate; } // end function getcommissionrate commission.cpp (1 of 3) 90 45
46 26 27 // 傳回抽成員工銷售總額 28 double CommissionEmployee::getGrossSales() const 29 { 30 return grosssales; } // end function getgrosssales // 設定抽成員工底薪 (=0) 35 void CommissionEmployee::setGrossSales( double sales ) 36 { 37 grosssales = sales < 0.0? 0.0 : sales; } // end function setgrosssales // 設定抽成員工傭金比率 42 void CommissionEmployee::setCommissionRate( double rate ) 43 { 44 commissionrate = (rate > 0.0 && rate < 1.0)? rate : 0.0; } // end function setcommissionrate 47 commission.cpp (2 of 3) // 計算及傳回抽成員工薪水 49 double CommissionEmployee::earnings() const 50 { 51 return getcommissionrate() * getgrosssales(); } // end function earnings // 印出抽成員工姓名 56 void CommissionEmployee::print() const 57 { 58 cout << "\ncommission employee: "; 59 Employee::print(); // 呼叫 Employee 的 print 函式 } // end function print commission.cpp (3 of 3) 92 46
47 1 // Fig : baseplus.h 2 // BasePlusCommissionEmployee( 有底薪抽成員工 ) 類別定義 3 #ifndef BASEPLUS_H 4 #define BASEPLUS_H 5 6 #include "commission.h" // 引用 Employee 類別定義 7 8 class BasePlusCommissionEmployee : public CommissionEmployee { 9 10 public: 11 BasePlusCommissionEmployee(const string &, const string &, 12 const string &, double=0.0, double=0.0, double=0.0); void setbasesalary( double );// 設定底薪 15 double getbasesalary() const;// 取得底薪 virtual double earnings() const; 18 virtual void print() const; private: 21 double basesalary; // 每周底薪 }; // end class BasePlusCommissionEmployee #endif // BASEPLUS_H baseplus.h (1 of 1) 93 1 // Fig : baseplus.cpp 2 // 實作 BasePlusCommissionEmployee( 有底薪抽成員工 ) 成員函式 3 #include <iostream> 4 5 using std::cout; 6 7 #include "baseplus.h" 8 9 // BasePlusCommissionEmployee 建構子 10 BasePlusCommissionEmployee::BasePlusCommissionEmployee( 11 const string &first, const string &last, 12 const string &socialsecuritynumber, 13 double grosssalesamount, double rate, 14 double basesalaryamount ) 15 : CommissionEmployee( first, last, socialsecuritynumber, 16 grosssalesamount, rate ) 17 { 18 setbasesalary( basesalaryamount ); } // end BasePlusCommissionEmployee constructor // 設定底薪 23 void BasePlusCommissionEmployee::setBaseSalary(double salary) 24 { 25 basesalary = salary < 0.0? 0.0 : salary; } // end function setbasesalary baseplus.cpp (1 of 2) 94 47
48 28 29 // 傳回底薪 30 double BasePlusCommissionEmployee::getBaseSalary() const 31 { 32 return basesalary; } // end function getbasesalary // 傳回薪水 37 double BasePlusCommissionEmployee::earnings() const 38 { 39 return getbasesalary() + CommissionEmployee::earnings(); } // end function earnings // 印出員工姓名 44 void BasePlusCommissionEmployee::print() const 45 { 46 cout << "\nbase-salaried commission employee: "; 47 Employee::print(); // 呼叫 Employee 的 print 函式 } // end function print baseplus.cpp (2 of 2) 95 1 // Fig : fig10_33.cpp 2 // 測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include <iomanip> using std::setprecision; #include <vector> using std::vector; #include <typeinfo> #include "employee.h" // 引用員工類別 20 #include "salaried.h" // 引用固定薪水員工類別 21 #include "commission.h" // 引用抽成員工類別 22 #include "baseplus.h" // 引用有底薪抽成員工類別 23 #include "hourly.h" // 引用時薪員工類別 24 fig10_33.cpp (1 of 4) 96 48
49 25 int main() 26 { 27 // 設定浮點數顯示格式 28 cout << fixed << setprecision( 2 ); // 建構員工陣列 31 vector < Employee * > employees( 4 ); // 初始化員工陣列內容 ( 使用基礎類別指標指到衍生類別 ) 34 employees[ 0 ] = new SalariedEmployee( "John", "Smith", 35 " ", ); 36 employees[ 1 ] = new CommissionEmployee( "Sue", "Jones", 37 " ", 10000,.06 ); 38 employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", 39 "Lewis", " ", 300, 5000,.04 ); 40 employees[ 3 ] = new HourlyEmployee( "Karen", "Price", 41 " ", 16.75, 40 ); 42 fig10_33.cpp (2 of 4) // 印出每個員工物件, 並將有底薪抽成員工底薪加 1 成 44 for ( int i = 0; i < employees.size(); i++ ) { // 印出員工內容 47 employees[ i ]->print(); // downcast: 將指標向下轉型成 " 有底薪抽成員工 " 50 BasePlusCommissionEmployee *commissionptr = 51 dynamic_cast < BasePlusCommissionEmployee * > 52 ( employees[ i ] ); // 檢查轉型後指標內容是否有效 55 // 如果有效則此員工為有底薪抽成員工, 底薪加 10% 56 if ( commissionptr!= 0 ) { 57 cout << "old base salary: $" 58 << commissionptr->getbasesalary() << endl; 59 commissionptr->setbasesalary( * commissionptr->getbasesalary() ); 61 cout << "new base salary with 10% increase is: $" 62 << commissionptr->getbasesalary() << endl; } // end if cout << "earned $" << employees[i]->earnings() << endl; } // end for 69 fig10_33.cpp (3 of 4) 98 49
50 70 // 釋放員工陣列的記憶體 71 for ( int j = 0; j < employees.size(); j++ ) { // 輸出類別名稱 74 cout << "\ndeleting object of " 75 << typeid( *employees[ j ] ).name(); delete employees[j];// 刪除基礎類別物件 } // end for cout << endl; return 0; } // end main fig10_33.cpp (4 of 4) 99 // 印出每個員工物件, 並將有底薪抽成員工底薪加 1 成 salaried employee: John Smith social security number: earned $ commission employee: Sue Jones social security number: earned $ fig10_33.cpp output (1 of 1) base-salaried commission employee: Bob Lewis social security number: old base salary: $ new base salary with 10% increase is: $ earned $ hourly employee: Karen Price social security number: earned $ // 釋放員工陣列的記憶體 deleting object of class SalariedEmployee deleting object of class CommissionEmployee deleting object of class BasePlusCommissionEmployee deleting object of class HourlyEmployee 50
Microsoft PowerPoint - 09_Inheritance.ppt
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 )
More informationMicrosoft PowerPoint - L17_Inheritance_v4.pptx
C++ Programming Lecture 17 Wei Liu ( 刘 威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology May. 2015 Lecture 17 Chapter 20. Object-Oriented Programming: Inheritance
More information運算子多載 Operator Overloading
多型 Polymorphism 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 子類別與父類別物件間的指定 (assignment)
More informationStrings
Polymorphism and Virtual Functions Cheng-Chin Chiang Virtual Function Basics 多 型 (Polymorphism) 賦 予 一 個 函 數 多 種 意 涵, 存 在 於 同 一 類 別 之 內 祖 先 類 別 與 後 代 類 別 間 物 件 導 向 程 式 設 計 基 本 原 理 虛 擬 函 數 (Virtual Function)
More information一、
考試時間 : 1 小時 30 分座號 : 全五頁第一頁 注意 : 禁止使用電子計算器 不必抄題, 作答時請將試題題號及答案依照順序寫在試卷上, 於本試題上作答者, 不予計分 一 就下列 Java 程式片斷中加入一個 for 迴圈, 使其印出右側結果 (15 分 ) public class AllNumbers number square cube 0 0 0 public static void
More informationMicrosoft Word - chap10.doc
78 10. Inheritance in C++ 我 們 已 介 紹 了 物 件 導 向 程 式 的 第 一 個 主 要 特 性, 即 程 式 可 模 組 化 成 為 類 別 ( 物 件 ), 類 別 具 有 資 料 封 裝 的 特 性 接 下 來 我 們 要 介 紹 物 件 導 向 程 式 的 另 一 個 主 要 特 性, 那 就 是 類 別 具 有 繼 承 的 功 能 繼 承 就 是 重 複
More informationMicrosoft PowerPoint - CPP-Ch Print.ppt [兼容模式]
Chapter 13 Object-Oriented Programming: Polymorphism http://jssec.seu.edu.cn 杨明 yangming2002@seu.edu.cn OBJECTIVES What polymorphism( 多态 ) is, how it makes programming more convenient, and how it makes
More information96年特種考試第一次司法人員考試試題解答
106 年公務人員特種考試警察人員 一般警察人員考試及 106 年特種考試交通事業鐵路人員考試試題 考試別 : 鐵路人員考試 等別 : 員級考試 類科 ( 別 ): 資訊處理 科目 : 程式設計概要 考試時間 :1.5 小時 一 就下列 Java 程式片斷中加入一個 for 迴圈, 使其印出右側結果 (15 分 ) public class AllNumbers number square cube
More informationMicrosoft PowerPoint - 11_Templates.ppt
1 1. 上機考 20% 期末考 6/23( 四 ) 晚 6:30~8:30 範圍 : 第 7, 8, 9, 10 章實習內容 按座位坐, 隨機抽兩題 2. 紙上測驗 20% 6/21( 二 ) :9:30~11:00 課本 7-11, 13 章內容 2 第 11 章樣版 (Templates) 11.1 簡介 11.2 函式樣版 11.3 多載函式樣版 11.4 類別樣版 11.5 類別樣版與無型
More information新版 明解C++入門編
511!... 43, 85!=... 42 "... 118 " "... 337 " "... 8, 290 #... 71 #... 413 #define... 128, 236, 413 #endif... 412 #ifndef... 412 #if... 412 #include... 6, 337 #undef... 413 %... 23, 27 %=... 97 &... 243,
More informationStrings
Inheritance Cheng-Chin Chiang Relationships among Classes A 類 別 使 用 B 類 別 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人 類 使 用 交 通 工 具 旅 行 A 類 別 中 有 B 類 別 汽 車 有 輪 子 三 角 形 有 三 個 頂 點 電 腦 內 有 中 央 處 理 單 元 A
More informationuntitled
(encapsulation) 例 類 說 類 料 來 料 information hiding 念 (inheritance) 來說 類 類 類 類 類 類 行 利 來 (polymorphism) 不 類 數 不 1 2 3 4 類 類 不 類 不 類 5 6 7 // virtual 不見了 #include #include using namespace
More information第七讲 继承与多态
第 七 章 继 承 与 派 生 1 本 章 主 要 内 容 的 继 承 成 员 的 访 问 控 制 单 继 承 与 多 继 承 派 生 的 构 造 析 构 函 数 成 员 的 标 识 与 访 问 深 度 探 索 2 的 继 承 与 派 生 的 继 承 与 派 生 保 持 已 有 的 特 性 而 构 造 新 的 过 程 称 为 继 承 在 已 有 的 基 础 上 新 增 自 己 的 特 性 而 产 生
More informationMicrosoft Word - ch04三校.doc
4-1 4-1-1 (Object) (State) (Behavior) ( ) ( ) ( method) ( properties) ( functions) 4-2 4-1-2 (Message) ( ) ( ) ( ) A B A ( ) ( ) ( YourCar) ( changegear) ( lowergear) 4-1-3 (Class) (Blueprint) 4-3 changegear
More informationuntitled
1 7 7.1 7.2 7.3 7.4 7.5 2 7.1 VFT virtual 7.1 3 1 1. 2. public protected public 3. VFT 4. this const volatile 4 2 5. ( ) ( ) 7.1 6. no-static virtual 7.2 7. inline 7.3 5 3 8. this this 9. ( ) ( ) delete
More information<4D6963726F736F667420506F776572506F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>
程 序 设 计 实 习 INFO130048 3-2.C++ 面 向 对 象 程 序 设 计 重 载 继 承 多 态 和 聚 合 复 旦 大 学 计 算 机 科 学 与 工 程 系 彭 鑫 pengxin@fudan.edu.cn 内 容 摘 要 方 法 重 载 类 的 继 承 对 象 引 用 和 拷 贝 构 造 函 数 虚 函 数 和 多 态 性 类 的 聚 集 复 旦 大 学 计 算 机 科 学
More information第十章 虛擬函數 (Virtual Functions)
繼承的優點 程式碼再使用 (code reuse) 抽象概念再使用 類別階層化 澄清物件間的關係 繼承與 Code Reuse( 被動 ) 主計劃 子計劃 1 子計劃 2 子計劃 3 ( 你所在的組 ) 類別庫函式庫 (.lib.dll) 繼承與 Code Reuse class List { void insert() { void delete() { 重新改寫??? (1) 原始碼在哪? (2)
More informationMicrosoft Word - 物件導向編程精要.doc
Essential Object-Oriented Programming Josh Ko 2007.03.11 object-oriented programming C++ Java OO class object OOP Ruby duck typing complexity abstraction paradigm objects objects model object-oriented
More informationMicrosoft PowerPoint - Class5.pptx
C++ 程式初探 V 2015 暑期 ver. 1.0.1 C++ 程式語言 大綱 1. 大量檔案讀取 & 計算 2. 指標 3. 動態記憶體 & 動態陣列 4. 標準函式庫 (STL) vector, algorithm 5. 結構與類別 2 大量檔案讀取 & 計算 若目前有一個程式將讀取純文字文件 (.txt) 中的整數, 並將該文件中的整數有小到大排序後, 儲存到另外一個新的純文字件中 假設有
More informationChapter 9: Objects and Classes
Fortran Algol Pascal Modula-2 BCPL C Simula SmallTalk C++ Ada Java C# C Fortran 5.1 message A B 5.2 1 class Vehicle subclass Car object mycar public class Vehicle extends Object{ public int WheelNum
More informationC 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
201 201 21 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 1 B 1 C 5 D RPG 10 E 10 F 1 G II 1 1 201 201 C 1 # include 2 int main ( void
More informationFY.DOC
高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主
More informationMicrosoft PowerPoint - 13_Exception.ppt
1 第 13 章例外處理 (Exception Handling) 13.1 簡介 13.2 例外處理概觀 13.3 其它錯誤處理技術 13.4 簡單的例外處理範例 - 除 0 錯誤 13.5 重新丟出例外 13.6 函式例外清單 13.7 處理非預期例外 13.8 堆疊返回 13.9 建構子, 解構子, 與例外處理 13.10 例外與繼承 13.11 處理新的錯誤 13.12 auto_ptr 類別與動態記憶體配置
More informationuntitled
1 Outline 數 料 數 數 列 亂數 練 數 數 數 來 數 數 來 數 料 利 料 來 數 A-Z a-z _ () 不 數 0-9 數 不 數 SCHOOL School school 數 讀 school_name schoolname 易 不 C# my name 7_eleven B&Q new C# (1) public protected private params override
More informationMicrosoft PowerPoint - 13_ClassAndObj.ppt
Visual Basic 2005 (VB.net 2.0) 程式設計 講師 : 戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所 第十三章 物件與類別 物件與類別 物件導向程式設計 物件與類別的建立 物件與類別 物件 (object) Ex. 人 屬性 (property) 身高 體重 血型 方法 (method) 走路 跑步 訊息 (message) 交談 事件
More informationMicrosoft Word - 970617cppFinalSolution.doc
國 立 台 灣 海 洋 大 學 資 訊 工 程 系 C++ 程 式 設 計 期 末 考 參 考 答 案 姓 名 : 系 級 : 學 號 : 97/06/17 考 試 時 間 :10:00 12:10 試 題 敘 述 蠻 多 的, 看 清 楚 題 目 問 什 麼, 針 對 重 點 回 答 是 很 重 要 的 ; 不 確 定 的 請 一 定 要 當 場 提 出 來, 不 要 白 花 力 氣 在 誤 會
More informationC++ 程式設計
C C 料, 數, - 列 串 理 列 main 數串列 什 pointer) 數, 數, 數 數 省 不 不, 數 (1) 數, 不 數 * 料 * 數 int *int_ptr; char *ch_ptr; float *float_ptr; double *double_ptr; 數 (2) int i=3; int *ptr; ptr=&i; 1000 1012 ptr 數, 數 1004
More information软件工程文档编制
实训抽象类 一 实训目标 掌握抽象类的定义 使用 掌握运行时多态 二 知识点 抽象类的语法格式如下 : public abstract class ClassName abstract void 方法名称 ( 参数 ); // 非抽象方法的实现代码 在使用抽象类时需要注意如下几点 : 1 抽象类不能被实例化, 实例化的工作应该交由它的子类来完成 2 抽象方法必须由子类来进行重写 3 只要包含一个抽象方法的抽象类,
More information2013 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
2013 18 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp, Compilation Error cin scanf Time Limit Exceeded 1: A 5 B 5 C 5 D 5 E 5 F 5 1 2013 C 1 # include 2 int main ( void ) 3 { 4 int cases, a, b,
More informationC C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40
C C trio@seu.edu.cn C C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 Week3 C Week5 Week5 Memory & Pointer
More informationMicrosoft PowerPoint - P766Ch06.ppt
PHP5&MySQL 程式設計 第 6 章物件導向 6-1 認識物件導向 物件 (object) 或 案例 (instance) 屬性 (property) 欄位 (field) 或 成員變數 (member variable) 方法 (method) 或 成員函式 (member function) 事件 (event) 類別 (class) 物件導向程式設計 (OOP) 具有下列特點 : 封裝
More informationThe Embedded computing platform
嵌入式系統及實驗 Embedded System and Experiment 詹曉龍 長庚大學電機系 Java 的類別與物件 : 宣告類別 建構子 public class Customer { private String name; private String address; // Customer 類別宣告 // 成員資料 public int age; // 建構子 : 使用參數設定成員資料初始值
More information2013 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("%
2013 ( 28 ) ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 10 B 1 C 1 D 5 E 5 F 1 G II 5 H 30 1 2013 C 1 #include 2 int main(void) 3
More informationMicrosoft Word - chap13.doc
101 13. More on Inheritance in C++ 13.1 Virtual Bases and Diamond Hierarchies 13.1.1 Virtual Bases and Constructor Diamond hierarchies 為 C++ 多 重 繼 承 之 一 大 問 題 程 式 範 例 : oop_ex116.cpp 對 於 此,C++ 提 供 了 virtual
More informationFactory Methods
Factory Methods 工 厂 方 法 eryar@163.com 摘 要 Abstract: 本 文 主 要 是 对 API Design for C++ 中 Factory Methods 章 节 的 翻 译, 若 有 不 当 之 处, 欢 迎 指 正 关 键 字 Key Words:C++ Factory Pattern 一 概 述 Overview 工 厂 方 法 是 创 建 型 模
More information第3章.doc
3 3 3 3.1 3 IT Trend C++ Java SAP Advantech ERPCRM C++ C++ Synopsys C++ NEC C C++PHP C++Java C++Java VIA C++ 3COM C++ SPSS C++ Sybase C++LinuxUNIX Motorola C++ IBM C++Java Oracle Java HP C++ C++ Yahoo
More informationC/C++语言 - C/C++数据
C/C++ C/C++ Table of contents 1. 2. 3. 4. char 5. 1 C = 5 (F 32). 9 F C 2 1 // fal2cel. c: Convert Fah temperature to Cel temperature 2 # include < stdio.h> 3 int main ( void ) 4 { 5 float fah, cel ;
More information運算子多載 Operator Overloading
函數樣板 (Function Template) 與 類別樣板 (Class Template) 講師 : 洪安 1 資料結構與 C++ 程式設計進階班 為何需要通用函數? (1/2) int abs(int x) { return (x>0)?x:-x; 取名困難不好記 float fabs(float x) { return (x>0)?x:-x; complex cabs(complex x)
More information提问袁小兵:
C++ 面 试 试 题 汇 总 柯 贤 富 管 理 软 件 需 求 分 析 篇 1. STL 类 模 板 标 准 库 中 容 器 和 算 法 这 部 分 一 般 称 为 标 准 模 板 库 2. 为 什 么 定 义 虚 的 析 构 函 数? 避 免 内 存 问 题, 当 你 可 能 通 过 基 类 指 针 删 除 派 生 类 对 象 时 必 须 保 证 基 类 析 构 函 数 为 虚 函 数 3.
More informationMicrosoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式]
用户定义类型 User-defined Datatypes classes and structs 几何向量 (Geometry Vector) 二维平面上的向量由起点和终点构成 每个点包含两个坐标 (x, y), 因此一个向量需要四个实数表示 Start= (0.9,1.5) Start= (0.4,0.8) int main() { double xstart = 0.4; double xend
More informationuntitled
A, 3+A printf( ABCDEF ) 3+ printf( ABCDEF ) 2.1 C++ main main main) * ( ) ( ) [ ].* ->* ()[] [][] ** *& char (f)(int); ( ) (f) (f) f (int) f int char f char f(int) (f) char (*f)(int); (*f) (int) (
More informationMicrosoft Word - 第7章 类与对象.docx
第 7 章类与对象 案例 1 地址类 #include class Address char Name[21]; char Street[51]; char City[51]; char Postcode[10]; SetAddress(char *name,char *street,char *city,char *postcode); void ChangeName(char
More informationMicrosoft Word - 投影片ch11
Java2 JDK5.0 教學手冊第三版洪維恩編著博碩文化出版書號 pg20210 第十一章抽象類別與介面 本章學習目標認識抽象類別學習介面的使用認識多重繼承與介面的延伸 抽象類別與介面 11-2 11.1 抽象類別 抽象類別的目的是要依據它的格式來修改並建立新的類別 11.1.1 定義抽象類別 定義抽象類別的語法如下 : abstract class 類別名稱 { 宣告資料成員 ; // 定義抽象類別
More information全国计算机技术与软件专业技术资格(水平)考试
全 国 计 算 机 技 术 与 软 件 专 业 技 术 资 格 ( 水 平 ) 考 试 2008 年 上 半 年 程 序 员 下 午 试 卷 ( 考 试 时 间 14:00~16:30 共 150 分 钟 ) 试 题 一 ( 共 15 分 ) 阅 读 以 下 说 明 和 流 程 图, 填 补 流 程 图 中 的 空 缺 (1)~(9), 将 解 答 填 入 答 题 纸 的 对 应 栏 内 [ 说 明
More information内团发﹝2016﹞13号内蒙古团委脱贫攻坚部门分工方案
共 青 团 内 蒙 古 自 治 区 委 员 会 文 件 内 团 发 2016 13 号 关 于 落 实 关 于 共 青 团 助 力 脱 贫 攻 坚 战 的 实 施 方 案 的 分 工 方 案 机 关 各 部 ( 室 ) 二 级 单 位, 各 盟 市 团 委, 满 洲 里 二 连 浩 特 市 团 委, 各 高 等 院 校 大 厂 矿 企 业 团 委, 自 治 区 直 属 机 关 团 工 委, 自 治
More informationScott 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
Effective Modern C++ C++ C++ C++11/C++14 C++ Scott Meyers Gerhard Kreuzer Siemens AG Effective Modern C++ Effective Modern C++ Andrei Alexandrescu Facebook Modern C++ Design C++ C++ Nevin Liber DRW Trading
More informationChapter12 Derived Classes
继 承 -- 派 生 类 复 习 1. 有 下 面 类 的 说 明, 有 错 误 的 语 句 是 : class X { A) const int a; B) X(); C) X(int val) {a=2 D) ~X(); 答 案 :C 不 正 确, 应 改 成 X(int val) : a(2) { 2. 下 列 静 态 数 据 成 员 的 特 性 中, 错 误 的 是 A) 说 明 静 态 数
More informationVolume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number 5.2007 Volume 2.Number
More information1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F
1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET 2.0 2.0.NET Framework.NET Framework 2.0 ( 3).NET Framework 2.0.NET Framework ( System ) o o o o o o Boxing UnBoxing() o
More information投影片 1
資料庫管理程式 ( 補充教材 -Part2) 使用 ADO.NET 連結資料庫 ( 自行撰寫程式碼 以實現新增 刪除 修改等功能 ) Private Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click ' 宣告相關的 Connection
More informationMicrosoft PowerPoint - plan08.ppt
程 序 设 计 语 言 原 理 Principle of Programming Languages 裘 宗 燕 北 京 大 学 数 学 学 院 2012.2~2012.6 8. 面 向 对 象 为 什 么 需 要 面 向 对 象? OO 语 言 的 发 展 面 向 对 象 的 基 本 概 念 封 装 和 继 承 初 始 化 和 终 结 处 理 动 态 方 法 约 束 多 重 继 承 总 结 2012
More informationKillTest 质量更高 服务更好 学习资料 半年免费更新服务
KillTest 质量更高 服务更好 学习资料 http://www.killtest.cn 半年免费更新服务 Exam : 310-055Big5 Title : Sun Certified Programmer for the Java 2 Platform.SE 5.0 Version : Demo 1 / 22 1. 11. public static void parse(string str)
More information第七章 繼承
繼承概念的優點 類別再使用 ( 程式碼再使用 ) 抽象化概念再使用 類別關係階層化 1 2-3 簡介繼承 Q: Q: 人 黑猩猩與猴子的有哪些共同屬性? 繼承 靈長類特徵 (( 屬性 )) -- 手 足 脊椎 大拇指型態行為 :: -- 育兒 :: 哺乳 -- 使用工具 人類 黑猩猩 彌猴 人類是靈長類的一種 人類繼承了靈長類應有的特徵及行為 人類繼承了靈長類 2 繼承的概念 員工 (employee)
More informationPowerPoint Presentation
第六章簡介運算子超載 (Operator Overloading) 6-1 運算子超載的基礎 6-2 超載二元運算子 6-3 超載邏輯與關係運算子 6-4 超載一元運算子 6-5 使用夥伴函數 6-6 細部檢視指定運算子 6-7 超載註標運算子 6-1 運算子超載的基礎 甚麼是運算子超載? 讓運算子 ( 符號 ) 有不同的意義 EX: 運算子的預設意義 ( 以 + 與 = 為例 ) class frac
More information第二章 簡介類別
Instructor Hsueh-Wen Tseng 曾學文,hwtseng@nchu.edu.tw Textbook C++ 程式設計風格與藝術 (O Reilly). Requirements Assignment x? 100% TAs 第一章概觀 C++ 1-2 二種版本的 C++ 1-5 初步檢視類別 1-1 何謂物件導向程式設計 1-8 C++ 的關鍵字 1-2 二種版本的 C++ //
More information《大话设计模式》第一章
第 1 章 代 码 无 错 就 是 优? 简 单 工 厂 模 式 1.1 面 试 受 挫 小 菜 今 年 计 算 机 专 业 大 四 了, 学 了 不 少 软 件 开 发 方 面 的 东 西, 也 学 着 编 了 些 小 程 序, 踌 躇 满 志, 一 心 要 找 一 个 好 单 位 当 投 递 了 无 数 份 简 历 后, 终 于 收 到 了 一 个 单 位 的 面 试 通 知, 小 菜 欣 喜
More information文档 3
1 2 3 4 5 6 / A B A B B A 7 8 9 10 11 12 OO A B A B 13 14 15 16 17 18 19 20 21 22 OOA OOA 23 24 25 OOA OOA 26 27 28 29 30 31 32 use case 33 use case 34 35 36 37 OOD OOA OOD 38 OOA 39 OOD 40 41 / 42 OOD
More information任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class
踩地雷遊戲 高慧君南港高中 開啟專案 MineSweep 任務一 : 產生 30X20 個磚塊編輯 Table 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class MyWorld
More informationAdobe® Flash® 的 Adobe® ActionScript® 3.0 程式設計
337 18 Adobe Flash CS4 Professional MovieClip ActionScript Flash ActionScript Flash Flash Flash MovieClip MovieClip ActionScript ( ) MovieClip Flash Sprite ActionScript MovieClip ActionScript 3.0 Shape
More information(procedure-oriented)?? 2
1 (procedure-oriented)?? 2 (Objected-Oriented) (class)? (method)? 3 : ( 4 ???? 5 OO 1966 Kisten Nygaard Ole-Johan Dahl Simula Simula 爲 6 Smalltalk Alan Kay 1972 PARC Smalltalk Smalltalk 爲 Smalltalk 爲 Smalltalk
More informationMicrosoft PowerPoint - ch11.ppt
11 前處理指令 前處理指令可以要求前處理器 (preprocessor) 在程式編譯之前, 先進行加入其它檔案的內容 文字取代以及選擇性編譯等工作 1/39 前處理指令 11.1 11.2 11.3 11.4 11.5 前處理器使用 #define 進行文字取代使用 #define 設定巨集指令條件式編譯其他與編譯器有關的前處理指令 2/39 3/39 前處理指令 #include 前處理指令不是
More information運算子多載 Operator Overloading
多載 Overloading 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 函數多載 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 運算子多載 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 函數多載 Function
More informationMicrosoft PowerPoint - SAGE 2010
SAGE Journals Online -Communication Studies 大綱 SAGE 簡介 Communication Studies 收錄內容 SJO 平台功能介紹 首頁 瀏覽功能 檢索功能 進階服務 SAGE Content 超過 520 種人文 社會科學 理工 科技領域電子期刊 SAGE 與超過 245 個國際知名的學會合作 ( 包括 American Sociological
More information1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity
1 C++ 1 C++ Primer C++ (giantchen@gmail.com) 2012-7-11 Creative Commons - - 3.0 Unported (cc by-nc-nd) http://creativecommons.org/licenses/by-nc-nd/3.0/ 1 C++ 2009 Stanley Lippman C++ C++ Java/C#/Python
More informationuntitled
3 C++ 3.1 3.2 3.3 3.4 new delete 3.5 this 3.6 3.7 3.1 3.1 class struct union struct union C class C++ C++ 3.1 3.1 #include struct STRING { typedef char *CHARPTR; // CHARPTR s; // int strlen(
More informationMicrosoft Word - JAVA Programming Language Homework I ans
JAVA Programming Language Homework I - OO concept Student ID: Name: 1. Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class? A. Declare
More information程序设计语言及基础
Chapter 10 Classes: A Deeper Look, Part 2 http://jssec.seu.edu.cn 杨明 yangming2002@seu.edu.cn OBJECTIVES To specify const (constant) objects and const member functions. To create objects composed of other
More information踏出C++的第一步
踏出 C++ 的第一步 講師 : 洪安 1 已經學會的 C 語言基本概念 基本資料型態 變數 基本輸入輸出 控制敘述 選擇控制 迴圈 陣列 函式 指標 字元與字串 結構 檔案處理 2 C v.s. C++ C 函數 程序式語言 Procedural language 結構化程式設計 Structured programming 演算法 Top-down C++ 類別 物件導向程式設計 Object-Oriented
More informationMicrosoft Word - 01.DOC
第 1 章 JavaScript 简 介 JavaScript 是 NetScape 公 司 为 Navigator 浏 览 器 开 发 的, 是 写 在 HTML 文 件 中 的 一 种 脚 本 语 言, 能 实 现 网 页 内 容 的 交 互 显 示 当 用 户 在 客 户 端 显 示 该 网 页 时, 浏 览 器 就 会 执 行 JavaScript 程 序, 用 户 通 过 交 互 式 的
More informationPython a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2
Chapter 02 變數與運算式 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.2 2.2.1 2.2.2 2.2.3 type 2.2.4 2.3 2.3.1 print 2.3.2 input 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 + 2.4.6 Python Python 2.1 2.1.1 a p p l e b e a r c 65438790
More information主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new
ListView 自訂排版 主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new int[]{r.drawable.dog1, R.drawable.dog2,
More information第1章
第 8 章 函式 1 本章提要 8.1 前言 8.2 如何定義函式 8.3 函式的呼叫和返回 8.4 傳遞陣列 8.5 方法多載 8.6 遞迴 8.7 綜合練習 8.8 後記 2 8.1 前言 每一種高階程式語言都有提供函式 (Function)( 或稱函數 ) 的功能, 以便將經常使用到的程式功能包裝成函式的形式, 如此一來便能反覆地呼叫該函式來完成某件特定工作在高階程式語言中, 副程式 (Subroutine)
More informationOOP with Java 通知 Project 4: 5 月 2 日晚 9 点
OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public char
More informationint *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++;
Memory & Pointer trio@seu.edu.cn 2.1 2.1.1 1 int *p int a 0x00C7 0x00C7 0x00C7 2.1.2 2 int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++; 2.1.3 1. 2. 3. 3 int A,
More informationOOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数
OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double
More informationMicrosoft PowerPoint - 07-overloaded.ppt
Overloaded Functions 前言 處理多載函式宣告的規則 處理多載函式呼叫的規則 多載函式與 scope 函式呼叫的議決 前言 C 語言規定 : 函式的名稱不可相同 這樣的規定使得我們必須為功能相近但參數型態相異的函式取不同的名稱, 譬如 : int imax (int, int); double dmax (double, double ); // max function for
More informationFun 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
References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22 Fun Time (1) What happens in memory? 1 i n t i ; 2
More informationCC213
: (Ken-Yi Lee), E-mail: feis.tw@gmail.com 9 [P.11] : Dev C++ [P.12] : http://c.feis.tw [P.13] [P.14] [P.15] [P.17] [P.23] Dev C++ [P.24] [P.27] [P.34] C / C++ [P.35] 10 C / C++ C C++ C C++ C++ C ( ) C++
More informationEJB-Programming-3.PDF
:, JBuilder EJB 2.x CMP EJB Relationships JBuilder EJB Test Client EJB EJB Seminar CMP Entity Beans Value Object Design Pattern J2EE Design Patterns Value Object Value Object Factory J2EE EJB Test Client
More informationcoverage2.ppt
Satellite Tool Kit STK/Coverage STK 82 0715 010-68745117 1 Coverage Definition Figure of Merit 2 STK Basic Grid Assets Interval Description 3 Grid Global Latitude Bounds Longitude Lines Custom Regions
More informationMicrosoft Word - 3D手册2.doc
第 一 章 BLOCK 前 处 理 本 章 纲 要 : 1. BLOCK 前 处 理 1.1. 创 建 新 作 业 1.2. 设 定 模 拟 控 制 参 数 1.3. 输 入 对 象 数 据 1.4. 视 图 操 作 1.5. 选 择 点 1.6. 其 他 显 示 窗 口 图 标 钮 1.7. 保 存 作 业 1.8. 退 出 DEFORMTM3D 1 1. BLOCK 前 处 理 1.1. 创 建
More informationOOP with Java 通知 Project 4: 5 月 2 日晚 9 点
OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class - Table.class
More informationRunPC2_.doc
PowerBuilder 8 (5) PowerBuilder Client/Server Jaguar Server Jaguar Server Connection Cache Thin Client Internet Connection Pooling EAServer Connection Cache Connection Cache Connection Cache Connection
More informationOOP with Java 通知 Project 4: 4 月 19 日晚 9 点
OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public char
More informationebook50-11
11 Wi n d o w s C A D 53 M F C 54 55 56 57 58 M F C 11.1 53 11-1 11-1 MFC M F C C D C Wi n d o w s Wi n d o w s 4 11 199 1. 1) W M _ PA I N T p W n d C W n d C D C * p D C = p W n d GetDC( ); 2) p W n
More informationebook39-5
5 3 last-in-first-out, LIFO 3-1 L i n e a r L i s t 3-8 C h a i n 3 3. 8. 3 C + + 5.1 [ ] s t a c k t o p b o t t o m 5-1a 5-1a E D 5-1b 5-1b E E 5-1a 5-1b 5-1c E t o p D t o p D C C B B B t o p A b o
More informationRUN_PC連載_10_.doc
PowerBuilder 8 (10) Jaguar CTS ASP Jaguar CTS PowerDynamo Jaguar CTS Microsoft ASP (Active Server Pages) ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar CTS ASP Jaguar Server ASP
More informationPowerPoint Presentation
C++ 與資料結構 NTU CSIE 大綱 使用類別 (Class) 建立資料結構 使用繼承 (Inheritance) 建立資料結構 C++ 物件導向 以物件為基礎的程式設計, 將程式中互動的單元視為一個個的物件 封裝 (Encapsulation) 封裝物件資訊是第一步, 您要瞭解如何使用類別定義物件, 像是定義物件的屬性 方法 ( 行為 ) 等等, 類別是建構物件時所依賴的規格書 Example
More informationuntitled
1 Outline 料 類 說 Tang, Shih-Hsuan 2006/07/26 ~ 2006/09/02 六 PM 7:00 ~ 9:30 聯 ives.net@gmail.com www.csie.ntu.edu.tw/~r93057/aspnet134 度 C# 力 度 C# Web SQL 料 DataGrid DataList 參 ASP.NET 1.0 C# 例 ASP.NET 立
More informationC++11概要 ライブラリ編
C++11 Egtra 2012 6 23 1 Boost. #9 1.1 C++11 1.2 http://creativecommons.org/licenses/by-sa/2.1/jp/ - 2.1 2 Misc 2.1 C++11 unique_ptr shared_ptr // #include std::unique_ptr up(new int(1));
More informationCC213
: (Ken-Yi Lee), E-mail: feis.tw@gmail.com 49 [P.51] C/C++ [P.52] [P.53] [P.55] (int) [P.57] (float/double) [P.58] printf scanf [P.59] [P.61] ( / ) [P.62] (char) [P.65] : +-*/% [P.67] : = [P.68] : ,
More informationMicrosoft PowerPoint - Lecture7II.ppt
Lecture 8II SUDOKU PUZZLE SUDOKU New Play Check 軟體實作與計算實驗 1 4x4 Sudoku row column 3 2 } 4 } block 1 4 軟體實作與計算實驗 2 Sudoku Puzzle Numbers in the puzzle belong {1,2,3,4} Constraints Each column must contain
More informationbingdian001.com
TSM12M TSM12 STM8L152C6, STM8L152R8 MSP430F5325 whym1987@126.com! /******************************************************************************* * : TSM12.c * : * : 2013/10/21 * : TSM12, STM8L f(sysclk)
More informationEJB-Programming-4-cn.doc
EJB (4) : (Entity Bean Value Object ) JBuilder EJB 2.x CMP EJB Relationships JBuilder EJB Test Client EJB EJB Seminar CMP Entity Beans Session Bean J2EE Session Façade Design Pattern Session Bean Session
More informationMicrosoft PowerPoint - 10 模板 Template.pptx
模板 Tempalte 泛型编程的需要 Why Templates? 设想你对整数类型实现了一个排序算法 : void sort(int *is,int n); 用该函数可以对实 复数或工资单排序吗? 模板可以复用源代码 - 泛型编程. inline void Swap( int &x, int &y){ int t = x; x = y; y =t; inline void Swap(double
More informationOOP with Java 通知 Project 4: 4 月 19 日晚 9 点
OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d
More information投影片 1
Chap 8 繼承 抽象類別與介面 8-1 類別的繼承 8-2 介面 8-3 介面的繼承 8-4 抽象類別 8-5 抽象類別與介面 8-6 常數類別 8-1 類別的繼承 8-1-1 類別架構 8-1-2 類別的繼承 8-1-3 覆寫和隱藏父類別的方法 8-1-4 隱藏父類別的成員變數 8-1-5 使用父類別的建構子 8-1-1 類別架構 - 繼承關係 類別的繼承關係可以讓我們建立類別架構, 在 UML
More information斯 福 也 因 此 被 認 為 是 美 國 歷 史 上 最 偉 大 的 總 統 之 一 由 於 生 育 率 一 直 比 較 高 ( 美 國 到 1960 年 每 個 婦 女 生 育 孩 子 數 還 接 近 四 個 ), 而 當 時 人 口 壽 命 不 長 (1940 年, 美 國 人 口 的 平 均
第 14 章 計 劃 生 育 政 策 嚴 重 威 脅 中 國 的 持 續 發 展 14.1 老 有 所 養, 誰 來 養? 老 有 所 養 對 社 會 的 穩 定 非 常 重 要, 對 個 人 更 為 重 要, 社 會 養 老 更 是 人 類 幾 千 年 的 夢 想 很 多 人 指 望 能 通 過 建 立 西 方 國 家 現 在 這 樣 的 養 老 制 度 來 解 決 養 老 問 題 事 實 上 西
More informationOOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点
OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public
More information