您的位置:首页 > 其它

第10、11周项目-摩托车继承自行车和机动车

2016-05-16 12:36 337 查看
问题描述及代码:

/*
*copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:hellow.cpp
*作者:田甜
*完成日期:2016年5月15日
*版本号:v1.0
*问题描述:
在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承。
(1)根据上面各类间关系的描述,补全下面程序段中空缺的代码;
(2)实现程序中声明的成员函数,注意相应操作中的动作发生的条件不能满足时应给出提示。
(3)运行程序,享受开摩托的过程。
*输入描述:///
*程序输出:///
*/

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
enum vehicleStatus {rest,running};
class Vehicel
{
protected:
int maxSpeed;
int currentSpeed;
int weight;
vehicleStatus status;
public:
Vehicel(int maxS,int w):maxSpeed(maxS),currentSpeed(0),weight(w),status(rest){}
void start();
void stop();
void speed_up();
void slow_down();
};
void Vehicel::start()
{
if(status==rest)
{
status=running;
currentSpeed=1;
}
else
cout<<"车辆已行驶。"<<endl;
}

void Vehicel::stop()
{
if(status==running)
{
status=rest;
currentSpeed=0;
}
else
cout<<"车辆未行驶。"<<endl;
}

void Vehicel::speed_up()
{
if(status==running)
{
if(currentSpeed<maxSpeed)
{
currentSpeed++;
}
else
cout<<"请勿超速行驶。"<<endl;
}
else
cout<<"车辆未行驶。"<<endl;
}

void Vehicel::slow_down()
{
if(status==running)
{
currentSpeed--;
}
else
cout<<"车辆未行驶。"<<endl;
if(currentSpeed==0)//停止是减速的极限因此staus=0在减速函数里面
status=rest;//到这里便是减速循环的终点
}
class Motorcar:virtual public Vehicel
{
protected:
int seatNum;
int passonerNum;
public:
Motorcar(int maxS=150,int w=1500,int seat=5,int pass=1):Vehicel(maxS,w),seatNum(seat),passonerNum(pass){}
void addPassoner(int p=1);
};
void Motorcar::addPassoner(int p)
{
if(status==running)
{
cout<<"车辆正在行驶,请等车辆停稳后下车。"<<endl;
}
else
{
passonerNum+=p;
if(passonerNum>seatNum)
{
passonerNum=seatNum;
cout<<"涉嫌超员,已清除多余乘客。"<<endl;
}
else if(passonerNum<1)
{
passonerNum=1;
cout<<"司机请不要离开岗位。"<<endl;
}
}
}

class Bicycle:virtual public Vehicel
{
protected:
double hight;
public:
Bicycle(int maxS=10,int w=50,double h=0.7):Vehicel(maxS,w),hight(h){}
};

class MotorBicycle:public Bicycle,public Motorcar
{
public://不写类型默认为private型
MotorBicycle(int maxS=90,int w=100,int seat=3,int pass=1,double h=0.7):Vehicel(maxS,w),Bicycle(maxS,w,h),Motorcar(maxS,w,seat,pass){}
void show();
};

void MotorBicycle::show()
{
cout<<"状态:"<<endl;
if(status==rest)
cout<<"泊车;\t";
else
cout<<"行进;\t";
cout<<"车速:"<<currentSpeed<<"/"<<maxSpeed<<"\t当前成员:"<<passonerNum<<"/"<<seatNum<<endl;
}

int main()
{
MotorBicycle m;
bool end=false;
while(!end)
{
cout<<"请操作:1-启动  2-加速  3-减速  4-有人上车  5-有人下车  6-停车 0-结束"<<endl;
char keydown=_getch();
switch(keydown)
{
case '1':
cout<<"选中的操作是1-启动\t";
m.start();
break;
case '2':
cout<<"选中的操作是2-加速\t";
m.speed_up();
break;
case'3':
cout<<"选中的操作是3-减速\t";
m.slow_down();
break;
case'4':
cout<<"选中的操作是4-有人上车\t";
m.addPassoner();
break;
case'5':
cout<<"选中的操作是5-有人下车\t";
m.addPassoner(-1);
break;
case'6':
cout<<"选中的操作是6-停车\t";
m.stop();
break;
case'0':
end=true;
break;
};

m.show();
cout<<endl;
Sleep(200);
}
return 0;
}


运行结果:





使用虚基类可以避免派生类的多个基类所共有的基类里面的数据的重复。

心得体会:

在MotorBicycle的构造函数的形参列表中除了要有Motor和Bicycle的构造函数的全部参数,还要有虚基类Vehicle类的所有形参。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: