第十二周C++实验报告(三)

来源:互联网 发布:淘宝香水正品店 编辑:程序博客网 时间:2024/06/06 05:08
[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<conio.h>  
  3. #include<windows.h>  
  4. #include<iomanip>  
  5. using namespace std;  
  6. enum vehicleStaus {rest,running};  //车辆状态:泊车、行进  
  7. class vehicle //车辆类  
  8. {  
  9. protected:  
  10.     int maxSpeed;       //最大车速  
  11.     int currentSpeed;   //当前速度  
  12.     int weight;         //车重  
  13.     vehicleStaus status; //rest-泊车状态;running-行进状态  
  14. public:  
  15.     vehicle(int maxS, int w); //构造函数,初始时,当前速度总为0且处在停车状态  
  16.     void start();  //由rest状态到running, 初速为1  
  17.     void stop(); //由running状态到rest, 当前速度小于5时,才允许停车  
  18.     void speed_up();  //加速,调用1次,速度加1  
  19.     void slow_down(); //减速,调用1次,速度减1,速度为0时,停车  
  20.     int get_maxSpeed(){return maxSpeed;}  
  21.     int get_currentSpeed(){return currentSpeed;}  
  22.     int get_weight(){return weight;}  
  23.     int get_status(){return status;}  
  24. };  
  25.   
  26. vehicle::vehicle(int maxS, int w) //构造函数,初始时,当前速度总为0且处在停车状态   
  27. {  
  28.     maxSpeed=maxS;  
  29.     currentSpeed=0;  
  30.     weight=w;  
  31.     status=rest;  
  32. }  
  33.   
  34. void vehicle::start()  //由rest状态到running, 初速为1  
  35. {  
  36.     currentSpeed=1;  
  37.     status= running;  
  38.       
  39. }  
  40.   
  41. void vehicle::stop() //由running状态到rest, 当前速度小于5时,才允许停车  
  42. {  
  43.     if(currentSpeed<5)  
  44.         status=rest;  
  45.     else  
  46.         cout<<"当前速度大于5时,不允许停车!";  
  47. }  
  48.   
  49. void vehicle::speed_up()  //加速,调用1次,速度加1  
  50. {  
  51.     if(currentSpeed<maxSpeed)  
  52.         currentSpeed+=1;  
  53.     else  
  54.         cout<<"当前车速已是最大值,不允许加速!";  
  55.       
  56. }  
  57.   
  58. void vehicle::slow_down() //减速,调用1次,速度减1,速度为0时,停车  
  59. {  
  60.     if(currentSpeed!=0)  
  61.         currentSpeed-=1;  
  62.     else  
  63.         status=rest;  
  64. }  
  65.   
  66. class bicycle :virtual public vehicle  //(1)自行车类的虚基类为车辆类  
  67. {   
  68. protected:  
  69.     double height; //车高  
  70. public:  
  71.     bicycle(int maxS=10, int w=50, double h=0.7);   //定义构造函数  
  72.     double get_height(){return height;}  
  73. };  
  74.   
  75. bicycle::bicycle(int maxS, int w, double h):vehicle(maxS,w)   //定义构造函数  
  76. {  
  77.     height=h;  
  78. }  
  79.   
  80. class motorcar :virtual public vehicle  //(2)机动车类的虚基类也为车辆类  
  81. {   
  82. protected:  
  83.     int seatNum; //座位数  
  84.     int passengerNum; //乘客人数  
  85. public:  
  86.     motorcar(int maxS=150, int w=1500, int s=5, int p=1);   //定义构造函数  
  87.     void addPassenger(int p=1);   //搭载乘客,超员要拒载,有人下车时,p为负数。当然车上乘客至少有1个(司机)。上下车时要保证安全……  
  88.     int get_seatNum(){return seatNum;}  
  89.     int get_passengerNum(){return passengerNum;}  
  90. };  
  91.   
  92. motorcar::motorcar(int maxS, int w, int s, int p):vehicle(maxS,w)  //定义构造函数  
  93. {  
  94.     seatNum=s;  
  95.     passengerNum=p;  
  96. }  
  97.   
  98. void motorcar::addPassenger(int p)   //搭载乘客,超员要拒载,有人下车时,p为负数。当然车上乘客至少有1个(司机)。上下车时要保证安全……  
  99. {  
  100.     if((p+passengerNum)>seatNum)  
  101.         cout<<"即将超员,请您配合,不要上车,等下一班车...";  
  102.     passengerNum+=p;  
  103. }  
  104.   
  105. class motorcycle: public bicycle,public motorcar //(3)摩托车类的基类为自行车类和机动车类  
  106. {   
  107. public:  
  108.     motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);   //定义构造函数  
  109.     void show(); //显示摩托车的运行状态  
  110. };  
  111.   
  112. motorcycle::motorcycle(int maxS, int w, int s, int p, int h):vehicle(maxS,w),bicycle(maxS,w,h),motorcar(maxS,w,s,p){}  //定义构造函数  
  113.   
  114. void motorcycle::show() //显示摩托车的运行状态  
  115. {  
  116.     cout<<"状态:"<<((status== rest)?"泊车":"行进")<<"           "<<"车速:"<<currentSpeed<<'/'<<maxSpeed<<"          "<<"当前成员"<<passengerNum<<'/'<<seatNum;  
  117. }  
  118.   
  119. int main( )  
  120. {  
  121.     motorcycle m;  
  122.     bool end=false;  
  123.     while (!end){  
  124.         cout<<"请操作:1-启动  2-加速  3-减速  4-有人上车  5-有人下车  6-停车 0-结束"<<endl;  
  125.         char keydown= _getch(); //_getch()返回键盘上读取的字符,应包含头文件<conio.h>  
  126.         switch(keydown)  
  127.         {  
  128.         case '1':   
  129.             cout<<"操作(启动)\t"; m.start(); break;  
  130.         case '2':                           
  131.             cout<<"操作(加速)\t"; m.speed_up(); break;  
  132.         case '3':                            
  133.             cout<<"操作(减速)\t"; m.slow_down(); break;  
  134.         case '4':                          
  135.             cout<<"操作(有人上车)\t"; m.addPassenger(); break;  
  136.         case '5':                        
  137.             cout<<"操作(有人下车)\t"; m.addPassenger(-1); break;  
  138.         case '6':                        
  139.             cout<<"操作(停车)\t"; m.stop(); break;  
  140.         case '0':                 
  141.             end=truebreak;  
  142.         }  
  143.         m.show();  
  144.         cout<<endl;  
  145.         Sleep(200);  //要包含头文件<windows.h>  
  146.     }  
  147.     system("pause");  
  148.     return 0;