图文例解C++类的多重继承与虚拟继承

  在过去的学习中,我们始终接触的单个类的继承,但是在现实生活中,一些新事物往往会拥有两个或者两个以上事物的属性,为了解决这个问题,C++引入了多重继承的概念,C++允许为一个派生类指定多个基类,这样的继承结构被称做多重继承

举个例子,交通工具类可以派生出汽车和船连个子类,但拥有汽车和船共同特性水陆两用汽车就必须继承来自汽车类与船类的共同属性。

  由此我们不难想出如下的图例与代码:

  //站点:www.cndev-lab.com   
//所有稿件均有版权,如要转载,请务必著名出处和作者   
 
#include <iostream> 
using namespace std; 
 
class Vehicle 

    public: 

        Vehicle(int weight = 0) 
        { 
            Vehicle::weight = weight; 
        } 
        void SetWeight(int weight) 

        { 
            cout<<"重新设置重量"<<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//水陆两用汽车,多重继承的体现 


    public: 
        AmphibianCar(int weight,int aird,float tonnage) 

        :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage) 

        //多重继承要注意调用基类构造函数 
        { 

         
        } 
        void ShowMe() 

        { 
            cout<<"我是水陆两用汽车!"<<endl; 
        } 
}; 

int main() 


    AmphibianCar a(4,200,1.35f);//错误 

    a.SetWeight(3);//错误 
    system("pause");  

}

  上面的代码从表面看,看不出有明显的语发错误,但是它是不能够通过编译的。这有是为什么呢?
  这是由于多重继承带来的继承的模糊性带来的问题。

  先看如下的图示:

  在图中深红色标记出来的地方正是主要问题所在,水陆两用汽车类继承了来自Car类与Boat类的属性与方法,Car类与Boat类同为AmphibianCar类的基类,在内存分配上AmphibianCar获得了来自两个类的SetWeight()成员函数,当我们调用a.SetWeight(3)的时候计算机不知道如何选择分别属于两个基类的被重复拥有了的类成员函数SetWeight()。

  由于这种模糊问题的存在同样也导致了AmphibianCar
a(4,200,1.35f);执行失败,系统会产生Vehicle”不是基或成员的错误。

  以上面的代码为例,我们要想让AmphibianCar类既获得一个Vehicle的拷贝,而且又同时共享用Car类与Boat类的数据成员与成员函数就必须通过C++所提供的虚拟继承技术来实现。

  我们在Car类和Boat类继承Vehicle类出,在前面加上virtual关键字就可以实现虚拟继承,使用虚拟继承后,//站点:www.cndev-lab.com   
//所有稿件均有版权,如要转载,请务必著名出处和作者   
 
#include <iostream> 
using namespace std; 
 
class Vehicle 

    public: 

        Vehicle(int weight = 0) 
        { 
            Vehicle::weight = weight; 
            cout<<"载入Vehicle类构造函数"<<endl; 
        } 

        void SetWeight(int weight) 
        { 
            cout<<"重新设置重量"<<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类构造函数"<<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类构造函数"<<endl; 
        } 

        void ShowMe() 
        { 

            cout<<"我是船!"<<endl; 

        } 
    protected: 
        float tonnage; 
}; 
 
class AmphibianCar:public Car,public Boat//水陆两用汽车,多重继承的体现 


    public: 
        AmphibianCar(int weight,int aird,float tonnage) 

        :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage) 

        //多重继承要注意调用基类构造函数 
        { 

            cout<<"载入AmphibianCar类构造函数"<<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");  
}

  注意观察类构造函数的构造顺序。

  虽然说虚拟继承与虚函数有一定相似的地方,但读者务必要记住,他们之间是绝对没有任何联系的!

 
 

最新文章

  1. SQL Server中行列转换 Pivot UnPivot
  2. svn post-commit 同步
  3. python---PrettyTable
  4. ubuntu14升级到15后遇到的问题
  5. LinQ To Object 基本用法
  6. (网络层)IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)
  7. memcachedb-持久化存储的缓存系统
  8. jquerymobile知识点:button与a
  9. [Javascript] Object.assign()
  10. 很强的PHP图片处理类
  11. (转)深度学习word2vec笔记之基础篇
  12. Python的命令模式和交互模式
  13. bugfree3.0.1-修改“优先级”为中文引起的PHP Error
  14. 逻辑回归(logic regression)的分类梯度下降
  15. Python入门之面向对象之类继承与派生
  16. ReentrantLock 和 Condition的使用
  17. 学习一份百度的JavaScript编码规范
  18. 【Go】语法基础之结构体
  19. piranha配置
  20. vue webpack多页面构建

热门文章

  1. django-nginx与uwsgi项目部署
  2. 【洛谷 P4302】 [SCOI2003]字符串折叠(DP)
  3. UML系列——OO Unit4分析和学期总结
  4. Java NIO和IO的区别
  5. 【转载】 C#中List集合使用First方法查找符合条件的第一个元素
  6. mysql死锁检查
  7. 将集群WEB节点静态数据迁移到共享存储器(LNMP环境)
  8. Linux环境变量$PATH
  9. 空指针异常:解决 RequestContextHolder.getRequestAttributes()为空的问题
  10. 学习python的日常6