• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            攀升·Uranus


            Something Different,Something New
            數(shù)據(jù)加載中……

            (轉(zhuǎn)載)圖文例解C++類的多重繼承與虛擬繼承

            在過去的學(xué)習(xí)中,我們始終接觸的單個類的繼承,但是在現(xiàn)實生活中,一些新事物往往會擁有兩個或者兩個以上事物的屬性,為了解決這個問題,C++引入了多重繼承的概念,C++允許為一個派生類指定多個基類,這樣的繼承結(jié)構(gòu)被稱做多重繼承

              舉個例子,交通工具類可以派生出汽車和船兩個子類,但擁有汽車和船共同特性水陸兩用汽車就必須繼承來自汽車類與船類的共同屬性。

              由此我們不難想出如下的圖例與代碼:

             當(dāng)一個派生類要使用多重繼承的時候,必須在派生類名和冒號之后列出所有基類的類名,并用逗好分隔。

               
             
            #include <iostream
            using namespace std; 
             
            class Vehicle 

                public
                    Vehicle(int weight = 0) 
                    { 
                        Vehicle::weight = weight; 
                    } 
                    void SetWeight(int weight) 
                    { 
                        cout<<"重新設(shè)置重量"<<endl; 
                        Vehicle::weight = weight; 
                    } 
                    virtual void ShowMe() = 0; 
                protected
                    int weight; 
            }; 
            class Car:public Vehicle//汽車 

                public
                    Car(int weight=0,int aird=0):Vehicle(weight) 
                    { 
                        Car::aird = aird; 
                    } 
                    void ShowMe() 
                    { 
                        cout<<"我是汽車!"<<endl; 
                    } 
                protected
                    int aird; 
            }; 
             
            class Boat:public Vehicle//船 

                public
                    Boat(int weight=0,float tonnage=0):Vehicle(weight) 
                    { 
                        Boat::tonnage = tonnage; 
                    } 
                    void ShowMe() 
                    { 
                        cout<<"我是船!"<<endl; 
                    } 
                protected
                    float tonnage; 
            }; 
             
            class AmphibianCar:public Car,public Boat//水陸兩用汽車,多重繼承的體現(xiàn) 

                public
                    AmphibianCar(int weight,int aird,float tonnage) 
                    :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage) 
                    //多重繼承要注意調(diào)用基類構(gòu)造函數(shù) 
                    { 
                     
                    } 
                    void ShowMe() 
                    { 
                        cout<<"我是水陸兩用汽車!"<<endl; 
                    } 
            }; 
            int main() 

                AmphibianCar a(4,200,1.35f);//錯誤 
                a.SetWeight(3);//錯誤 
                system("pause");  
            }

              上面的代碼從表面看,看不出有明顯的語發(fā)錯誤,但是它是不能夠通過編譯的。這有是為什么呢?
              這是由于多重繼承帶來的繼承的模糊性帶來的問題。

              先看如下的圖示:

             

              在圖中深紅色標(biāo)記出來的地方正是主要問題所在,水陸兩用汽車類繼承了來自Car類與Boat類的屬性與方法,Car類與Boat類同為AmphibianCar類的基類,在內(nèi)存分配上AmphibianCar獲得了來自兩個類的SetWeight()成員函數(shù),當(dāng)我們調(diào)用a.SetWeight(3)的時候計算機(jī)不知道如何選擇分別屬于兩個基類的被重復(fù)擁有了的類成員函數(shù)SetWeight()。

              由于這種模糊問題的存在同樣也導(dǎo)致了AmphibianCar a(4,200,1.35f);執(zhí)行失敗,系統(tǒng)會產(chǎn)生Vehicle”不是基或成員的錯誤。

              以上面的代碼為例,我們要想讓AmphibianCar類既獲得一個Vehicle的拷貝,而且又同時共享用Car類與Boat類的數(shù)據(jù)成員與成員函數(shù)就必須通過C++所提供的虛擬繼承技術(shù)來實現(xiàn)。

              我們在Car類和Boat類繼承Vehicle類出,在前面加上virtual關(guān)鍵字就可以實現(xiàn)虛擬繼承,使用虛擬繼承后,當(dāng)系統(tǒng)碰到多重繼承的時候就會自動先加入一個Vehicle的拷貝,當(dāng)再次請求一個Vehicle的拷貝的時候就會被忽略,保證繼承類成員函數(shù)的唯一性

              修改后的代碼如下,注意觀察變化:


            #include <iostream
            using namespace std; 
             
            class Vehicle 

                public
                    Vehicle(int weight = 0) 
                    { 
                        Vehicle::weight = weight; 
                        cout<<"載入Vehicle類構(gòu)造函數(shù)"<<endl; 
                    } 
                    void SetWeight(int weight) 
                    { 
                        cout<<"重新設(shè)置重量"<<endl; 
                        Vehicle::weight = weight; 
                    } 
                    virtual void ShowMe() = 0; 
                protected
                    int weight; 
            }; 
            class Car:virtual public Vehicle//汽車,這里是虛擬繼承 

                public
                    Car(int weight=0,int aird=0):Vehicle(weight) 
                    { 
                        Car::aird = aird; 
                        cout<<"載入Car類構(gòu)造函數(shù)"<<endl; 
                    } 
                    void ShowMe() 
                    { 
                        cout<<"我是汽車!"<<endl; 
                    } 
                protected
                    int aird; 
            }; 
             
            class Boat:virtual public Vehicle//船,這里是虛擬繼承 

                public
                    Boat(int weight=0,float tonnage=0):Vehicle(weight) 
                    { 
                        Boat::tonnage = tonnage; 
                        cout<<"載入Boat類構(gòu)造函數(shù)"<<endl; 
                    } 
                    void ShowMe() 
                    { 
                        cout<<"我是船!"<<endl; 
                    } 
                protected
                    float tonnage; 
            }; 
             
            class AmphibianCar:public Car,public Boat//水陸兩用汽車,多重繼承的體現(xiàn) 

                public
                    AmphibianCar(int weight,int aird,float tonnage) 
                    :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage) 
                    //多重繼承要注意調(diào)用基類構(gòu)造函數(shù) 
                    { 
                        cout<<"載入AmphibianCar類構(gòu)造函數(shù)"<<endl; 
                    } 
                    void ShowMe() 
                    { 
                        cout<<"我是水陸兩用汽車!"<<endl; 
                    } 
                    void ShowMembers() 
                    { 
                        cout<<"重量:"<<weight<<"頓,"<<"空氣排量:"<<aird<<"CC,"<<"排水量:"<<tonnage<<"頓"<<endl; 
                    } 
            }; 
            int main() 

                AmphibianCar a(4,200,1.35f); 
                a.ShowMe(); 
                a.ShowMembers(); 
                a.SetWeight(3); 
                a.ShowMembers(); 
                system("pause");  
            }

              注意觀察類構(gòu)造函數(shù)的構(gòu)造順序。

              雖然說虛擬繼承與虛函數(shù)有一定相似的地方,但讀者務(wù)必要記住,他們之間是絕對沒有任何聯(lián)系的!

            posted on 2008-12-18 11:15 攀升 閱讀(691) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            国产成人AV综合久久| 久久人做人爽一区二区三区 | 91精品国产高清久久久久久io | 99久久精品毛片免费播放| 日本福利片国产午夜久久| 一级做a爰片久久毛片毛片| 国产亚洲精品自在久久| 欧洲国产伦久久久久久久| 国产精品免费福利久久| 久久婷婷午色综合夜啪| 亚洲天堂久久精品| 精品国产一区二区三区久久久狼| 久久一区二区三区免费| 国产欧美一区二区久久| 无码日韩人妻精品久久蜜桃 | 久久这里只有精品首页| 99久久精品免费观看国产| 久久精品国产99久久久| 成人久久免费网站| 欧美成a人片免费看久久| 久久青青草原综合伊人| 久久w5ww成w人免费| 久久久久久久97| 久久亚洲国产精品成人AV秋霞| 久久久久18| 久久国产午夜精品一区二区三区| 久久婷婷久久一区二区三区| 日韩人妻无码精品久久久不卡| 久久精品国产99国产精品导航| 性高湖久久久久久久久AAAAA| 久久久久久国产精品无码下载| 嫩草影院久久国产精品| 久久精品国产亚洲AV无码偷窥| 色欲久久久天天天综合网| 久久99久久99精品免视看动漫| 久久人妻无码中文字幕| 久久无码高潮喷水| 无码久久精品国产亚洲Av影片| 色妞色综合久久夜夜| 久久天天躁狠狠躁夜夜96流白浆| 久久人爽人人爽人人片AV|