Questions and codes:
/*copyright School of Computer and control engineering completion Date: May 8, 2016 Ma Yanyan file name: Teacher and Cadre category Problem Description: (1) According to the above categories of the relationship between the description, complete the following section of the code is vacant, (2) implement the member functions declared in the program, Note the prompt should be given when the condition of the action in the corresponding action does not meet. (3) Run the program and enjoy the process of driving a motorcycle. (You can download the executable file Motorcar.exe, run the reprogramming first.) No need to apply for a driver's license, this motorcycle is safe. ) input Description: No output Description: Member Information */#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};//constructor, initially, the current speed is always 0 and in the parking state vehicle::vehicle (int maxs, int w): Maxspeed (MAXS), Currentspeed (0 ), Weight (W), status (rest) {}//start: From rest state to running, muzzle velocity is 1void vehicle::start () {if (status==rest) {Status=runni Ng currentspeed=1; } ELSE cout<< "The vehicle has been driving! "<<endl;} Parking void Vehicle::stop () {if (status==running) if (currentspeed<5) {if the running state to rest, current speed is less than 5 o'clock Status=rest; Currentspeed=0; } else cout<< "Speed too fast!" Slow down before stopping ... "<<endl; else cout<< "Vehicle not activated! "<<endl;} Acceleration, call 1 times, speed plus 1void vehicle::speed_up () {if (status==running) if (currentspeed<maxspeed) ++currentsp Eed else cout<< "Please don't go speeding ..." <<endl; else cout<< "Vehicle not activated! "<<endl;} Slow down, call 1 times, speed minus 1, speed is 0 o'clock, stop void Vehicle::slow_down () {if (status==running) {if (currentspeed>0)--c Urrentspeed; } else cout<< "Vehicle not started! "<<endl; if (currentspeed==0) status=rest;} Class Bicycle:virtual Public Vehicle//() Bicycle class {protected:double height;//car height public:bicycle (int maxs=10, int w=50, int h=0.7); Define constructor}; bicycle::bicycle (int maxs, int w, int h): Vehicle (maxS, W), height (h) {}class motorcar:virtual public vehicle//() motor vehicle class {Protected:int seatnum;//number of seats int passengernum;// Number of passengers public:motorcar (int maxs=150, int w=1500, int s=5, int p=1); Defines the constructor void Addpassenger (int p=1); Carrying passengers, overcrowding to refuse, when someone alighted, p is negative. Of course, there are at least 1 passengers (drivers) in the car. Keep safe when you get off the bus ...};//define constructors motorcar::motorcar (int maxs, int w, int s, int p): Vehicle (Maxs, W), Seatnum (s), Passengernum (p) {}// Carrying passengers, overcrowding to refuse, when someone alighted, p is negative. Of course, there are at least 1 passengers (drivers) in the car. Keep safe when you get off the bus ... void motorcar::addpassenger (int p) {if (status==running) {cout<< "vehicle is driving, stop and get off! "<<endl; } else {passengernum+=p; if (passengernum>seatnum) {passengernum=seatnum; cout<< "allegedly overcrowding, has been cleared to reach full!" "<<endl; } else if (passengernum<1) {passengernum=1; cout<< "Ask the driver not to leave the post!" "<<endl; }}}class motorcycle:public Bicycle, Public motorcar//() Motorcycle class {public:motorcycle (int maxs=90, int w=100, int s=3, I NT P=1, int h=0.7); Define constructor void Show (); Displays the running status of the motorcycle};//definition constructor motorcycle::motorcycle (int maxs, int w, int s, int p, int h): Vehicle (Maxs, W), Bicycle (Maxs, W, h), Mo Torcar (Maxs, W, S, p) {}//shows the running status of the motorcycle void Motorcycle::show () {cout<< "status:"; if (status==rest) cout<< "parking; \ t"; else cout<< "marching; \ t"; cout<< "Speed:" <<currentSpeed<< "/" << maxspeed << "\ t 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 characters read on the keyboard, which should include the header file <conio.h> switch (keydown) {case ' 1 ': cout<< "selected operator 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 (); 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:
Summary of Knowledge points:
Virtual base class mainly solves the problem of two semantics.
10th, 11 weeks-motorcycles inherit bicycles and motor vehicles