
- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期和時間
- C++ 控制語句
- C++ 決策
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
C++ 多型
多型這個詞的意思是具有多種形式。通常,當存在類層次結構並且它們透過繼承相關時,就會發生多型。
C++ 多型意味著對成員函式的呼叫將導致執行不同的函式,具體取決於呼叫該函式的物件的型別。
考慮以下示例,其中基類已被其他兩個類派生:
#include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a = 0, int b = 0){ width = a; height = b; } int area() { cout << "Parent class area :" << width * height << endl; return width * height; } }; class Rectangle: public Shape { public: Rectangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Rectangle class area :" << width * height << endl; return (width * height); } }; class Triangle: public Shape { public: Triangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Triangle class area :" << (width * height)/2 << endl; return (width * height / 2); } }; // Main function for the program int main() { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); // store the address of Rectangle shape = &rec; // call rectangle area. shape->area(); // store the address of Triangle shape = &tri; // call triangle area. shape->area(); return 0; }
當編譯並執行上述程式碼時,會產生以下結果:
Parent class area :70 Parent class area :50
輸出錯誤的原因是 area() 函式的呼叫在編譯時被設定為基類中定義的版本。這稱為函式呼叫的靜態解析或靜態連結 - 函式呼叫在程式執行之前就已經確定。這有時也稱為早期繫結,因為 area() 函式是在程式編譯期間設定的。
但是現在,讓我們對程式稍作修改,並在 Shape 類中 area() 的宣告前加上關鍵字virtual,使其如下所示:
#include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a = 0, int b = 0){ width = a; height = b; } virtual int area() { cout << "Parent class area :" << width * height << endl; return width * height; } }; class Rectangle: public Shape { public: Rectangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Rectangle class area :" << width * height << endl; return (width * height); } }; class Triangle: public Shape { public: Triangle( int a = 0, int b = 0):Shape(a, b) { } int area () { cout << "Triangle class area :" << (width * height)/2 << endl; return (width * height / 2); } }; // Main function for the program int main() { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); // store the address of Rectangle shape = &rec; // call rectangle area. shape->area(); // store the address of Triangle shape = &tri; // call triangle area. shape->area(); return 0; }
經過此微小的修改後,當編譯並執行之前的示例程式碼時,會產生以下結果:
Rectangle class area :70 Triangle class area :25
這次,編譯器檢視指標的內容而不是它的型別。因此,由於 *shape 中儲存了 tri 和 rec 類物件的地址,因此會呼叫相應的 area() 函式。
如您所見,每個子類都有 area() 函式的單獨實現。這就是多型通常的使用方式。您有不同的類具有相同名稱的函式,甚至相同引數,但實現不同。
虛擬函式
虛擬函式是基類中使用關鍵字virtual宣告的函式。在基類中定義一個虛擬函式,並在派生類中定義另一個版本,這向編譯器發出訊號,表明我們不希望對該函式進行靜態連結。
我們想要的是,在程式的任何給定點上呼叫的函式的選擇基於為其呼叫該函式的物件的型別。這種操作稱為動態連結或後期繫結。
純虛擬函式
您可能希望在基類中包含一個虛擬函式,以便可以在派生類中重新定義它以適應該類的物件,但是您無法在基類中為該函式提供有意義的定義。
我們可以將基類中的虛擬函式 area() 更改為:
class Shape { protected: int width, height; public: Shape(int a = 0, int b = 0) { width = a; height = b; } // pure virtual function virtual int area() = 0; };
= 0 告訴編譯器該函式沒有主體,並且上面的虛擬函式將被稱為純虛擬函式。
廣告