虚拟继承

虚拟继承是在多重继承下引入的一个解决由于多重继承中继承相同成员函数时带来编译器编译时不知道具体调用哪个函数的问题。

 

例如下面的例子:

 

#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);//错误,不知道调用哪个基类的SetWeight 
    system("pause");  
}

 

采用虚拟继承时,当系统碰到多重继承的时候就会自动先加入一个Vehicle的拷贝,当再次请求一个Vehicle的拷贝的时候就会被忽略,保证继承类成员函数的唯一性。如下所示:

#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");  
}

 

代码资料来自:www.cndev-lab.com,作者: 管宁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值