11th Week Item 5-motorcycles Inherit bicycles and motor vehicles

Source: Internet
Author: User

Questions and codes:

/* *copyright (c) 2016, Yantai University School of computer *all rights reserved. * File name: Zwj.cpp * Author: Zhang Weijing * Completion date: May 10, 2016 * Version number: v1.0 * * Problem Description: Motorcycle Inheritance Bike and motor vehicle * Input Description: * Program output: */#include <iostream> #incl  Ude<conio.h> #include <windows.h>using namespace Std;enum Vehiclestaus {rest, running}; Vehicle status: Parking, travel class vehicle//Vehicle class {Protected:int maxspeed;//max speed int currentspeed;//current speed int weight;//car weight vehicle Staus 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, initial velocity is 0.vehicle::vehicle (int maxs, int w): Maxspeed (MAXS), Currentspeed (0), Weight (W),        Status (REST) {}//defines individual member functions//start void Vehicle::start () {if (status==rest) {status = running;    Currentspeed = 1; } else cout<< "vehicle in motion! "<<endl;}    Parking void Vehicle::stop () {if (status = = running)    if (Currentspeed < 5) {status = rest;        currentspeed = 0;    } else cout<< "Please stop after slowing down ..." <<endl; Else cout<< "The vehicle is not started. "<<endl;}        Accelerated void Vehicle::speed_up () {if (status = = running) if (currentspeed<maxspeed) ++currentspeed; else cout<< "Please do not speed!"    "<<endl; Else cout<< "The vehicle is not started. "<<endl;} deceleration void Vehicle::slow_down () {if (status = = running) {if (currentspeed>0) currentspeed=current    Speed-1; } else cout<< "The vehicle was not started.    "<<endl; if (Currentspeed = = 0) status = Rest;} Class Bicycle:virtual Public vehicle//(1) The virtual base class of the bicycle class is the vehicle class {protected:double height;//vehicle height public:bicycle (int maxs=10   , int w=50, int h=0.7); Defining constructors};//Bike class constructors bicycle::bicycle (int maxs, int w, int h): vehicle (maxs,w), height (h) {}class motorcar:virtual public vehicle//(2) The virtual base class of motor vehicle class is also the vehicle class {Protected:int seatnum;//Seating number 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); 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. };//Motor vehicle Constructor Motorcar::motorcar (int maxs, int w, int s, int p): vehicle (MAXS,W), Seatnum (s), Passengernum (p) {}//motor vehicle Overcrowding case void Motorcar::addpassenger (int p) {if (status = = running) cout<< "vehicle is not allowed to alight.    "<<endl;        else {passengernum+=p;            if (passengernum>seatnum) {passengernum=seatnum; cout<< "The vehicle is full and no boarding is allowed.        "<<endl;            } else if (Passengernum < 1) {passengernum = 1; cout<< "Ask the driver not to leave the seat.        "<<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= s=3 int, int p=1, int h=0.7);//define constructor void Show (); Show the running status of motorcycles};//motorcycle Constructors motorcycle::motorcycle (int maxs, int w, int s, int p, int h): vehicle (maxs,w), bicycle(maxs,w,h), Motorcar (Maxs, W, S, p) {}//motorcycle status 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 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 ();        Break Case ' 5 ': cout<< "Selected action is 5-someone get 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:

The first level of inheritance is to set the common base class to the virtual base class.

The member of the virtual base class is called by the constructor of the most derived class, and the constructor of the virtual base class is initialized.

Learning experience:

When defining a vehicle class's constructor, careless forgetting to define the state, the compilation does not produce errors, but the operation is always wrong, the program from the beginning of a little bit of research to find out the error.

Be careful!!!


11th Week Item 5-motorcycles Inherit bicycles and motor vehicles

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.