問題及代碼:
/*copyright 計算機與控制工程學院
完成日期:2016年5月8日
作者:馬豔豔
檔案名稱:教師兼幹部類
問題描述:(1)根據上面各類間關系的描述,補全下面程式段中空缺的代碼;
(2)實作程式中聲明的成員函數,注意相應操作中的動作發生的條件不能滿足時應給出提示。
(3)運作程式,享受開摩托的過程。(可以下載下傳可執行檔案motorcar.exe,先運作再程式設計。不必申請駕照,這個機車很安全。)
輸入描述:無
輸出描述:成員資訊
*/
#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時,停車
};
//構造函數,初始時,目前速度總為0且處在停車狀态
Vehicle::Vehicle(int maxS, int w):maxSpeed(maxS), currentSpeed(0),weight(w), status(rest) {}
//啟動:由rest狀态到running, 初速為1
void Vehicle::start()
{
if (status==rest)
{
status=running;
currentSpeed=1;
}
else
cout<<"車輛已經行駛!"<<endl;
}
//由running狀态到rest, 目前速度小于5時,才允許停車
void Vehicle::stop()
{
if (status==running)
if(currentSpeed<5)
{
status=rest;
currentSpeed=0;
}
else
cout<<"車速太快!先減速再停車……"<<endl;
else
cout<<"車輛未啟動!"<<endl;
}
//加速,調用1次,速度加1
void Vehicle::speed_up()
{
if (status==running)
if(currentSpeed<maxSpeed)
++currentSpeed;
else
cout<<"請不要超速行駛……"<<endl;
else
cout<<"車輛未啟動!"<<endl;
}
//減速,調用1次,速度減1,速度為0時,停車
void Vehicle::slow_down()
{
if (status==running)
{
if(currentSpeed>0)
--currentSpeed;
}
else
cout<<"車輛未啟動!"<<endl;
if(currentSpeed==0)
status=rest;
}
class Bicycle :virtual public Vehicle //()自行車類
{
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//()機動車類
{
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個(司機)。上下車時要保證安全……
};
//定義構造函數
Motorcar::Motorcar(int maxS, int w, int s, int p): Vehicle(maxS, w),seatNum(s),passengerNum(p) {}
//搭載乘客,超員要拒載,有人下車時,p為負數。當然車上乘客至少有1個(司機)。上下車時要保證安全……
void Motorcar::addPassenger(int p)
{
if (status==running)
{
cout<<"車輛正在行駛,停車後再上下車!"<<endl;
}
else
{
passengerNum+=p;
if(passengerNum>seatNum)
{
passengerNum=seatNum;
cout<<"涉嫌超員,已清理後達到滿員!"<<endl;
}
else if (passengerNum<1)
{
passengerNum=1;
cout<<"請司機不要離開崗位!"<<endl;
}
}
}
class Motorcycle: public Bicycle, public Motorcar //()機車類
{
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),Bicycle(maxS, w, h),Motorcar(maxS, w, s, p) {}
//顯示機車的運作狀态
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()傳回鍵盤上讀取的字元,應包含頭檔案<conio.h>
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.addPassenger();
break;
case '5':
cout<<"選中的操作是5-有人下車\t";
m.addPassenger(-1);
break;
case '6':
cout<<"選中的操作是6-停車\t";
m.stop();
break;
case '0':
end=true;
break;
}
m.show();
cout<<endl;
Sleep(200); //要包含頭檔案<windows.h>
}
return 0;
}
運作結果:

知識點總結:
虛基類主要解決二義性問題。