第十二周任務3


 
#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);//構造函數,初始時,當前速度總為且處在停車狀態
 void start();  //由rest狀態到running, 初速為  
    void stop(); //由running狀態到rest, 當前速度小於時,才允許停車  
    void speed_up();  //加速,調用次,速度加  
    void slow_down(); //減速,調用次,速度減,速度為時,停車  
 
class bicycle :virtual public vehicle//()自行車類的虛基類為車輛類
{ 
protected:
 double height; //車高
public:
 bicycle(int maxS=10, int w=50, double h=0.7);
 
 //定義構造函數
 
};
 
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);   };
 
class motorcycle:public motorcar,public bicycle//()摩托車類的基類為自行車類和機動車類
{////////////////////////無虛基類?
public:
 motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);
void show(); //顯示摩托車的運行狀態
};
 
 
 
enum vehicleStaus {rest, running};  
using namespace std;
vehicle::vehicle(int maxS, int w):maxSpeed(maxS), currentSpeed(0),weight(w), status(rest){}  
 
/*vehicle::vehicle(int maxS, int w)
{
 S=maxS;
 weight=w;
 currentSpeed=0;
 status=rest;
}*/
void vehicle:: start() //由rest狀態到running, 初速為
{
 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)  
            ++currentSpeed;  
        else  
            cout<<"請不要超速行駛……"<<endl;  
    else  
        cout<<"車輛未啟動!"<<endl;  
 
}
 
void vehicle::slow_down()
{
 if (status==running)  
    {  
        if(currentSpeed>0)  
            --currentSpeed;  
    }  
    else  
        cout<<"車輛未啟動!"<<endl;  
    if(currentSpeed==0)  
        status=rest;  
 
}
 
bicycle::bicycle(int maxS, int w, int h):vehicle(maxS, w),height(h){}   
motorcar::motorcar(int maxS, int w, int s, int p): vehicle(maxS, w),seatNum(s),passengerNum(p){}
 
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;  
        }  
    }  
}  
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<<"請操作:-啟動2-加速3-減速4-有人上車5-有人下車6-停車-結束"<<endl;
  char keydown= _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>
 }
 system("pause");
 return 0;
}


 
 




感悟:1,調用基類構造函數時,對基類數據成員初始化

2,調用自對象構造函數,對子對象數據成員初始化。

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com