13 weeks Task 3 Motorcycle

Source: Internet
Author: User
# Include <iostream> # include <conio. h> # include <windows. h> using namespace std; enum vehicleStaus {rest, running}; // vehicle status: Parking, traveling class vehicle // vehicle type {protected: int maxSpeed; // maximum speed int currentSpeed; // int weight of the current speed; // vehicle weight vehicleStaus status; // rest-parking status; running-traveling state public: vehicle (int maxS, int w); // constructor, initially, the current speed is always 0 and is in the parking status void start (); // from rest to running, the initial speed is 1 void stop (); // from running to rest, parking void is allowed only when the current speed is less than 5 Speed_up (); // acceleration, 1 call, 1 void slow_down (); // deceleration, 1 call, 1 speed reduction, 0, parking }; vehicle: vehicle (int maxS, int w): maxSpeed (maxS), currentSpeed (0), weight (w), status (rest) {} void vehicle: start () {if (status = rest) {currentSpeed = 1; status = running;} else cout <"the car is running:" ;}void vehicle: stop () {if (status = running) {if (currentSpeed <= 5) status = rest; else cout <"sorry, the vehicle speed is too high. First, stop it ";} else cout <"car not started:";} void vehicle:: Speed_up () {if (currentSpeed <maxSpeed) currentSpeed ++; else cout <"has reached the playing speed and cannot be accelerated:";} void vehicle :: slow_down () {if (currentSpeed> 1) currentSpeed --; else status = rest;} class bicycle: virtual public vehicle // (1) the bicycle virtual base class is the vehicle class {protected: double height; // the public: bicycle (int maxS = 10, int w = 50, int h = 0.7 ); // define the constructor}; bicycle: bicycle (int maxS, int w, int h): vehicle (maxS, w), height (h) {} class motorcar: virtual Public vehicle // (2) the virtual foundation class for motor vehicles is also the 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); // define the constructor void addPassenger (int p = 1 ); // increase the number of passengers on board. The number of passengers exceeding the number of passengers is negative when someone gets off the bus. Of course, there must be at least one passenger (driver) on the bus ). Customers can only get on or off after the vehicle is stopped .}; Motorcar: motorcar (int maxS, int w, int s, int p): vehicle (maxS, w), seatNum (s), passengerNum (p) {} void motorcar :: addPassenger (int p) {if (seatNum <5) p ++; else cout <"the car is full and cannot be accessed:";} class motorcycle: public bicycle, public motorcar // (3) The base class of the motorcycle class is bicycle class and motor class {public: // definition constructor motorcycle (int maxS = 90, int w = 100, int s = 3, int p = 1, int h = 0.7); void show (); // display the running status of the motorcycle}; // constructor motorcycle :: motorcycle (int maxS, int w, int S, int p, int h): vehicle (maxS, w), bicycle (s, w, h), motorcar (maxS, w, s, p) {} void motorcycle:: show () {cout <"status:"; if (status = rest) cout <"parking:"; else cout <:"; cout <"Speed:" <currentSpeed <"" <"Number of passengers:" <passengerNum;} int main () {motorcycle m; bool end = false; while (! End) {cout <"Operation: 1-Start 2-acceleration 3-slow down 4-get on the bus 5-get off 6-Stop 0-end" <endl; char keydown = _ getch (); // _ getch () returns the character switch (keydown) {case '1': cout <"Operation (start) read on the keyboard) \ t "; m. start (); break; case '2': cout <"Operation (acceleration) \ t"; m. speed_up (); break; case '3': cout <"Operation (deceleration) \ t"; m. slow_down (); break; case '4': cout <"Operation (someone gets on the bus) \ t"; m. addPassenger (); break; case '5': cout <"Operation (get off) \ t"; m. addPassenger (-1); break; case '6': cout <"Operation (Parking) \ t"; m. stop (); break; case '0': end = true; break;} m. show (); cout <endl; Sleep (200); // header file to be included <windows. h >}return 0 ;}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.