问答题 请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motocycle类。要求将vehicle作为虚基类,避免二义性问题。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:Avehicleisrunning!Avehiclehasstopped!Abicycleisrunning!Abicyclehasstopped!Amotorcarisnmning!Amotocycleisrunning!注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream.h>classvehicle{private:intMaxSpeed;intWeight;public:vehicle():MaxSpeed(0),Weight(0){}vehicle(intmax_speed,intweight):MaxSpeed(max_speed),Weight(weight){}//**********found**********________Run(){cout<<"AvehicleiSrunning!"<<end1;}//**********found**********________Stop(){cout<<"Avehiclehasstopped!"<<end1;}};classbicycle:virtualpublicvehicle{private:intHeight;public:bicycle():Height(0){}bicycle(intmax_speed,intweight,intheight):vehicle(max_speed,weight),Height(height){};voidRun(){cout<<"Abicycleisrunning!"<<end1;}voidStop(){cout<<”Abicyclehasstopped!”<<end1;));classmotorcar:virtualpublicvehicle{private:intSeatNum;public:motorcar():SeatNum(0){}motorcar(intmaxspeed,intweight,intseat_num)//**********found**********:________{}voidRun(){cout<<"Amotorcarisrunning!"<<end1;}voidStop(){cout<<"Amotorcarhasstopped!"<<end1;}};//**********found**********classmOtOrcycle:________{public:motorcycle(){}motorcycle(intmax_speed,intweight,intheight,intseet_num):bicycle(max_speed,weight,height),motorcar(max_speed,weight,seet_num){};~motorcycle(){};voidRun(){cout<<"Amotorcycleisrunning!"<<end1;}voidStop(){cout<<"AmotOrcyclehasstopped!"<<end1;}};intmain(){vehicle*ptr;vehiclea;bicycleb;motorcarc;motorcycled;a.Run();a.Stop();b.Run();b.Stop();ptr=&c;ptr->Run();ptr=&d;ptr->Run(),return0;}
【正确答案】正确答案:(1)virtual void (2)virtual void (3)vehicle(max_speed, weight), SeatNum(seat_num) (4)public bicycle, public motorcar
【答案解析】解析:(1)和(2)主要考查考生对虚函数的掌握,虚函数使用virtual定义。 (3)主要考查考生对构造函数的掌握,使用成员列表初始化。 (4)主要考查考生对派生类的掌握,派生类继承基类时要表明继承方式,公有继承为public,多个继承时要使用","隔开。