在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承,如图所示。
(1)根据上面各类间关系的描述,补全下面程序段中空缺的代码;
(2)实现程序中声明的成员函数,注意相应操作中的动作发生的条件不能满足时应给出提示。
(3)运行程序,享受开摩托的过程。
(4)在报告中,请用自己的话写清楚使用虚基类解决什么问题?
#include <iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
enum vehicleStaus {rest, running}; //车辆状态:泊车、行进
class vehicle //车辆类
{
protected:
int maxSpeed; //最大车速
int currentSpeed; //当前速度
int weight; //车重
vehicleStaus status; //rest-泊车状态;running-行进状态
public:
vehicle(int maxS, int w); //构造函数,初始时,当前速度总为0且处在停车状态
void start(); //由rest状态到running, 初速为1
void stop(); //由running状态到rest, 当前速度小于5时,才允许停车
void speed_up(); //加速,调用1次,速度加1
void slow_down(); //减速,调用1次,速度减1,速度为0时,停车
};
vehicle::vehicle(int maxS,int w):maxSpeed(maxS),currentSpeed(0),weight(w),status(rest){}
void vehicle::start()
{
if(status==rest)
{
status=running;
currentSpeed=1;
}
else
{
cout<<"车辆行驶中!"<<endl;
}
}
void vehicle::stop()
{
if(status==running)
{
if(currentSpeed<5)
{
status=rest;
currentSpeed=0;
}
else
{
cout<<"车辆速度过快,不允许停车!"<<endl;
}
}
else
{
cout<<"车辆停泊中!"<<endl;
}
}
void vehicle::speed_up()
{
if(status==running)
{
if(currentSpeed==maxSpeed)
{
cout<<"已经达到最大速度!"<<endl;
}
else
currentSpeed+=1;
}
else
{
cout<<"机车未启动!"<<endl;
}
}
void vehicle::slow_down()
{
if(status==running)
{
if(currentSpeed>0)
--currentSpeed;
}
else
{
cout<<"车辆未启动!"<<endl;
}
if(currentSpeed==0)
{
status=rest;
}
}
class bicycle :virtual public vehicle//(1)自行车类的虚基类为车辆类
{
protected:
double height; //车高
public:
bicycle(int maxS=10, int w=50, int h=0.7); //定义构造函数
};
bicycle::bicycle(int maxS,int w,int h):vehicle(maxS,w),height(h){}
class motorcar :virtual public vehicle//(2)机动车类的虚基类也为车辆类
{
protected:
int seatNum; //座位数
int passengerNum; //乘客人数
public:
motorcar(int maxS=150, int w=1500, int s=5, int p=1); //定义构造函数
void addPassenger(int p=1); //增加搭载的乘客,超员要拒载,有人下车时,p为负数。当然车上乘客至少有1个(司机)。只有车停稳后才能上下客。
void delPassenger(int p=1);
};
motorcar::motorcar(int maxS, int w, int s, int p)
:vehicle(maxS,w),seatNum(s),passengerNum(p){}
void motorcar::delPassenger(int p)
{
if(status==running)
{
cout<<"车还未停稳!"<<endl;
}
else
{
passengerNum-=1;
if(passengerNum<1)
{
passengerNum=1;
cout<<"司机必须在座位!"<<endl;
}
else
{
cout<<"座位数已不能再容纳更多的人!"<<endl;
}
}
}
void motorcar::addPassenger(int p)
{
if(status==running)
{
cout<<"车还未停稳!"<<endl;
}
else
{
if(passengerNum<seatNum)
{
passengerNum+=p;
}
else if(passengerNum<1)
{
passengerNum=1;
cout<<"司机必须在座位!"<<endl;
}
else
{
passengerNum=seatNum;
cout<<"座位数已不能再容纳更多的人!"<<endl;
}
}
}
class motorcycle: public bicycle,public motorcar //(3)摩托车类的基类为自行车类和机动车类
{
public:
motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);//定义构造函数
void show(); //显示摩托车的运行状态
};
motorcycle::motorcycle(int maxS, int w ,int s, int p, int h)
:vehicle(maxS,w),motorcar(maxS,w,s,p),bicycle(maxS,w,h){}
void motorcycle::show()
{
cout<<"状态:";
if(status==rest)
cout<<"泊车;\t";
else
cout<<"行进;\t";
cout<<"车速:"<<currentSpeed<<" / "<< maxSpeed <<"\t当前乘员:"<<passengerNum<<" / "<< seatNum << endl;
}
int main()
{
motorcycle m;
bool end=false;
while (!end)
{
cout<<"请操作:1-启动 2-加速 3-减速 4-有人上车 5-有人下车 6-停车 0-结束"<<endl;
char keydown= _getch(); //_getch()返回键盘上读取的字符
switch(keydown)
{
case '1':
cout<<"选中的操作是1-启动\t"<<endl;
m.start();
break;
case '2':
cout<<"选中的操作是2-加速\t"<<endl;
m.speed_up();
break;
case '3':
cout<<"选中的操作是3-减速\t"<<endl;
m.slow_down();
break;
case '4':
cout<<"选中的操作是4-有人上车\t"<<endl;
m.addPassenger();
break;
case '5':
cout<<"选中的操作是5-有人下车\t"<<endl;
m.delPassenger(1);
break;
case '6':
cout<<"选中的操作是6-停车\t"<<endl;
m.stop();
break;
case '0':
end=true;
break;
}
getchar();
m.show();
cout<<endl;
Sleep(200); //要包含头文件<windows.h>
}
return 0;
}
运行结果:
虚基类能够很好的解决程序的二义性问题,在这里,motorcar和bicycle类都是继承于vehicle类,而在最后的motorcycle又继承于motorcar和bicycle类,他们又是由同一个基类派生而来,导致最后不知道调用哪一个基类的成员变量和函数,如果在子类的基类前面加上virtual,就能够解决这个问题了。