请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基

admin2021-02-22  18

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。
请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
clasSvehicle
{
private:
intMaxSpeed;
intWeight;
public:
//**********found**********
vehicle(intmaxspeed,int
weight):________
一vehicle(){);
intgetMaxSpeed(){returnMaxSpeed;}
intgetWeight(){returnWeight;}
},
//**********found**********
classbicycle:________publicvehicle
{
private:
intHeight;
public:
bicycle(intmaxspeed,intweight,
intheight):vehicle(maxspeed,
weight),Height(height){}
intgetHeight(){returnHeight;};
},
//**********found**********
classmotorcar:________publicvehicle
{
private:
intSeatNum;
public:
motorcar(intmaxspeed,intweight,
intseatnum):vehicle(maxspeed,
weight),SeatNum(seatnum){}
intgetSeatNum(){returnSeatNum;};
};
//**********found**********
classmotorcycle:________
{
public:
motorcycle(intmaxspeed,int
weight,intheight):vehicle(maxspeed,weight),bicycle(maxspeed,
weight,height),motorcar(maxspeed,
weight,1){}
};
voidmain()
{
motorcyclea(80,150,100);
cout<cout<cout<cout<}

选项

答案(1)MaxSpeed(maxspeed),Weigllt(weigllt){}; (2)virtual (3)virtual (4)publicbicycle,publicmotorcar

解析 (1)主要考查考生对构造函数的掌握,构造函数使用初始化列表来对私有成员MaxSpeed和Weight初始化。
(2)主要考查考生对派生类的掌握,题目要求将vehicle作为虚基类,避免二义性问题。因此在这里添加virtual使vehicle成为虚基类。
(3)主要考查考生对派生类的掌握,题目要求以motorcar和bicycle作为基类,再派生出motorcycle类。在主函数中可以看到motorcycle类的实例a调用getHeight函数和getSeatNum函数,由此可知这两个基类都是公有继承,因此得出语句:publicbicycle,publicmotorcaro。
转载请注明原文地址:https://kaotiyun.com/show/o7yp777K
0

最新回复(0)