11th Week Project 5 motorcycles Inherit bicycles and motor vehicles

Source: Internet
Author: User

/**copyright (c) 2016, Yantai University School of computer *all rights reserved.* file name:*    : Liu Yun * completion date: May 8, 2016 * version number: v6.0** problem description:  Motorcycle Succession Vehicles and motor vehicles
* Input Description: no * program output:*/#include <iostream> #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): Maxspeed (MAXS), Weight (w) {}//constructor, initial, current speed is always 0 and is in parking state void St  Art (); 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};void Vehicle::start () {status=running; currentspeed=1;}    void Vehicle::stop () {status=rest; if (currentspeed<5) cout<< "Allow parking!!! "<<endl;} void Vehicle::speed_up () {currentspeed+=1;}    void Vehicle::slow_down () {if (currentspeed>0) currentspeed-=1; else cout<< "Parking!!! "<<endl;} Class Bicycle:virtual public vehicle//(1) Bike class virtual base class for vehiclesclass {protected:double height;//vehicle height public:bicycle (int maxs=10, int w=50, int h=0.7): vehicle (maxs,w), height (h) {}//define Construct Build function};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;//By Guest    Number Public:motorcar (int maxs=150, int w=1500, int s=5, int p=1): vehicle (MAXS,W), Seatnum (s), Passengernum (p) {}//define 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.       };void motorcar::addpassenger (int p) {if (status==rest) {passengernum+=p; } else cout<< "Can't get off!!!" "<<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, int p=1, int h=0.7): vehicle (maxs,w), bicycle (maxs,w,h), motorcar (maxs,w,s,p) {}//definition constructor void Show ();    Displays the running status of the motorcycle};void Motorcycle::show () {if (status==running) cout<< "Running" <<endl; else cout<< "Rest" &LT;&LT;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<< "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;

Virtual base class main problem solved:

The virtual base class is relative to its derived class, which can itself be a generic class. It is called a virtual base class only if its derived class is virtual, and it is called a base class If there is no virtual inheritance. For example, the virtual Class B inherits from Class A, that class A is called the virtual base class B, if there is no virtual inheritance, then the Class B is just the base class of Class A. Virtual inheritance is primarily used in cases where a class inherits multiple classes, avoiding the repetition of inheriting the same class two or more times. For example, Class A derives from Class B and Class C, and Class D inherits both Class B and Class C, at which point Class D avoids inheriting class A two times in a virtual inheritance way. Abstract classes refer to classes with one or more pure virtual functions. Abstract class generic values are used for inheritance, you cannot define class objects, but you can define class pointers and references.

The most important reason for the ambiguity is that the base class produces two child objects in the derived class, resulting in the uniqueness of access to the base class members. So you just have to make the public base class produce only one sub-object.

In other words, the virtual base class constructor is guaranteed to be called only once. The virtual base class from the constructor of the most derived class is implemented by calling the constructor of the virtual base class, at which point the call to the virtual base class's constructor listed in all base classes of the most derived class is ignored during execution, and from the guaranteed initialization of the virtual base class from the object value. Also, the initialization list for the most derived class must list the call to the virtual base class constructor, or, if not listed, the default constructor that uses the virtual base class.

11th Week Project 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.