Week 3 Project 3-vehicle categories (implemented using virtual bases)

Source: Internet
Author: User
/** Copyright and version declaration section of the Program * Copyright (c) 2012, student * All rightsreserved from computer College of Yantai University. * file name: object. cpp * Author: Yang Chen * Completion Date: July 15, May 24, 2013 * version: v1.0 * input Description: none * Problem description: * program output: */# 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-row Public: vehicle (int maxS, int w); // constructor. Initially, the current speed is always 0 and void start () is in the stopped state (); // from the rest state to running, the initial speed is 1 void stop (); // from the running state to rest, the current speed is less than 5, the void speed_up () can be stopped (); // acceleration, call once, speed plus 1 void slow_down (); // deceleration, call once, speed minus 1, speed is 0, parking }; // constructor. Initially, the current speed is always 0 and the vehicle: vehicle (int maxS, int w): maxSpeed (maxS), currentSpeed (0 ), weight (w), status (rest) {}// start: from rest to running, the initial speed is 1 void vehicle: start () {if (status = rest ){ Status = running; currentSpeed = 1;} elsecout <"the vehicle is running! "<Endl;} // from running to rest. The void vehicle: stop () {if (status = running) can be stopped only when the current speed is less than 5) if (currentSpeed <5) {status = rest; currentSpeed = 0;} elsecout <"speed too fast! Slow down before stopping ...... "<Endl; elsecout <" the vehicle is not started! "<Endl;} // acceleration, call once, speed plus 1 void vehicle: speed_up () {if (status = running) if (currentSpeed <maxSpeed) ++ currentSpeed; elsecout <"Please do not overspeed ...... "<Endl; elsecout <" the vehicle is not started! "<Endl;} // slow down, call once, speed minus 1, speed is 0, parking void vehicle: slow_down () {if (status = running) {if (currentSpeed> 0) -- currentSpeed;} elsecout <"the vehicle is not started! "<Endl; if (currentSpeed = 0) status = rest;} class bicycle: virtual public vehicle // () bicycle class {protected: double height; // vehicle height public: bicycle (int maxS = 10, int w = 50, int h = 0.7); // defines the 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); // define the constructor void addPassenger (int p = 1); // The number of passengers carrying the constructor. When a passenger gets off the bus, the value of p is negative. Of course, there must be at least one passenger (driver) on the bus ). Ensure security when getting on and off the bus ......}; // Define the constructor motorcar: motorcar (int maxS, int w, int s, int p): vehicle (maxS, w), seatNum (s), passengerNum (p) {} // the number of passengers on board. The number of passengers on the train is negative when someone gets off the bus. Of course, there must be at least one passenger (driver) on the bus ). Ensure security when getting on and off the bus ...... Void motorcar: addPassenger (int p) {if (status = running) {cout <"the vehicle is driving. Stop and get on and off again! "<Endl ;}else {passengerNum + = p; if (passengerNum> seatNum) {passengerNum = seatNum; cout <" suspected of being overstaffed. After cleaning, it reaches full capacity! "<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, int p = 1, int h = 0.7); // define the constructor void show (); // display the running status of the motorcycle }; // define the constructor motorcycle: motorcycle (int maxS, int w, int s, int p, int h): vehicle (maxS, w), bicycle (maxS, w, h), motorcar (maxS, w, s, p) {}// display the running status of the motorcycle void motorcycle: show () {cout <"status :"; if (status = rest) cout <"parking; \ t"; elsecout <"traveling ;\ T "; cout <" Speed: "<currentSpeed <"/"<maxSpeed <" \ t current occupant: "<passengerNum <"/"<seatNum <endl ;}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 read on the keyboard, which should contain the header file <conio. h> switch (keydown) {case '1': cout <"Operation (start) \ 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 ;}

Output 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.