開心檔之C++ 多態(tài)
C++ 多態(tài)
多態(tài)按字面的意思就是多種形態(tài)。當(dāng)類之間存在層次結(jié)構(gòu),并且類之間是通過繼承關(guān)聯(lián)時,就會用到多態(tài)。
C++ 多態(tài)意味著調(diào)用成員函數(shù)時,會根據(jù)調(diào)用函數(shù)的對象的類型來執(zhí)行不同的函數(shù)。
下面的實例中,基類 Shape 被派生為兩個類,如下所示:
實例
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 :" <<endl; ? ? ? ? return 0;
? ? ?}
};class Rectangle: public Shape{ ? public: ? ? ?Rectangle( int a=0, int b=0):Shape(a, b) { } ? ? ?int area ()
? ? ?{
? ? ? ? cout << "Rectangle class area :" <<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 :" <<endl; ? ? ? ? return (width * height / 2);
? ? ?}
};// 程序的主函數(shù)int main( ){
? Shape *shape; ? Rectangle rec(10,7); ? Triangle ?tri(10,5);
? // 存儲矩形的地址
? shape = &rec; ? // 調(diào)用矩形的求面積函數(shù) area
? shape->area();
? // 存儲三角形的地址
? shape = &tri; ? // 調(diào)用三角形的求面積函數(shù) area
? shape->area(); ?
? return 0;
}
當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
Parent class area :
Parent class area :
導(dǎo)致錯誤輸出的原因是,調(diào)用函數(shù) area() 被編譯器設(shè)置為基類中的版本,這就是所謂的靜態(tài)多態(tài)?,或靜態(tài)鏈接?- 函數(shù)調(diào)用在程序執(zhí)行前就準(zhǔn)備好了。有時候這也被稱為早綁定,因為 area() 函數(shù)在程序編譯期間就已經(jīng)設(shè)置好了。
但現(xiàn)在,讓我們對程序稍作修改,在 Shape 類中,area() 的聲明前放置關(guān)鍵字?virtual,如下所示:
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 :" <<endl; ? ? ? ? return 0;
? ? ?}
};
修改后,當(dāng)編譯和執(zhí)行前面的實例代碼時,它會產(chǎn)生以下結(jié)果:
Rectangle class area :
Triangle class area :
此時,編譯器看的是指針的內(nèi)容,而不是它的類型。因此,由于 tri 和 rec 類的對象的地址存儲在 *shape 中,所以會調(diào)用各自的 area() 函數(shù)。
正如您所看到的,每個子類都有一個函數(shù) area() 的獨立實現(xiàn)。這就是多態(tài)的一般使用方式。有了多態(tài),您可以有多個不同的類,都帶有同一個名稱但具有不同實現(xiàn)的函數(shù),函數(shù)的參數(shù)甚至可以是相同的。
虛函數(shù)
虛函數(shù)?是在基類中使用關(guān)鍵字?virtual?聲明的函數(shù)。在派生類中重新定義基類中定義的虛函數(shù)時,會告訴編譯器不要靜態(tài)鏈接到該函數(shù)。
我們想要的是在程序中任意點可以根據(jù)所調(diào)用的對象類型來選擇調(diào)用的函數(shù),這種操作被稱為動態(tài)鏈接?,或后期綁定。
純虛函數(shù)
您可能想要在基類中定義虛函數(shù),以便在派生類中重新定義該函數(shù)更好地適用于對象,但是您在基類中又不能對虛函數(shù)給出有意義的實現(xiàn),這個時候就會用到純虛函數(shù)。
我們可以把基類中的虛函數(shù) 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 告訴編譯器,函數(shù)沒有主體,上面的虛函數(shù)是純虛函數(shù)。