![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/769eafc51e51a6f73840c9012b276c8d.png#pic_center)
报错:
![为什么啊](https://i-blog.csdnimg.cn/blog_migrate/632dda4d090a33c98f00241aa697ec03.png#pic_center)
解决:使用虚基类
#include <iostream>
using namespace std;
class CAR{
protected:
double speed;
public:
CAR(double sp)
:speed(sp)
{cout<<"创建汽车("<<this->speed<<"公里/小时)"<<endl;}
virtual void Show()const {cout<<"汽车: "<<this->speed<<"公里/小时"<<endl;}
};
class BUS:virtual public CAR{
protected:
int seat;
public:
BUS(double sp,int se)
:CAR(sp),seat(se)
{
cout<<"创建客车("<<this->seat<<"人)"<<endl;
}
virtual void Show()const {cout<<"客车: "<<this->speed<<"公里/小时, "<<this->seat<<"人"<<endl;}
};
class TRUCK:virtual public CAR{
protected:
double load;
public:
TRUCK(double sp,double lo)
:CAR(sp),load(lo)
{
cout<<"创建货车("<<this->load<<"吨)"<<endl;
}
virtual void Show()const {cout<<"货车: "<<this->speed<<"公里/小时, "<<this->load<<"吨"<<endl;}
};
class VAN:public BUS,public TRUCK{
public:
VAN(double sp,int se,double lo)
:CAR(sp),BUS(sp,se),TRUCK(sp,lo)
{
cout<<"创建客货两用车"<<endl;
}
virtual void Show()const {cout<<"客货两用车: "<<this->BUS::speed<<"公里/小时, "<<this->seat<<"人, "<<this->load<<"吨"<<endl;}
};
void Test(const CAR &x)
{
x.Show();
}
int main()
{
VAN a(80.5, 6, 1.2);
Test(a);
return 0;
}