基类的两个派生类再派生一个派生类 用virtual避免二义性

class vehicle
{
int MaxSpeed;
int Weight;
public:
vehicle(int maxspeed, int weight) :MaxSpeed(maxspeed), Weight(weight){}
~vehicle(){}
int getMaxspeed()const { return MaxSpeed; }
int getWeight()const { return Weight; }
};
class bicycle: virtual public vehicle
{
int Height;
public:
bicycle(int maxspeed, int weight, int height)
:vehicle(maxspeed, weight), Height(height){}
int getHeight()const { return Height; }
};
class motorcar :virtual public vehicle
{
int SeatNum;
public:
motorcar(int maxspeed, int weight, int seatnum) :vehicle(maxspeed, weight), SeatNum(seatnum){}
int getseatnum()const{ return SeatNum; }
};
class motorcycle : public bicycle,public motorcar
{
public:
motorcycle(int maxspeed, int weight, int height) :
vehicle(maxspeed, weight), bicycle(maxspeed, weight, height),
motorcar(maxspeed, weight, 1){}

};

int _tmain()
{
motorcycle a(80, 150, 100);
cout << a.getMaxspeed() << endl;
cout << a.getWeight() << endl;
cout << a.getHeight() << endl;
cout << a.getseatnum() << endl;

}

原文地址:https://www.cnblogs.com/huninglei/p/5470690.html