2-12-2 摩托車繼承自行車和機動車

問題及代碼:

#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;
    weight=w;
    currentSpeed=0;
    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<<"車速大於5,請先減速再停車!"<<endl;
    else
        cout<<"車輛未啓動!"<<endl;
}
void vehicle::speed_up()
{
    if(status==running&¤tSpeed<maxSpeed)
        ++currentSpeed;
    else
    {
        if(status==rest)
            cout<<"您在泊車狀態,請先啓動!"<<endl;
        else
            cout<<"超速了!"<<endl;
    }
}
void vehicle::slow_down()
{
    if(status==running)
        if(currentSpeed==1)
        {
            --currentSpeed;
            status=rest;
        }
        else
            --currentSpeed;

    else
        cout<<"您在泊車狀態,請先啓動!"<<endl;
}
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個(司機)。只有車停穩後才能上下客。
};
motorcar::motorcar(int maxS,int w,int s,int p):vehicle(maxS,w),seatNum(s),passengerNum(p) {}
void motorcar::addPassenger(int p)
{
    if(status==rest)
    {
        if(p+passengerNum<=seatNum&&p+passengerNum>0)
            passengerNum+=p;
        else
        {
            if(p+passengerNum>seatNum)
                cout<<"超載了!"<<endl;
            else
                cout<<"駕駛員不能下車!"<<endl;
        }
    }
    else
        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),bicycle(maxS,w,h),motorcar(maxS,w,s,p) {}
void motorcycle::show()
{
    cout<<(status==running?"狀態:行進;":"狀態:泊車;")<<'\t';
    cout<<"車速:"<<currentSpeed<<'/'<<maxSpeed<<'\t';
    cout<<"當前乘員:"<<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";
            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;
}


運行結果:


學習小結:

經檢驗,可以上路了!

這周項目完成啦!滿意~

這個程序感覺主要時候起的debug比較花時間,因爲在寫的時候想不到一些可能會引起"事故"的情況

加油把

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章