第十二周C++实验报告(三)
来源:互联网 发布:淘宝香水正品店 编辑:程序博客网 时间:2024/06/06 05:08
- #include<iostream>
- #include<conio.h>
- #include<windows.h>
- #include<iomanip>
- using namespace std;
- enum vehicleStaus {rest,running};
- class vehicle
- {
- protected:
- int maxSpeed;
- int currentSpeed;
- int weight;
- vehicleStaus status;
- public:
- vehicle(int maxS, int w);
- void start();
- void stop();
- void speed_up();
- void slow_down();
- int get_maxSpeed(){return maxSpeed;}
- int get_currentSpeed(){return currentSpeed;}
- int get_weight(){return weight;}
- int get_status(){return status;}
- };
-
- vehicle::vehicle(int maxS, int w)
- {
- maxSpeed=maxS;
- currentSpeed=0;
- weight=w;
- status=rest;
- }
-
- void vehicle::start()
- {
- currentSpeed=1;
- status= running;
-
- }
-
- void vehicle::stop()
- {
- if(currentSpeed<5)
- status=rest;
- else
- cout<<"当前速度大于5时,不允许停车!";
- }
-
- void vehicle::speed_up()
- {
- if(currentSpeed<maxSpeed)
- currentSpeed+=1;
- else
- cout<<"当前车速已是最大值,不允许加速!";
-
- }
-
- void vehicle::slow_down()
- {
- if(currentSpeed!=0)
- currentSpeed-=1;
- else
- status=rest;
- }
-
- class bicycle :virtual public vehicle
- {
- protected:
- double height;
- public:
- bicycle(int maxS=10, int w=50, double h=0.7);
- double get_height(){return height;}
- };
-
- bicycle::bicycle(int maxS, int w, double h):vehicle(maxS,w)
- {
- height=h;
- }
-
- 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);
- int get_seatNum(){return seatNum;}
- int get_passengerNum(){return passengerNum;}
- };
-
- motorcar::motorcar(int maxS, int w, int s, int p):vehicle(maxS,w)
- {
- seatNum=s;
- passengerNum=p;
- }
-
- void motorcar::addPassenger(int p)
- {
- if((p+passengerNum)>seatNum)
- cout<<"即将超员,请您配合,不要上车,等下一班车...";
- passengerNum+=p;
- }
-
- class motorcycle: public bicycle,public motorcar
- {
- 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== rest)?"泊车":"行进")<<" "<<"车速:"<<currentSpeed<<'/'<<maxSpeed<<" "<<"当前成员"<<passengerNum<<'/'<<seatNum;
- }
-
- int main( )
- {
- motorcycle m;
- bool end=false;
- while (!end){
- cout<<"请操作:1-启动 2-加速 3-减速 4-有人上车 5-有人下车 6-停车 0-结束"<<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);
- }
- system("pause");
- return 0;
- }