/*copyright (c) 2016. College of Computer Science, Yantai University * All rights reserved, * file name: text. CPP * Liu Tao * completion date: May 9, 2016 * version number: vc++6.0 * Problem Description: In the following paragraph of the definition of class, the virtual base class of bicycles is the vehicle class, the virtual base class of motor vehicles is also the vehicle class, motorcycle class base class for bicycles and motor vehicles, between the class are public inheritance. Download the executable link motorcar.exe. (1) According to the above categories of the relationship between the description, complete the following sections of the code is vacant, (2) implement the member functions declared in the program, note that the action in the corresponding action occurs when the conditions are not satisfied should be given a hint. (3) Run the program and enjoy the process of driving a motorcycle. (You can download the executable file in the blog, start the motorcycle, then program.) No need to apply for a driver's license, this motorcycle is safe. ) */#include <iostream> #include <conio.h> #include <windows.h>using namespace Std;enum Vehiclestaus { rest, running}; Vehicle status: Parking, travel class vehicle//Vehicle class {Protected:int maxspeed; Maximum speed int currentspeed; Current speed int weight; Vehicle weight Vehiclestaus status; rest-parking status; running-travel status public:vehicle (int maxs, int w); Constructor, when initially, the current speed is always 0 and in the parking state void start (); From rest state to running, the initial velocity is 1 void stop (); Parking void speed_up () is allowed by the running state to rest at the current speed of less than 5 o'clock; Acceleration, call 1 times, speed plus 1 void slow_down (); Deceleration, call 1 times, speed minus 1, speed is 0 o'clock, parking};vehicle::vehicle (int maxs,int w) {maxspeed=maxs; Weight=w; Currentspeed=0; Status=rest;} void Vehicle::start () {if (status==running) currentspeed=1; else status=running; currentspeed=1; cout<< "Vehicle has been started" <<ENDL;} void Vehicle::stop () {if (status==running) {if (currentspeed<5) {status=rest; Currentspeed=0; cout<< "Vehicle already docked" <<endl; } else cout<< "Speed too fast, please slow down and stop" <<endl; } else cout<< "The vehicle is already in restful condition and does not need to stop" <<ENDL;} void Vehicle::speed_up () {if (status==running) {currentspeed++; } else cout<< "The vehicle is in rest, please start the vehicle first" <<endl; status=running;} void Vehicle::slow_down () {if (status==running) {if (currentspeed==1) {cout<< "vehicle speed minus one, Already stopped "<<endl; Status=rest; Currentspeed=0; } else currentspeed--; } else cout<< "The vehicle is in rest and does not need to be slowed down!" <<endl;} /*--------------------------------------------------------------------------------------------------*/class bicycle:virtual Public vehicle//(1) Bicycle class virtual base class for the vehicle class { protected:double height; Car height public:bicycle (int maxs=10, int w=50, int h=0.7): vehicle (maxs,w), height (h) {}//define constructor};/*----------------------- ----------------------------------------------------------------------------*/class motorcar:virtual Public vehicle//(2) The virtual base class of motor vehicle class is also the vehicle class {Protected:int seatnum;//seat number int passengernum;//passenger number Public:motorcar (int maxs=150, in T w=1500, int s=5, int p=1): vehicle (MAXS,W), Seatnum (s), Passengernum (p) {}//define constructor void Addpassenger (int p); To increase the number of passengers, overcrowding to refuse, when someone gets off, p is negative. Of course, there are at least 1 passengers (drivers) in the car. Only when the car stops steady can the guest. };void motorcar::addpassenger (int p) {if (status==rest) {if (seatnum>=passengernum+p) {cout& lt;< "Vehicles can carry all passengers without overloading" <<endl; Passengernum=passengernum+p; } else {passengernum=seatnum; cout<< "The vehicle is full, unable to carry all passengers, and refused to refuse" <<passengerNum+p-seatNum<<"Passengers" <<endl; }} else cout<< "The vehicle is driving, please stop and then get off the bus" <<ENDL; /*--------------------------------------------------------------------------------------------------*/class Motorcycle:public bicycle,public motorcar//(3) Motorcycle class base class for bicycle class and motor vehicle class {public:motorcycle (int maxs=90, int w=100, int s=3, I NT P=1, int h=0.7): vehicle (maxs,w), bicycle (maxs,w,h), motorcar (maxs,w,s,p) {}//definition constructor void Show (); Show the running status of the motorcycle};void Motorcycle::show () {cout<< "status:"; if (status==running) cout<< "marching;"; else cout<< "parking;"; cout<< "Speed:" <<currentSpeed<< "/" <<maxSpeed<< "" << "current occupant:" <<passengernum << "/" <<SEATNUM<<ENDL;} /*--------------------------------------------------------------------------------------------------*/int Main ( ) {Motorcycle m; BOOL End=false; while (!end) {cout<< "please operate: 1-Start 2-acceleration 3-deceleration 4-people get on the bus 5-someone gets off 6-stop 0-end" <<endl; Char keydown= _Getch (); _getch () returns the character read on the keyboard switch (keydown) {case ' 1 ': cout<< "The selected operation is 1-start \ t"; M.start (); Break Case ' 2 ': cout<< "The selected operation is 2-acceleration \ t"; M.speed_up (); Break Case ' 3 ': cout<< "The selected operation is 3-deceleration \ t"; M.slow_down (); Break Case ' 4 ': cout<< "The selected operation is 4-someone get on the bus \ T"; M.addpassenger (1); Break Case ' 5 ': cout<< "The selected operation is 5-someone gets off \ t"; M.addpassenger (-1); Break Case ' 6 ': cout<< "The selected operation is 6-stop \ T"; M.stop (); Break Case ' 0 ': end=true; Break } m.show (); cout<<endl; Sleep (200); To include header file <windows.h>} return 0;}
Operation Result:
Knowledge Point: Virtual base class.
Learning experience:
1. The use of enumeration types works well when the state needs to switch between several states.
2. There is also the need to consider the actual situation, can not do whatever.
3. Another problem is that the motorcycles and bicycles here don't seem to be used, and the main function is finally output, and their speed, weight, height and so on are no use.
4. The role of virtual base class: When some or all of the direct base classes of a class are derived from another common base class, in the object of the derived class, those data members with the same name will have multiple replicas, consuming memory, this time using the virtual base class to set the common base class to the virtual base class, A data member with the same name inherited from a different path has only one copy in memory, and the same function name has only one mapping.
Tenth week 11th week on the Machine Practice project-Item 5-Motorcycles Inherit bicycles and motor vehicles