问答题
请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motocar和bicycle作为基类,再派生出motocycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“//**********found**********”。#include<iostream.h>classvehicle{private:intMaxSpeed;intWeight;public://**********found**********vehicle(intmaxspeed,intweight):________________~vehicle(){};intgetMaxSpeed(){returnMaxSpeed;}intgetWeight(){returnWeight;}};//**********found**********classbicycle:________publicvehicle{private:intHeight;public:bicycle(intmaxspeedlintweight,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,intweight,intheight):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}};voidmain(){motorcyclea(8.0,150,100);cout<<a.getMaxSpeed()<<end1;cout<<a.getWeight()<<end1;cout<<a.getHeight()<<end1;cout<<a.getSeatNum()<<end1;}
【正确答案】正确答案:(1)MaxSpeed(maxspeed),Weight(weight){} (2)virtual (3)virtual (4)publicbicycle,publicmotorcar
【答案解析】解析:(1)主要考查考生对构造函数的掌握情况,vehicle类在构造函数的成员初始化列表中,完成对数据成员的初始化操作。 (2)主要考查考生对虚继承的掌握,在继承虚基类时,派生列表中应该包含virtual关键字。 (3)主要考查考生对虚继承的掌握,在继承虚基类时,派生列表中应该包含virtual关键字。 (4)主要考查考生对多继承的掌握,在多继承的派生列表中,派生类为每个基类指定访问级别。