第十三周上級任務項目3

來源:互聯網
上載者:User
01./*      02.* 程式的著作權和版本聲明部分      03.* Copyright (c)2013, 煙台大學電腦學院學生      04.* All rightsreserved.      05.* 檔案名稱: vehicle.cpp                                 06.* 作    者:趙冠哲                                  07.* 完成日期:2013年5月24日      08.* 版本號碼: v1.0            09.* 輸入描述:      10.* 問題描述:    11.*/ #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, 初速為1void stop(); //由running狀態到rest, 當前速度小於5時,才允許停車void speed_up();  //加速,調用1次,速度加1void slow_down(); //減速,調用1次,速度減1,速度為0時,停車};vehicle::vehicle( int maxS,int w){    maxSpeed=maxS, weight=w,currentSpeed=0, status=rest;}void vehicle::start(){    if(status==running)    {        cout<<"親,該車已在行駛中!"<<endl;        return;    }    currentSpeed=1,status=running;}void vehicle::stop(){    if(currentSpeed>=5)    {        cout<<"親,您的駕駛速度太快,請減速停車!"<<endl;        return;    }    currentSpeed=0,status=rest;}void vehicle::speed_up(){    if(currentSpeed>=maxSpeed)    {        cout<<"親,請不要超速行駛!"<<endl;        return;    }    if(status==rest)    {        cout<<"親,您的車還沒啟動呢!"<<endl;        return;    }    currentSpeed++;}void vehicle::slow_down(){    if(currentSpeed<=0)    {        cout<<"親,您的車還沒啟動呢!"<<endl;        return;    }    currentSpeed--;    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個(司機)。只有車停穩後才能上下客。};motorcar::motorcar(int maxS, int w, int s, int p):vehicle(maxS,w){    seatNum = s,passengerNum=p;}void motorcar::addPassenger(int p){   if (p>0)    {        if (status == running)        {            cout <<"親,正在駕駛中,停車後再上人!" << endl;        return;        }        if (passengerNum >= seatNum)        {            cout << "超載了!" << endl;        return;    }        passengerNum++;    }    else    {       if (passengerNum<=1)        {            cout<<"這車沒司機啦!"<<endl;            return;        }        passengerNum--;    }}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):bicycle(maxS,w,h), motorcar(maxS,w,s,p),vehicle(maxS, w){}void motorcycle::show(){    cout << "狀態:"<<(status==rest?"泊車":"行進")<<'\t'<< "車速:"<<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<<"操作(啟動)\t"; m.start(); break;case '2':cout<<"操作(加速)\t"; m.speed_up(); break;case '3':cout<<"操作(減速)\t"; m.slow_down(); break;case '4':cout<<"操作(有人上車)\t"; m.addPassenger(); break;case '5':cout<<"操作(有人下車)\t"; m.addPassenger(-1); break;case '6':cout<<"操作(停車)\t"; m.stop(); break;case '0':end=true; break;}m.show();cout<<endl;Sleep(200);  //要包含標頭檔<windows.h>}return 0;}

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.