C++ 实验12 虚基类
class vehicle{//车类,作为基类
protected:
int //最大速度,重量
public:
void Run(){ cout<<"车vehicle类开始运行!"<<endl; }
void Stop(){ cout<<"车vehicle类停止运行!"<<endl; }
};
class bicycle: {//自行车类:公有继承虚基类vehicle
protected:
int //高度
};
class motorcar: {//汽车类:公有继承虚基类vehicle
protected:
int //座位数
};
class motorcycle : {//摩托车类:公有继承自行车类和汽车类
public:
void Setdata(int ms,int wt,int ht,int sn)
{ MaxSpeed=ms; Weight=wt; Height=ht; SeatNum=sn; };
void Show(){
cout<<"最大速度:"<<MaxSpeed<<" 重量:"<<Weight
<<" 高度:"<<Height<<" 座位数:"<<SeatNum<<endl;
}
};
void main(){
cout<<"int类型占用字节数: "<<sizeof(int)<<endl;
cout<<"vehicle占用字节数: "<< <<endl;
cout<<"bicycle占用字节数: "<< <<endl;
cout<<"motorcar占用字节数: "<< <<endl;
cout<<"motorcycle占用字节数: "<< <<endl;
motorcycle mt;
mt.Run();
mt.Setdata(100,200,130,2);
mt.Show();
mt.Stop();
}
l 编译成功后,把vehicl类设置为非虚基类,再编译一次,观察程序运行情况
我的答案:
【任务二】:根据类图,使用虚基类,修改实验11中的任务3。实现以下运行结果。
类图:
运行结果:
我的答案: