第七章:圖文例解C++類(lèi)的多重繼承與虛擬繼承

字號(hào):

在過(guò)去的學(xué)習(xí)中,我們始終接觸的單個(gè)類(lèi)的繼承,但是在現(xiàn)實(shí)生活中,一些新事物往往會(huì)擁有兩個(gè)或者兩個(gè)以上事物的屬性,為了解決這個(gè)問(wèn)題,C++引入了多重繼承的概念,C++允許為一個(gè)派生類(lèi)指定多個(gè)基類(lèi),這樣的繼承結(jié)構(gòu)被稱(chēng)做多重繼承。
    舉個(gè)例子,交通工具類(lèi)可以派生出汽車(chē)和船連個(gè)子類(lèi),但擁有汽車(chē)和船共同特性水陸兩用汽車(chē)就必須繼承來(lái)自汽車(chē)類(lèi)與船類(lèi)的共同屬性。
    由此我們不難想出如下的圖例與代碼:
    當(dāng)一個(gè)派生類(lèi)要使用多重繼承的時(shí)候,必須在派生類(lèi)名和冒號(hào)之后列出所有基類(lèi)的類(lèi)名,并用逗好分隔。
    #include
    using namespace std;
    class Vehicle
    {
    public:
    Vehicle(int weight = 0)
    {
    Vehicle::weight = weight;
    }
    void SetWeight(int weight)
    {
    cout<<"重新設(shè)置重量"< Vehicle::weight = weight;
    }
    virtual void ShowMe() = 0;
    protected:
    int weight;
    };
    class Car:public Vehicle//汽車(chē)
    {
    public:
    Car(int weight=0,int aird=0):Vehicle(weight)
    {
    Car::aird = aird;
    }
    void ShowMe()
    {
    cout<<"我是汽車(chē)!"< }
    protected:
    int aird;
    };
    class Boat:public Vehicle//船
    {
    public:
    Boat(int weight=0,float tonnage=0):Vehicle(weight)
    {
    Boat::tonnage = tonnage;
    }
    void ShowMe()
    {
    cout<<"我是船!"< }
    protected:
    float tonnage;
    };
    class AmphibianCar:public Car,public Boat//水陸兩用汽車(chē),多重繼承的體現(xiàn)
    {
    public:
    AmphibianCar(int weight,int aird,float tonnage)
    :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
    //多重繼承要注意調(diào)用基類(lèi)構(gòu)造函數(shù)
    {
    }
    void ShowMe()
    {
    cout<<"我是水陸兩用汽車(chē)!"< }
    };
    int main()
    {
    AmphibianCar a(4,200,1.35f);//錯(cuò)誤
    a.SetWeight(3);//錯(cuò)誤
    system("pause");
    }
    上面的代碼從表面看,看不出有明顯的語(yǔ)發(fā)錯(cuò)誤,但是它是不能夠通過(guò)編譯的。這有是為什么呢?
    這是由于多重繼承帶來(lái)的繼承的模糊性帶來(lái)的問(wèn)題。
    先看如下的圖示:
    在圖中深紅色標(biāo)記出來(lái)的地方正是主要問(wèn)題所在,水陸兩用汽車(chē)類(lèi)繼承了來(lái)自Car類(lèi)與Boat類(lèi)的屬性與方法,Car類(lèi)與Boat類(lèi)同為AmphibianCar類(lèi)的基類(lèi),在內(nèi)存分配上AmphibianCar獲得了來(lái)自?xún)蓚€(gè)類(lèi)的SetWeight()成員函數(shù),當(dāng)我們調(diào)用a.SetWeight(3)的時(shí)候計(jì)算機(jī)不知道如何選擇分別屬于兩個(gè)基類(lèi)的被重復(fù)擁有了的類(lèi)成員函數(shù)SetWeight()。
    由于這種模糊問(wèn)題的存在同樣也導(dǎo)致了AmphibianCar a(4,200,1.35f);執(zhí)行失敗,系統(tǒng)會(huì)產(chǎn)生Vehicle”不是基或成員的錯(cuò)誤。
    以上面的代碼為例,我們要想讓AmphibianCar類(lèi)既獲得一個(gè)Vehicle的拷貝,而且又同時(shí)共享用Car類(lèi)與Boat類(lèi)的數(shù)據(jù)成員與成員函數(shù)就必須通過(guò)C++所提供的虛擬繼承技術(shù)來(lái)實(shí)現(xiàn)。
    我們?cè)贑ar類(lèi)和Boat類(lèi)繼承Vehicle類(lèi)出,在前面加上virtual關(guān)鍵字就可以實(shí)現(xiàn)虛擬繼承,使用虛擬繼承后,當(dāng)系統(tǒng)碰到多重繼承的時(shí)候就會(huì)自動(dòng)先加入一個(gè)Vehicle的拷貝,當(dāng)再次請(qǐng)求一個(gè)Vehicle的拷貝的時(shí)候就會(huì)被忽略,保證繼承類(lèi)成員函數(shù)的性。
    修改后的代碼如下,注意觀(guān)察變化:
    #include
    using namespace std;
    class Vehicle
    {
    public:
    Vehicle(int weight = 0)
    {
    Vehicle::weight = weight;
    cout<<"載入Vehicle類(lèi)構(gòu)造函數(shù)"< }
    void SetWeight(int weight)
    {
    cout<<"重新設(shè)置重量"< Vehicle::weight = weight;
    }
    virtual void ShowMe() = 0;
    protected:
    int weight;
    };
    class Car:virtual public Vehicle//汽車(chē),這里是虛擬繼承
    {
    public:
    Car(int weight=0,int aird=0):Vehicle(weight)
    {
    Car::aird = aird;
    cout<<"載入Car類(lèi)構(gòu)造函數(shù)"< }
    void ShowMe()
    {
    cout<<"我是汽車(chē)!"< }
    protected:
    int aird;
    };
    class Boat:virtual public Vehicle//船,這里是虛擬繼承
    {
    public:
    Boat(int weight=0,float tonnage=0):Vehicle(weight)
    {
    Boat::tonnage = tonnage;
    cout<<"載入Boat類(lèi)構(gòu)造函數(shù)"< }
    void ShowMe()
    {
    cout<<"我是船!"< }
    protected:
    float tonnage;
    };
    class AmphibianCar:public Car,public Boat//水陸兩用汽車(chē),多重繼承的體現(xiàn)
    {
    public:
    AmphibianCar(int weight,int aird,float tonnage)
    :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
    //多重繼承要注意調(diào)用基類(lèi)構(gòu)造函數(shù)
    {
    cout<<"載入AmphibianCar類(lèi)構(gòu)造函數(shù)"< }
    void ShowMe()
    {
    cout<<"我是水陸兩用汽車(chē)!"< }
    void ShowMembers()
    {
    cout<<"重量:"< }
    };
    int main()
    {
    AmphibianCar a(4,200,1.35f);
    a.ShowMe();
    a.ShowMembers();
    a.SetWeight(3);
    a.ShowMembers();
    system("pause");
    }
    注意觀(guān)察類(lèi)構(gòu)造函數(shù)的構(gòu)造順序。
    雖然說(shuō)虛擬繼承與虛函數(shù)有一定相似的地方,但讀者務(wù)必要記住,他們之間是絕對(duì)沒(méi)有任何聯(lián)系的!