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

admin2020-06-29  31

问题 请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
    80
    150
    100
    1
    注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
1   #include
2  class vehicle
3   {
4    private:
5   int MaxSpeed;
6    int Weight;
7   public:
8   //**********found**********
9    vehicle  (int  maxspeed,  int weight):_____
10   ~vehicle(){};
11    int getMaxSpeed(){return Max-Speed;}
12    int getWeight(){return Weight;}
13    };
14    //**********found**********
15    class bicycle:______public vehicle
16  {
17   private:
18    int Height;
19    public:
20    bicycle(int maxspeed,int weight,int height):  vehicle  {maxspeed,weight),Height(height){}
21  int getHeight(){return Height;};
22  };
23   //**********found**********
24    class motorcar:_______public ve-hicle
25  {
26  private:
27    int SeatNum;
28    public:
29    motorcar(int maxspeed,int weight,int  seatnum):vehicle  (maxspeed,weight),SeatNum(seatnum){}
30    int getSeatNum(){return SeatNum;};
31  };
32    //**********found**********
33   class motorcycle:_______
34  {
35 public:
36    motorcycle(int  maxspeed,  int weight,int height):vehicle(max speed,weight),bicycle(maxspeed, weight,height),motorcar(maxspeed, weight,1){}
37    };
38  void main()
39  {
40    motorcycle a(8 0,150,100);
41    cout<42    cout<43    cout<44    cout<45  }

选项

答案(1)MaxSpeed(maxspeed),Weight(weight){}; (2)virtual (3)virtual (4)public bicycle,public motorcar

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

最新回复(0)