Microsoft PowerPoint - 09_Inheritance.ppt

Size: px
Start display at page:

Download "Microsoft PowerPoint - 09_Inheritance.ppt"

Transcription

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

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

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

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

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

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

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

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

9 基礎類別與衍生類別的關係 範例程式 : 點與圓 (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 ); // 內定建構子 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 18 9

10 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; } // end Point constructor // 設定 x 座標 18 void Point::setX( int xvalue ) 19 { 20 x = xvalue; // 將 x 座標的值設定成 xvalue, 沒有檢查動作 } // end function setx point.cpp (1 of 2) 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; // 將 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 << '[' << x << ", " << y << ']'; } // end function print 20 point.cpp (2 of 2) 10

11 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 物件 // 顯示點的座標 15 cout << "X coordinate is " << point.getx() 16 << "\ny coordinate is " << point.gety(); point.setx( 10 ); // 設定 x 座標 19 point.sety( 10 ); // 設定 y 座標 // 顯示目前的點座標 22 cout << "\n\nthe new location of point is "; 23 point.print(); 24 cout << endl; 25 pointtest.cpp (1 of 2) return 0; // 程式正常結束 } // 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

12 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 ); void setx( int ); // 設定圓心的 x 座標 14 int getx() const; // 傳回圓心的 x 座標 void sety( int ); // 設定圓心的 y 座標 17 int gety() const; // 傳回圓心的 y 座標 void setradius( double ); // 設定半徑 20 double getradius() const; // 傳回半徑 double getdiameter() const; // 傳回直徑 23 double getcircumference() const; // 傳回圓周長度 24 double getarea() const; // 傳回面積 25 circle.h (1 of 2) void print() const; // 印出 Circle 內容 private: 29 int x; // 圓心的 x 座標 30 int y; // 圓心的 x 座標 31 double radius; // 半徑 }; // end class Circle #endif circle.h (2 of 2) 24 12

13 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 ); } // end Circle constructor // 設定圓心的 x 座標 19 void Circle::setX( int xvalue ) 20 { 21 x = xvalue; // 設定圓心的 x 座標為 xvalue, 沒有檢查動作 } // end function setx circle.cpp (1 of 4) 25 // 取得圓心的 x 座標 26 int Circle::getX() const 27 { 28 return x; } // end function getx // 設定圓心的 y 座標 33 void Circle::setY( int yvalue ) 34 { 35 y = yvalue; // 設定圓心的 y 座標為 yvalue, 沒有檢查動作 } // end function sety // 取得圓心的 y 座標 40 int Circle::getY() const 41 { 42 return y; } // end function gety circle.cpp (2 of 4) 13

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

15 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> using std::setprecision; #include circle.h // Circle 類別定義 int main() 16 { 17 Circle circle( 37, 43, 2.5 ); // 建立一個 Circle 物件 // 印出圓心座標與半徑 20 cout << "X coordinate is " << circle.getx() 21 << "\ny coordinate is " << circle.gety() 22 << "\nradius is " << circle.getradius(); 23 circletest.cpp (1 of 2) circle.setx( 2 ); // 設定新的圓心 x 座標 25 circle.sety( 2 ); // 設定新的圓心 y 座標 26 circle.setradius( 4.25 ); // 設定新的半徑 // 使用 print() 函式印出圓的內容 29 cout << "\n\nthe new location and radius of circle are\n"; 30 circle.print(); // 設定印出數值的精確度到小數以下第 2 位 33 cout << fixed << setprecision( 2 ); // 印出圓的直徑 36 cout << "\ndiameter is " << circle.getdiameter(); // 印出圓周長度 39 cout << "\ncircumference is " << circle.getcircumference(); // 印出圓的面積 42 cout << "\narea is " << circle.getarea(); cout << endl; return 0; // 程式正常結束 } // end main circletest.cpp (2 of 2) 30 15

16 // 印出圓心座標及半徑 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 // 印出圓的面積 Area is 使用繼承 Point 的方式實作 Circle 類別 錯誤版本 16

17 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: // 內定建構子 13 Circle2( 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; // 印出 Circle2 物件內容 private: 25 double radius; // Circle2 半徑 }; // end class Circle #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 ); } // end Circle2 constructor // 設定半徑 19 void Circle2::setRadius( double radiusvalue ) 20 { 21 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); } // end function setradius circle2.cpp (1 of 3) 34 17

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

19 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

20 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 ); // 內定建構子 void setx( int ); // 設定 x 座標 12 int getx() const; // 取得 x 座標 void sety( int ); // 設定 y 座標 15 int gety() const; // 取得 y 座標 void print() const; // 印出 Point2 物件內容 protected: 20 int x; // x 座標 21 int y; // y 座標 }; // end class Point #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; } // end Point2 constructor // 設定 x 座標 18 void Point2::setX( int xvalue ) 19 { 20 x = xvalue; } // end function setx 23 point2.cpp (1 of 2) 40 20

21 24 // 取得 x 座標的值 25 int Point2::getX() const 26 { 27 return x; } // end function getx // 設定 y 座標 32 void Point2::setY( int yvalue ) 33 { 34 y = yvalue; } // end function sety // 取得 y 座標的值 39 int Point2::getY() const 40 { 41 return y; } // end function gety // 印出 Point2 物件內容 46 void Point2::print() const 47 { 48 cout << '[' << x << ", " << y << ']'; } // 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: // 內定建構子 13 Circle3( 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; // 印出 Circle3 物件 }; // end class Circle private: 25 double radius; // 半徑 注意 :Circle3 沒有定義 1.x,y 2.setX, sety, getx, gety 29 #endif circle3.h 42 21

22 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 ); } // end Circle3 constructor // 設定半徑 19 void Circle3::setRadius( double radiusvalue ) 20 { 21 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); } // end function setradius // 傳回半徑 26 double Circle3::getRadius() const 27 { 28 return radius; } // end function getradius circle3.cpp (1 of 2) // 計算及傳回直徑 33 double Circle3::getDiameter() const 34 { 35 return 2 * radius; } // end function getdiameter // 計算及傳回圓周長 40 double Circle3::getCircumference() const 41 { 42 return * getdiameter(); } // end function getcircumference // 計算及傳回面積 47 double Circle3::getArea() const 48 { 49 return * radius * radius; } // end function getarea // 印出 Circle3 物件內容 54 void Circle3::print() const 55 { // 可直接存取 Point 的 x,y 成員 56 cout << "Center = [" << x << ", " << y << ']' 57 << "; Radius = " << radius; } // end function print circle3.cpp (2 of 2) 44 22

23 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> using std::setprecision; #include circle3.h // 引用 Circle3 類別定義 int main() 16 { 17 Circle3 circle( 37, 43, 2.5 ); // 建構一個 Circle3 物件 // 印出 circle 內容 20 cout << "X coordinate is " << circle.getx() 21 << "\ny coordinate is " << circle.gety() 22 << "\nradius is " << circle.getradius(); circle.setx( 2 ); // 設定圓心 x 座標, 使用 Point 的成員 25 circle.sety( 2 ); // 設定圓心 y 座標, 使用 Point 的成員 26 circle.setradius( 4.25 ); // 設定半徑 27 circletest3.cpp (1 of 2) // 印出 circle 的內容 29 cout << "\n\nthe new location and radius of circle are\n"; 30 circle.print(); // 設定輸出精確度 33 cout << fixed << setprecision( 2 ); // 輸出直徑 36 cout << "\ndiameter is " << circle.getdiameter(); // 輸出圓周長 39 cout << "\ncircumference is " << circle.getcircumference(); // 輸出圓面積 42 cout << "\narea is " << circle.getarea(); cout << endl; return 0; // 程式正常結束 } // end main circletest3.cpp (2 of 2) 46 23

24 // 印出 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 // 輸出圓面積 Area is 基礎類別與衍生類別的關係 protected 資料成員的優缺點 優點 (Advantages) 衍生類別可直接存取 程式執行效率稍高 因為沒有使用 set/get 函式呼叫 24

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

26 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 ); // 預設建構子 void setx( int ); // 設定 x 座標 12 int getx() const; // 取得 x 座標 void sety( int ); // 設定 y 座標 15 int gety() const; // 取得 y 座標 void print() const; // 印出 Point3 物件內容 private: 20 int x; // x 座標 21 int y; // y 座標 }; // end class Point #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 // 空的 } // end Point3 constructor // 設定 x 座標 18 void Point3::setX( int xvalue ) 19 { 20 x = xvalue; } // end function setx // 傳回 x 座標 25 int Point3::getX() const 26 { 27 return x; } // end function getx point3.cpp (1 of 2) 52 26

27 30 31 // 設定 y 座標 32 void Point3::setY( int yvalue ) 33 { 34 y = yvalue; } // end function sety // 傳回 y 座標 39 int Point3::getY() const 40 { 41 return y; } // end function gety // 印出 Point3 物件 46 void Point3::print() const 47 { 48 cout << '[' << getx() << ", " << gety() << ']'; } // 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: // 內定建構子 13 Circle4( 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; // 印出 Circle4 物件內容 private: 25 double radius; // 半徑 }; // end class Circle #endif circle4.h 54 27

28 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 ); } // end Circle4 constructor // 設定半徑 18 void Circle4::setRadius( double radiusvalue ) 19 { 20 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); } // end function setradius // 傳回半徑 25 double Circle4::getRadius() const 26 { 27 return radius; } // end function getradius circle4.cpp (1 of 2) // 計算及傳回直徑 32 double Circle4::getDiameter() const 33 { 34 return 2 * getradius(); // 呼叫 getradius() 函式較好 } // end function getdiameter // 計算及傳回圓周長 39 double Circle4::getCircumference() const 40 { 41 return * getdiameter(); } // end function getcircumference // 計算及傳回面積 46 double Circle4::getArea() const 47 { 48 return * getradius() * getradius(); } // end function getarea // 印出 Circle4 物件內容 53 void Circle4::print() const 54 { 55 cout << "Center = "; 56 Point3::print(); // 呼叫 Point3 的 print 函式 57 cout << "; Radius = " << getradius(); } // end function print circle4.cpp (2 of 2) 56 28

29 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> using std::setprecision; #include circle4.h // Circle4 類別定義 int main() 16 { 17 Circle4 circle( 37, 43, 2.5 ); // 建構 Circle4 物件 // 印出圓的內容 20 cout << X coordinate is << circle.getx() //point 的函式 21 << "\ny coordinate is " << circle.gety() //point 的函式 22 << "\nradius is " << circle.getradius(); circletest4.cpp (1 of 2) 24 circle.setx( 2 ); // 設定 x 座標,point 函式 25 circle.sety( 2 ); // 設定 y 座標,point 函式 26 circle.setradius( 4.25 ); // 設定圓心 // 印出圓的內容 29 cout << "\n\nthe new location and radius of circle are\n"; 30 circle.print(); // 設定輸出精確度 33 cout << fixed << setprecision( 2 ); // 印出 Circle4 的直徑 36 cout << "\ndiameter is " << circle.getdiameter(); // 印出 Circle4 的圓周 39 cout << "\ncircumference is " << circle.getcircumference(); // 印出 Circle4 的面積 42 cout << "\narea is " << circle.getarea(); cout << endl; return 0; // 正常結束程式 } // end main circletest4.cpp (2 of 2) 58 29

30 // 印出圓的內容 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 // 印出 Circle4 的面積 Area is 案例研究 : 三層繼承 三層繼承 point/circle/cylinder Point x,y 座標 Circle x,y 座標 Radius Cylinder x,y 座標 Radius Height 30

31 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: // 內定建構子 13 Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); void setheight( double ); // 設定 Cylinder 高度 16 double getheight() const; // 傳回 Cylinder 高度 double getarea() const; // 傳回 Cylinder 面積 19 double getvolume() const; // 傳回 Cylinder 體積 20 void print() const; // 印出 Cylinder 物件內容 private: 23 double height; // Cylinder 高度 }; // end class Cylinder #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 ); } // end Cylinder constructor // 設定 Cylinder 高度 19 void Cylinder::setHeight( double heightvalue ) 20 { 21 height = ( heightvalue < 0.0? 0.0 : heightvalue ); } // end function setheight // 取得 Cylinder 高度 26 double Cylinder::getHeight() const 27 { 28 return height; } // end function getheight cylinder.cpp (1 of 2) 62 31

32 31 32 // 重新定義 Circle4 的 getarea 函式, 計算 Cylinder 面積 33 double Cylinder::getArea() const 34 { 35 return 2 * Circle4::getArea() + 36 getcircumference() * getheight(); } // end function getarea // 計算 Cylinder 體積 41 double Cylinder::getVolume() const 42 { // 呼叫 Circle4 的 getarea 函式 43 return Circle4::getArea() * getheight(); } // end function getvolume // 輸出 Cylinder 物件 48 void Cylinder::print() const 49 { 50 Circle4::print();// 呼叫 Circle4 的 print 函式 51 cout << "; Height = " << getheight(); } // 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> using std::setprecision; #include cylinder.h // 引用 Cylinder 類別定義 int main() 16 { 17 // 建構 Cylinder 物件 18 Cylinder cylinder( 12, 23, 2.5, 5.7 ); // 印出 cylinder 內容 21 cout << "X coordinate is " << cylinder.getx() 22 << "\ny coordinate is " << cylinder.gety() 23 << "\nradius is " << cylinder.getradius() 24 << "\nheight is " << cylinder.getheight(); 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

33 30 31 // 印出新的 cylinder 內容 32 cout << "\n\nthe new location and radius of circle are\n"; 33 cylinder.print(); // 設定輸出精確度 36 cout << fixed << setprecision( 2 ); // 印出 cylinder 直徑 39 cout << "\n\ndiameter is " << cylinder.getdiameter(); // 印出 cylinder 圓周長度 42 cout << "\ncircumference is " 43 << cylinder.getcircumference(); // 印出 cylinder 面積 46 cout << "\narea is " << cylinder.getarea(); // 印出 cylinder 體積 49 cout << "\nvolume is " << cylinder.getvolume(); cout << endl; return 0; // 程式正常結束 } // 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 // 印出 cylinder 面積 Area is // 印出 cylinder 體積 Volume is cylindertest.cpp output 66 33

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

35 衍生類別的建構子與解構子 基礎類別的建構子, 解構子, 與指派運算子 (=) 不會被衍生類別繼承 衍生類別的建構子, 解構子, 與指派運算子 (=) 可呼叫 基礎類別的建構子 基礎類別的指派運算子 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(); // 解構子 void setx( int ); // 設定 x 座標 13 int getx() const; // 傳回 x 座標 void sety( int ); // 設定 y 座標 16 int gety() const; // 傳回 y 座標 void print() const; // 印出 Point3 物件內容 private: 21 int x; // x 座標 22 int y; // y 座標 }; // end class Point #endif point4.h (1 of 1) 70 35

36 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; } // end Point4 constructor // 解構子 21 Point4::~Point4() 22 { 23 cout << "Point4 destructor: "; 24 print(); 25 cout << endl; } // end Point4 destructor 28 point4.cpp (1 of 3) // 設定 x 座標 30 void Point4::setX( int xvalue ) 31 { 32 x = xvalue; } // end function setx // 傳回 x 座標 37 int Point4::getX() const 38 { 39 return x; } // end function getx // 設定 y 座標 44 void Point4::setY( int yvalue ) 45 { 46 y = yvalue; } // end function sety 49 point4.cpp (2 of 3) 72 36

37 50 // 傳回 y 座標 51 int Point4::getY() const 52 { 53 return y; } // end function gety // 印出 Point4 物件內容 58 void Point4::print() const 59 { 60 cout << '[' << getx() << ", " << gety() << ']'; } // 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: // 內定建構子 13 Circle5( int = 0, int = 0, double = 0.0 ); ~Circle5(); // 解構子 16 void setradius( double ); // 設定半徑 17 double getradius() const; // 傳回半徑 double getdiameter() const; // 傳回直徑 20 double getcircumference() const; // 傳回圓周長度 21 double getarea() const; // 傳回面積 void print() const; // 印出 Circle5 物件內容 private: 26 double radius; // Circle5 的半徑 }; // end class Circle #endif circle5.h (1 of 2) 74 37

38 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 ); cout << "Circle5 constructor: "; 17 print(); 18 cout << endl; } // end Circle5 constructor // 解構子 23 Circle5::~Circle5() 24 { 25 cout << "Circle5 destructor: "; 26 print(); 27 cout << endl; } // end Circle5 destructor 30 circle5.cpp (1 of 3) // 設定半徑 32 void Circle5::setRadius( double radiusvalue ) 33 { 34 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); } // end function setradius // 傳回半徑 39 double Circle5::getRadius() const 40 { 41 return radius; } // end function getradius // 計算及傳回直徑 46 double Circle5::getDiameter() const 47 { 48 return 2 * getradius(); } // end function getdiameter // 計算及傳回圓周長度 53 double Circle5::getCircumference() const 54 { 55 return * getdiameter(); } // end function getcircumference 58 circle5.cpp (2 of 3) 76 38

39 59 // 計算及傳回面積 60 double Circle5::getArea() const 61 { 62 return * getradius() * getradius(); } // end function getarea // 印出 Circle5 物件 67 void Circle5::print() const 68 { 69 cout << "Center = "; 70 Point4::print(); // 呼叫 Point4 的 print 函式 71 cout << "; Radius = " << getradius(); } // 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 類別定義 int main() 12 { 13 { // 建構 Point4 物件 Point4 point( 11, 22 ); } // 解構 Point4 物件 cout << endl; 20 Circle5 circle1( 72, 29, 4.5 ); // 建構 Circle5 物件 cout << endl; 23 Circle5 circle2( 5, 5, 10 ); // 建構 Circle5 物件 cout << endl; return 0; } // end main fig09_29.cpp 78 39

40 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] public, protected 與 private 繼承關係 衍生類別 Public Protected Private 基礎類別 Public Protected Private Public Protected Private Protected Protected Private Hidden Hidden Hidden 40

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

Microsoft PowerPoint - 10_Polymophism_1.ppt

Microsoft PowerPoint - 10_Polymophism_1.ppt 1 第十章 - 多型 (Polymorphism) 10.1 簡介 10.2 物件在繼承階層中的關係 10.3 多型範例程式 10.4 Type 欄位與 switch 結構 10.5 抽象類別 (Abstract Class) 10.6 案例研讀 : 繼承介面與實作 10.7 多型, 虛擬函式與動態繫結 10.8 虛擬建構子 10.9 案例研究 : 使用多型實作薪資系統 2 10.1 簡介 多型 (Polymorphism)

More information

Microsoft Word - chap10.doc

Microsoft Word - chap10.doc 78 10. Inheritance in C++ 我 們 已 介 紹 了 物 件 導 向 程 式 的 第 一 個 主 要 特 性, 即 程 式 可 模 組 化 成 為 類 別 ( 物 件 ), 類 別 具 有 資 料 封 裝 的 特 性 接 下 來 我 們 要 介 紹 物 件 導 向 程 式 的 另 一 個 主 要 特 性, 那 就 是 類 別 具 有 繼 承 的 功 能 繼 承 就 是 重 複

More information

Strings

Strings Polymorphism and Virtual Functions Cheng-Chin Chiang Virtual Function Basics 多 型 (Polymorphism) 賦 予 一 個 函 數 多 種 意 涵, 存 在 於 同 一 類 別 之 內 祖 先 類 別 與 後 代 類 別 間 物 件 導 向 程 式 設 計 基 本 原 理 虛 擬 函 數 (Virtual Function)

More information

Microsoft PowerPoint - 11_Templates.ppt

Microsoft 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

Strings

Strings Inheritance Cheng-Chin Chiang Relationships among Classes A 類 別 使 用 B 類 別 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人 類 使 用 交 通 工 具 旅 行 A 類 別 中 有 B 類 別 汽 車 有 輪 子 三 角 形 有 三 個 頂 點 電 腦 內 有 中 央 處 理 單 元 A

More information

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft 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

第七讲 继承与多态

第七讲  继承与多态 第 七 章 继 承 与 派 生 1 本 章 主 要 内 容 的 继 承 成 员 的 访 问 控 制 单 继 承 与 多 继 承 派 生 的 构 造 析 构 函 数 成 员 的 标 识 与 访 问 深 度 探 索 2 的 继 承 与 派 生 的 继 承 与 派 生 保 持 已 有 的 特 性 而 构 造 新 的 过 程 称 为 继 承 在 已 有 的 基 础 上 新 增 自 己 的 特 性 而 产 生

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 多型 Polymorphism 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 子類別與父類別物件間的指定 (assignment)

More information

c_cpp

c_cpp C C++ C C++ C++ (object oriented) C C++.cpp C C++ C C++ : for (int i=0;i

More information

Microsoft PowerPoint - P766Ch06.ppt

Microsoft PowerPoint - P766Ch06.ppt PHP5&MySQL 程式設計 第 6 章物件導向 6-1 認識物件導向 物件 (object) 或 案例 (instance) 屬性 (property) 欄位 (field) 或 成員變數 (member variable) 方法 (method) 或 成員函式 (member function) 事件 (event) 類別 (class) 物件導向程式設計 (OOP) 具有下列特點 : 封裝

More information

untitled

untitled (encapsulation) 例 類 說 類 料 來 料 information hiding 念 (inheritance) 來說 類 類 類 類 類 類 行 利 來 (polymorphism) 不 類 數 不 1 2 3 4 類 類 不 類 不 類 5 6 7 // virtual 不見了 #include #include using namespace

More information

Microsoft Word - ch04三校.doc

Microsoft 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 information

新版 明解C++入門編

新版 明解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 information

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

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 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 information

Chapter 9: Objects and Classes

Chapter 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 information

untitled

untitled 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

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

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 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 information

Microsoft PowerPoint - 13_Exception.ppt

Microsoft 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 information

Chapter12 Derived Classes

Chapter12   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 information

FY.DOC

FY.DOC 高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主

More information

第七章 繼承

第七章 繼承 繼承概念的優點 類別再使用 ( 程式碼再使用 ) 抽象化概念再使用 類別關係階層化 1 2-3 簡介繼承 Q: Q: 人 黑猩猩與猴子的有哪些共同屬性? 繼承 靈長類特徵 (( 屬性 )) -- 手 足 脊椎 大拇指型態行為 :: -- 育兒 :: 哺乳 -- 使用工具 人類 黑猩猩 彌猴 人類是靈長類的一種 人類繼承了靈長類應有的特徵及行為 人類繼承了靈長類 2 繼承的概念 員工 (employee)

More information

PowerPoint Presentation

PowerPoint Presentation C++ 與資料結構 NTU CSIE 大綱 使用類別 (Class) 建立資料結構 使用繼承 (Inheritance) 建立資料結構 C++ 物件導向 以物件為基礎的程式設計, 將程式中互動的單元視為一個個的物件 封裝 (Encapsulation) 封裝物件資訊是第一步, 您要瞭解如何使用類別定義物件, 像是定義物件的屬性 方法 ( 行為 ) 等等, 類別是建構物件時所依賴的規格書 Example

More information

運算子多載 Operator Overloading

運算子多載 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

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

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

第3章.doc

第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 information

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

Microsoft 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 information

<4D6963726F736F667420506F776572506F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

<4D6963726F736F667420506F776572506F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074> 程 序 设 计 实 习 INFO130048 3-2.C++ 面 向 对 象 程 序 设 计 重 载 继 承 多 态 和 聚 合 复 旦 大 学 计 算 机 科 学 与 工 程 系 彭 鑫 pengxin@fudan.edu.cn 内 容 摘 要 方 法 重 载 类 的 继 承 对 象 引 用 和 拷 贝 构 造 函 数 虚 函 数 和 多 态 性 类 的 聚 集 复 旦 大 学 计 算 机 科 学

More information

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

Microsoft 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 information

The Embedded computing platform

The Embedded computing platform 嵌入式系統及實驗 Embedded System and Experiment 詹曉龍 長庚大學電機系 Java 的類別與物件 : 宣告類別 建構子 public class Customer { private String name; private String address; // Customer 類別宣告 // 成員資料 public int age; // 建構子 : 使用參數設定成員資料初始值

More information

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

Adobe® 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

沒有投影片標題

沒有投影片標題 C++ 類 (Class Inheriance) : 料 ( 異 ), 類 料, 利 類 狀, 不 更 來 data membermember function 類 料 類 類 (Base class) 類, 類 類 (Derived class) 類 利 料 料, 料 不 料, 料 料, 留 類 類, 料, 類 來 類 列 更 類 數 例 : 類 數 類 料 例, String 類, 類 料, 串

More information

第十章 虛擬函數 (Virtual Functions)

第十章 虛擬函數   (Virtual Functions) 繼承的優點 程式碼再使用 (code reuse) 抽象概念再使用 類別階層化 澄清物件間的關係 繼承與 Code Reuse( 被動 ) 主計劃 子計劃 1 子計劃 2 子計劃 3 ( 你所在的組 ) 類別庫函式庫 (.lib.dll) 繼承與 Code Reuse class List { void insert() { void delete() { 重新改寫??? (1) 原始碼在哪? (2)

More information

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

C/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

提问袁小兵:

提问袁小兵: C++ 面 试 试 题 汇 总 柯 贤 富 管 理 软 件 需 求 分 析 篇 1. STL 类 模 板 标 准 库 中 容 器 和 算 法 这 部 分 一 般 称 为 标 准 模 板 库 2. 为 什 么 定 义 虚 的 析 构 函 数? 避 免 内 存 问 题, 当 你 可 能 通 过 基 类 指 针 删 除 派 生 类 对 象 时 必 须 保 证 基 类 析 构 函 数 为 虚 函 数 3.

More information

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

Microsoft 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 information

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

OOP 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 information

Microsoft Word - 投影片ch11

Microsoft Word - 投影片ch11 Java2 JDK5.0 教學手冊第三版洪維恩編著博碩文化出版書號 pg20210 第十一章抽象類別與介面 本章學習目標認識抽象類別學習介面的使用認識多重繼承與介面的延伸 抽象類別與介面 11-2 11.1 抽象類別 抽象類別的目的是要依據它的格式來修改並建立新的類別 11.1.1 定義抽象類別 定義抽象類別的語法如下 : abstract class 類別名稱 { 宣告資料成員 ; // 定義抽象類別

More information

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

任務二 : 產生 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 information

<4D6963726F736F667420576F7264202D2030372D313032ABFCA6D2A4BDA5C1BB50AAC0B77CB8D5C34428A977BD5A29>

<4D6963726F736F667420576F7264202D2030372D313032ABFCA6D2A4BDA5C1BB50AAC0B77CB8D5C34428A977BD5A29> 大 學 入 學 考 試 中 心 102 學 年 度 指 定 科 目 考 試 試 題 考 科 - 作 答 注 意 事 項 - 考 試 時 間 :80 分 鐘 作 答 方 式 : 選 擇 題 用 2B 鉛 筆 在 答 案 卡 上 作 答 ; 更 正 時, 應 以 橡 皮 擦 擦 拭, 切 勿 使 用 修 正 液 ( 帶 ) 未 依 規 定 畫 記 答 案 卡, 致 機 器 掃 描 無 法 辨 識 答 案

More information

Factory Methods

Factory Methods Factory Methods 工 厂 方 法 eryar@163.com 摘 要 Abstract: 本 文 主 要 是 对 API Design for C++ 中 Factory Methods 章 节 的 翻 译, 若 有 不 当 之 处, 欢 迎 指 正 关 键 字 Key Words:C++ Factory Pattern 一 概 述 Overview 工 厂 方 法 是 创 建 型 模

More information

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

OOP 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 information

C++ 程式設計

C++ 程式設計 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

運算子多載 Operator Overloading

運算子多載 Operator Overloading 多載 Overloading 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 函數多載 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 運算子多載 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 函數多載 Function

More information

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

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc 2 5 8 11 0 13 1. 13 2. 15 3. 18 1 23 1. 23 2. 26 3. 28 2 36 1. 36 2. 39 3. 42 4. 44 5. 49 6. 51 3 57 1. 57 2. 60 3. 64 4. 66 5. 70 6. 75 7. 83 8. 85 9. 88 10. 98 11. 103 12. 108 13. 112 4 115 1. 115 2.

More information

Microsoft Word - JAVA Programming Language Homework I ans

Microsoft 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

untitled

untitled 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 information

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

OOP 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

Microsoft PowerPoint - Class5.pptx

Microsoft PowerPoint - Class5.pptx C++ 程式初探 V 2015 暑期 ver. 1.0.1 C++ 程式語言 大綱 1. 大量檔案讀取 & 計算 2. 指標 3. 動態記憶體 & 動態陣列 4. 標準函式庫 (STL) vector, algorithm 5. 結構與類別 2 大量檔案讀取 & 計算 若目前有一個程式將讀取純文字文件 (.txt) 中的整數, 並將該文件中的整數有小到大排序後, 儲存到另外一個新的純文字件中 假設有

More information

untitled

untitled 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 information

《二十一世紀》網絡版

《二十一世紀》網絡版 二 十 一 世 紀 網 絡 版 二 五 年 六 月 號 總 第 39 期 2005 年 6 月 30 日 從 天 理 世 界 觀 到 公 理 世 界 觀 的 轉 換 汪 暉 關 於 中 國 現 代 性 的 論 說 陳 贇 現 代 中 國 思 想 的 興 起 探 討 的 中 心 問 題 是 現 代 中 國 的 含 義 如 何 理 解 中 國 的 現 代 這 就 必 須 追 問 現 代 的 中 國 認

More information

C 1

C 1 C homepage: xpzhangme 2018 5 30 C 1 C min(x, y) double C // min c # include # include double min ( double x, double y); int main ( int argc, char * argv []) { double x, y; if( argc!=

More information

Microsoft Word - 0.5bh.doc

Microsoft Word - 0.5bh.doc 198 FG7. 199 HG8 E 圖中,DE 為一正方形, = 及 為一邊長 1 cm 的等邊三角形, 而 為此 = 90 若 DE 的面積為 10 cm, 三角形內的任意一點 ( 如圖所示 ) 若 至三邊 求 的面積 及 的垂直距離的總和為 x cm, 求 x 的值 In the figure shown, DE is a square and is an equilateral triangle

More information

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

全国计算机技术与软件专业技术资格(水平)考试 全 国 计 算 机 技 术 与 软 件 专 业 技 术 资 格 ( 水 平 ) 考 试 2008 年 上 半 年 程 序 员 下 午 试 卷 ( 考 试 时 间 14:00~16:30 共 150 分 钟 ) 试 题 一 ( 共 15 分 ) 阅 读 以 下 说 明 和 流 程 图, 填 补 流 程 图 中 的 空 缺 (1)~(9), 将 解 答 填 入 答 题 纸 的 对 应 栏 内 [ 说 明

More information

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

山东建筑大学学分制管理规定(试行) 山 建 大 校 字 2015 67 号 山 东 建 筑 大 学 关 于 印 发 学 分 制 管 理 规 定 ( 试 行 ) 的 通 知 各 院 部 校 直 各 部 门 : 山 东 建 筑 大 学 学 分 制 管 理 规 定 ( 试 行 ) 已 经 学 校 研 究 同 意, 现 印 发 给 你 们, 请 认 真 遵 照 执 行 山 东 建 筑 大 学 2015 年 8 月 7 日 1 山 东 建 筑

More information

untitled

untitled 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 information

Microsoft Word - 970617cppFinalSolution.doc

Microsoft Word - 970617cppFinalSolution.doc 國 立 台 灣 海 洋 大 學 資 訊 工 程 系 C++ 程 式 設 計 期 末 考 參 考 答 案 姓 名 : 系 級 : 學 號 : 97/06/17 考 試 時 間 :10:00 12:10 試 題 敘 述 蠻 多 的, 看 清 楚 題 目 問 什 麼, 針 對 重 點 回 答 是 很 重 要 的 ; 不 確 定 的 請 一 定 要 當 場 提 出 來, 不 要 白 花 力 氣 在 誤 會

More information

Microsoft PowerPoint - 13_ClassAndObj.ppt

Microsoft PowerPoint - 13_ClassAndObj.ppt Visual Basic 2005 (VB.net 2.0) 程式設計 講師 : 戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所 第十三章 物件與類別 物件與類別 物件導向程式設計 物件與類別的建立 物件與類別 物件 (object) Ex. 人 屬性 (property) 身高 體重 血型 方法 (method) 走路 跑步 訊息 (message) 交談 事件

More information

软件工程文档编制

软件工程文档编制 实训抽象类 一 实训目标 掌握抽象类的定义 使用 掌握运行时多态 二 知识点 抽象类的语法格式如下 : public abstract class ClassName abstract void 方法名称 ( 参数 ); // 非抽象方法的实现代码 在使用抽象类时需要注意如下几点 : 1 抽象类不能被实例化, 实例化的工作应该交由它的子类来完成 2 抽象方法必须由子类来进行重写 3 只要包含一个抽象方法的抽象类,

More information

ebook55-13

ebook55-13 1 3 C + + C C + + 13.1 X 256 C + + p r i v a t e p u b l i c p e r m u t e () X X Y 13.2 Y Y X 13 257 Y X Y X X m a i n () s i z e o f ( Y s i z e o f ( X ) p u b l i c p r i v a t e p u b l i c p r i

More information

Microsoft PowerPoint - plan08.ppt

Microsoft PowerPoint - plan08.ppt 程 序 设 计 语 言 原 理 Principle of Programming Languages 裘 宗 燕 北 京 大 学 数 学 学 院 2012.2~2012.6 8. 面 向 对 象 为 什 么 需 要 面 向 对 象? OO 语 言 的 发 展 面 向 对 象 的 基 本 概 念 封 装 和 继 承 初 始 化 和 终 结 处 理 动 态 方 法 约 束 多 重 继 承 总 结 2012

More information

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

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入 100 年 特 種 考 試 地 方 政 府 公 務 人 員 考 試 試 題 等 別 : 三 等 考 試 類 科 : 資 訊 處 理 科 目 : 系 統 分 析 與 設 計 一 請 參 考 下 列 旅 館 管 理 系 統 的 使 用 案 例 圖 (Use Case Diagram) 撰 寫 預 約 房 間 的 使 用 案 例 規 格 書 (Use Case Specification), 繪 出 入

More information

投影片 1

投影片 1 資料庫管理程式 ( 補充教材 -Part2) 使用 ADO.NET 連結資料庫 ( 自行撰寫程式碼 以實現新增 刪除 修改等功能 ) Private Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click ' 宣告相關的 Connection

More information

ebook39-6

ebook39-6 6 first-in-first-out, FIFO L i n e a r L i s t 3-1 C h a i n 3-8 5. 5. 3 F I F O L I F O 5. 5. 6 5. 5. 6.1 [ ] q u e n e ( r e a r ) ( f r o n t 6-1a A 6-1b 6-1b D C D 6-1c a) b) c) 6-1 F I F O L I F ADT

More information

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

相 应 功 能 (5) 再 将 Boy 类 作 为 Girl 类 的 友 元 类, 在 Boy 类 的 某 成 员 函 数 VisitGirl(Girl & ) 中 访 问 Girl 类 的 私 有 成 员, 观 察 编 译 器 给 出 的 信 息 ( 6 ) 删 除 两 个 类 中 的 函 数 V 面 向 对 象 程 序 设 计 及 C++ 课 程 实 验 教 学 大 纲 课 程 编 号 : B030001S 课 程 名 称 : 面 向 对 象 程 序 设 计 及 C++ 课 内 总 学 时 : 3 上 机 实 验 学 时 : 8 一 实 验 课 程 的 性 质 目 的 和 任 务 性 质 : 本 实 验 课 程 是 本 科 理 工 科 各 专 业 学 生 的 通 识 基 础 课, 该 实 验

More information

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

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款 JAVA 程 序 设 计 ( 肆 ) 徐 东 / 数 学 系 使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款 使 用 Java class 代 表 保 险 箱 public class SaveBox 类 名 类 类 体 实 现 封 装 性 使 用 class SaveBox 代 表 保

More information

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

KillTest 质量更高 服务更好 学习资料   半年免费更新服务 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

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

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc 2 5 8 11 0 1. 13 2. 15 3. 18 1 1. 22 2. 25 3. 27 2 1. 35 2. 38 3. 41 4. 43 5. 48 6. 50 3 1. 56 2. 59 3. 63 4. 65 5. 69 13 22 35 56 6. 74 7. 82 8. 84 9. 87 10. 97 11. 102 12. 107 13. 111 4 114 1. 114 2.

More information

Microsoft Word - HKU Talk doc

Microsoft Word - HKU Talk doc In the figure, E is a diameter and E is a straight line. Find x. 圖中, E 是一直徑, E為一直線 求 x. 54. 70. 74. 9 E. 94 In the figure, O is the center of the circle. EO and E are straight lines. Find x. 圖中, O 為圓心,

More information

1.5招募说明书(草案)

1.5招募说明书(草案) PUBLIC 2014 1 PUBLIC - 1 2014 1 PUBLIC - 2 2014 1 PUBLIC - 3 2014 1 PUBLIC - 4 2014 1 PUBLIC - 5 2014 1 PUBLIC - 6 2014 1 PUBLIC - 7 2014 1 PUBLIC - 8 2014 1 PUBLIC - 9 2014 1 PUBLIC - 10 2014 1 PUBLIC

More information

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

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 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 information

ebook39-5

ebook39-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 information

踏出C++的第一步

踏出C++的第一步 踏出 C++ 的第一步 講師 : 洪安 1 已經學會的 C 語言基本概念 基本資料型態 變數 基本輸入輸出 控制敘述 選擇控制 迴圈 陣列 函式 指標 字元與字串 結構 檔案處理 2 C v.s. C++ C 函數 程序式語言 Procedural language 結構化程式設計 Structured programming 演算法 Top-down C++ 類別 物件導向程式設計 Object-Oriented

More information

Microsoft PowerPoint - ch_12 [唯讀]

Microsoft PowerPoint - ch_12 [唯讀] 深 度 學 習 C ++ Chapter 12 類 別 間 的 關 係 ( 一 ) 嵌 入 與 繼 承 1 類 別 間 的 關 係 ( 一 ) : 嵌 入 與 繼 承 類 別 間 的 關 係 ( 一 ): 使 用 使 用 (use): 某 甲 類 別 使 用 某 乙 類 別 物 件 來 完 成 某 項 事 情 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人

More information

點 線 圓 本節性質與公式摘要 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

點 線 圓 本節性質與公式摘要 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 24 2-1 點 線 圓 本節性質與公式摘要 1 圓的切線 : 1 2 2 兩圓位置關係與公切線數量 : 1 r 1 2 r 2 r 1 r 2 1 2 r 1 r 2 1 2 r 1 r 2 2 2 1 2 r 1 r 2 2 1 r 1 r 2 1 2 r 1 r 2 2 0 1 2 r 1 r 2 1 0 0 1 2 r 1 r 2 0 0 3 圓外切四邊形 : 例 4 弦心距 : 例 M MMM

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

PowerPoint Presentation

PowerPoint Presentation 第六章簡介運算子超載 (Operator Overloading) 6-1 運算子超載的基礎 6-2 超載二元運算子 6-3 超載邏輯與關係運算子 6-4 超載一元運算子 6-5 使用夥伴函數 6-6 細部檢視指定運算子 6-7 超載註標運算子 6-1 運算子超載的基礎 甚麼是運算子超載? 讓運算子 ( 符號 ) 有不同的意義 EX: 運算子的預設意義 ( 以 + 與 = 為例 ) class frac

More information

(procedure-oriented)?? 2

(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 information

CC213

CC213 : (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 information

投影片 1

投影片 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

CC213

CC213 : (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 information

mvc

mvc Build an application Tutor : Michael Pan Application Source codes - - Frameworks Xib files - - Resources - ( ) info.plist - UIKit Framework UIApplication Event status bar, icon... delegation [UIApplication

More information

Microsoft PowerPoint - 10 模板 Template.pptx

Microsoft 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 information

Microsoft PowerPoint - chap08.ppt

Microsoft PowerPoint - chap08.ppt 第八章繼承 資訊科技系 林偉川 類別繼承使用 extends 關鍵字 修飾字 class 子類別 extends 父類別 1 { // 區塊內的程式敘述 類別以 final 宣告時, 不能做為父類別 extends 關鍵字之後只能接一個父類別 2 1 例子 : class A{ class B extends A { class C extends A { class D extends B { final

More information

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

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class

More information

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

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 Linux C July 27, 2016 Contents 1 Linux IDE 1 2 GCC 3 2.1 hello.c hello.exe........................... 5 2.2............................... 9 2.2.1 -Wall................................ 9 2.2.2 -E..................................

More information

Chapter 8 - Operator Overloading

Chapter 8 - Operator Overloading 1 第八章 - 運算子多載 Operator Overloading 8.1 簡介 8.2 運算子多載基本原理 8.3 運算子多載的限制 8.4 運算子函式 - 成員函式與非成員函式 8.5 多載 > 運算子 8.6 多載單運算元運算子 (Unary Operators) 8.7 多載雙運算元運算子 (Binary Operators) 8.8 案例研讀 : Array 類別 8.9 物件型別轉換

More information

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

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d =

More information

Microsoft Word - chap12.doc

Microsoft Word - chap12.doc 94 12. Inheritance in Java Java 的類別同樣有繼承的功能, 衍生類別同樣會繼承基本類別所有的資料成員與函式成員, 當繼承而來的函式成員不適於衍生類別時, 同樣可以在衍生類別之定義內將其重新定義 (override), 並可進一步加入新的成員 ( 資料或函式 ), 以符合衍生類別應有的屬性與行為 一基本類別 ( 父類別 ) 可以有需多繼承它的衍生類別 ( 子類別 ), 與

More information

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

北 京 师 范 大 学 庆 祝 抗 日 战 争 胜 利 70 周 年 暨 纪 念 12.9 运 动 80 周 年 歌 咏 比 赛 掠 影 党 委 书 记 刘 川 生 指 挥 唱 校 歌 党 委 书 记 刘 川 生 分 别 为 获 得 一 等 奖 和 获 得 精 神 风 貌 奖 单 位 颁 奖 党 委 弘 扬 师 德 积 极 投 身 教 师 志 愿 服 务 北 京 师 范 大 学 庆 祝 抗 日 战 争 胜 利 70 周 年 暨 纪 念 12.9 运 动 80 周 年 歌 咏 比 赛 成 功 举 办 烛 光 行 动 教 师 志 愿 服 务 签 约 仪 式 在 北 京 师 范 大 学 举 行 北 京 师 范 大 学 举 办 第 二 届 青 年 管 理 干 部 岗 位 技 能 竞 赛 北 京 师 范

More information

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

主程式 : 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

Template

Template 第十一章 ( 上篇 ) 函數樣板 (Function Template) 與 類別樣板 (Class Template) 建立通用函數 (Generic Functions) & 通用類別 (Generic Classes) - Code Reuse 的另一種發揮 - 煩人的事經歷一次就夠了 1 為何需要通用函數? int abs(int x) { return (x>0)?x:-x; int abs(int

More information

第1章

第1章 第 15 章 標準類別 1 本章提要 15.1 前言 15.2 基本資料類別介紹 15.3 Integer 類別 15.4 Double 類別 15.5 Float 類別 Long 類別 Short 類別 15.6 數學相關類別 Math 15.7 後記 2 15.1 前言 不同基本資料型別可以互相轉換, 但也只予許由小轉大的情況, 例如 1. byte 轉為 short int long float

More information

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

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 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

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

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 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 information

Microsoft PowerPoint - 14Threads.ppt

Microsoft PowerPoint - 14Threads.ppt Threads Outline Introduction to Threads How to create Thread extend Thread implement Runnable interface Synchronization What is thread? 定義 : 程式的執行軌跡 Single Thread Multi-Thread 依序執行 int x, y; int z; x =

More information

文档 3

文档 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

bingdian001.com

bingdian001.com TSM12M TSM12 STM8L152C6, STM8L152R8 MSP430F5325 whym1987@126.com! /******************************************************************************* * : TSM12.c * : * : 2013/10/21 * : TSM12, STM8L f(sysclk)

More information

untitled

untitled 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 information

用户大会 论文集2.2.doc

用户大会 论文集2.2.doc MagGis MapGis GIS MagGis API DLL MapGis VC++ VB BC++ Delphi., Windows API MapGis VC++V Delphi Delphi Delphi MapGis Delphi Delphi Windows Delphi Delphi MapGis MapGis DLL API MapGis function _InitWorkArea(HINST:Integer):Integer;

More information

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

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票 复习 创建对象 构造函数 函数重载 : 函数 = 函数名 + 参数列表 public class MyType { int i; double d; char c; void set(double x)

More information