C++ 多態
c++ 多態
多態按字面的意思就是多種形態。當類之間存在層次結構,并且類之間是通過繼承關聯時,就會用到多態。
c++ 多態意味著調用成員函數時,會根據調用函數的對象的類型來執行不同的函數。
下面的實例中,基類 shape 被派生為兩個類,如下所示:
#include 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 :" <