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 告訴編譯器該函式沒有主體,並且上面的虛擬函式將被稱為純虛擬函式

廣告