实验报告 12-3

 
  1. /* (程序头部注释开始)       
  2. * 程序的版权和版本声明部分       
  3. * Copyright (c) 2011, 烟台大学计算机学院学生        
  4. * All rights reserved.       
  5. * 文件名称:                                     
  6. * 作    者:   zhang  lu              
  7. * 完成日期:   2012   年  4 月 16 日       
  8. * 版 本 号:                 
  9. * 对任务及求解方法的描述部分       
  10. * 输入描述:       
  11. * 问题描述:实现分数类中的<<和>>运算符重载,实现分数的输入和输出      
  12. * 程序头部的注释结束       
  13. */      
  14. #include <iostream>   
  15. #include<conio.h>   
  16. #include <windows.h>   
  17. using namespace std;  
  18. enum vehicleStaus {rest, running};  //车辆状态:泊车、行进   
  19. class vehicle //车辆类   
  20. {  
  21. protected:  
  22.  int maxSpeed;  //最大车速   
  23.  int currentSpeed; //当前速度   
  24.  int weight;   //车重   
  25.  vehicleStaus status; //rest-泊车状态;running-行进状态   
  26. public:  
  27.  vehicle(int maxS, int w); //构造函数,初始时,当前速度总为0且处在停车状态   
  28.  void start();  //由rest状态到running, 初速为1   
  29.  void stop(); //由running状态到rest, 当前速度小于5时,才允许停车   
  30.  void speed_up();  //加速,调用1次,速度加1   
  31.  void slow_down(); //减速,调用1次,速度减1,速度为0时,停车   
  32. };  
  33. vehicle::vehicle(int maxS,int w)  
  34. {  
  35.  maxSpeed=maxS;  
  36.  currentSpeed=0;  
  37.  status=rest;  
  38. }  
  39. void vehicle::start()//由rest状态到running, 初速为1   
  40. {  
  41.  if(status==running)  
  42.  {  
  43.   cout<<"您的车已经启动,无需重新启动!"<<endl;  
  44.  }  
  45.  else  
  46.  {  
  47.     
  48.   status=running;  
  49.   currentSpeed=1;  
  50.  }  
  51. }  
  52. void vehicle::stop()//由running状态到rest, 当前速度小于5时,才允许停车   
  53. {  
  54.  if(currentSpeed>=5)  
  55.  {  
  56.   cout<<"车速过大,请减速后停车! "<<endl;  
  57.  }  
  58.  else  
  59.  {  
  60.   currentSpeed=0;  
  61.   status=rest;  
  62.  }  
  63. }  
  64. void vehicle::speed_up()//加速,调用1次,速度加1   
  65. {  
  66.  if(currentSpeed>=maxSpeed)  
  67.  {  
  68.   cout<<"为了您的安全,您已被限制加速!"<<endl;  
  69.  }  
  70.  else  
  71.  {  
  72.   currentSpeed++;  
  73.  }  
  74. }  
  75. void vehicle::slow_down()//减速,调用1次,速度减1,速度为0时,停车   
  76.   
  77. {  
  78.  if(currentSpeed>0)  
  79.   currentSpeed--;  
  80. }  
  81.   
  82.   
  83. class bicycle :virtual public vehicle//(1)自行车类的虚基类为车辆类   
  84. {   
  85. protected:  
  86.  double height; //车高   
  87. public:  
  88.  bicycle(int maxS=10, int w=50, int h=0.7);   //定义构造函数   
  89. };  
  90. bicycle::bicycle(int maxS,int w,int h):vehicle(maxS,w){height=h;}  
  91.   
  92.   
  93. class motorcar : virtual public vehicle//(2)机动车类的虚基类也为车辆类   
  94. {   
  95. protected:  
  96.  int seatNum; //座位数   
  97.  int passengerNum; //乘客人数   
  98. public:  
  99.  motorcar(int maxS=150, int w=1500, int s=5, int p=1);   //定义构造函数   
  100.  void addPassenger(int p=1);   //搭载乘客,超员要拒载,有人下车时,p为负数。当然车上乘客至少有1个(司机)。上下车时要保证安全……   
  101. };  
  102. motorcar::motorcar(int maxS,int w,int s,int p):vehicle(maxS,w){seatNum=s;passengerNum=p;}  
  103. void motorcar::addPassenger(int p)  
  104. {  
  105.  stop();  
  106.  if(currentSpeed==0)  
  107.  {  
  108.   passengerNum+=p;  
  109.   if(passengerNum>seatNum)  
  110.   {  
  111.    cout<<"对不起,该车次已经满员!"<<endl;  
  112.    passengerNum=seatNum;  
  113.      
  114.   }  
  115.  }  
  116.    
  117. }  
  118.   
  119.   
  120. class motorcycle: public bicycle,public motorcar //(3)摩托车类的基类为自行车类和机动车类   
  121. {   
  122. public:  
  123.  motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);   //定义构造函数   
  124.  void show(); //显示摩托车的运行状态   
  125. };  
  126. motorcycle::motorcycle(int maxS,int w,int s,int p,int h):vehicle(maxS,w),bicycle(s,p,h),motorcar(maxS,w,s,p){}  
  127. void motorcycle::show()  
  128. {  
  129.  cout<<"最大车速:"<<maxSpeed<<endl;  
  130.  cout<<"当前车速:"<<currentSpeed<<endl;  
  131.  cout<<"车重:"<<weight<<endl;  
  132.  cout<<"车辆状态:"<<status<<endl;  
  133.  cout<<"乘客数量:"<<passengerNum<<endl;  
  134.  cout<<"车高:"<<height<<endl;  
  135. }  
  136.   
  137.   
  138. int main( )  
  139. {  
  140.  motorcycle m;  
  141.  bool end=false;  
  142.  while (!end){  
  143.   cout<<"请操作:1-启动  2-加速  3-减速  4-有人上车  5-有人下车  6-停车 0-结束"<<endl;  
  144.   char keydown= _getch(); //_getch()返回键盘上读取的字符,应包含头文件<conio.h>   
  145.   switch(keydown)  
  146.   {  
  147.   case '1':   
  148.    cout<<"操作(启动)\t"; m.start(); break;  
  149.   case '2':                           
  150.    cout<<"操作(加速)\t"; m.speed_up(); break;  
  151.   case '3':                            
  152.    cout<<"操作(减速)\t"; m.slow_down(); break;  
  153.   case '4':                          
  154.    cout<<"操作(有人上车)\t"; m.addPassenger(); break;  
  155.   case '5':                        
  156.    cout<<"操作(有人下车)\t"; m.addPassenger(-1); break;  
  157.   case '6':                        
  158.    cout<<"操作(停车)\t"; m.stop(); break;  
  159.   case '0':                 
  160.    end=truebreak;  
  161.   }  
  162.   m.show();  
  163.   cout<<endl;  
  164.   Sleep(200);  //要包含头文件<windows.h>   
  165.  }  
  166.  //system("pause");   
  167.  return 0;  
  168. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值