Multiple inheritance of C + +

Source: Internet
Author: User

Most applications use common inheritance for a single base class, but in some cases, single inheritance is not enough, and multiple inheritance must be used. C + + allows you to specify multiple base classes for a derived class, so that the inheritance structure is called multiple inheritance.

For example, a vehicle class can derive a car and a ship's sub-class, but a vehicle and a ship's common characteristic amphibious vehicle must inherit the common properties from the car class and the ship class. As shown:


Code implementation:

Multiple inheritance #include <iostream> using namespace std; Class Vehicle {public:vehicle (int weight = 0) {vehicle::weight = weight;} void setweight (int weight) {cout << Reset weight "<< Endl; Vehicle::weight = weight; } virtual void ShowMe () = 0;//pure virtual function protected:int weight;  Private:}; Class Car:public vehicle//Auto {public:car (int weight = 0, int aird = 0): Vehicle (weight) {car::aird = aird;} void SHOWM E () {cout << "I'm a car!" "<< Endl;} Protected:int Aird;  Private:};  Class Boat:public vehicle//ship {public:boat (int weight = 0, float tonnage = 0): Vehicle (weight) {boat::tonnage = tonnage; } void ShowMe () {cout << "I'm a boat!" "<< Endl;} Protected:float tonnage;  Private:}; Class Amphibiancar:p ublic car, public boat//amphibious vehicle, multi-inheritance embodiment {public:amphibiancar (int weight, int aird, float tonnage): V Ehicle (weight), car (weight, Aird), boat (weight, tonnage)//Multiple inheritance to pay attention to calling the base class constructor {} void ShowMe () {cout << "I'm an amphibious car!"  "<< Endl;}}; int main () {AmphibiaNCar A (4, 1.35f);//Error A.setweight (3);//Error System ("pause"); }

The above code looks from the surface, there is no obvious error in the language, but it is not able to compile. The error is as follows:


What is this for? This is due to the problem caused by the ambiguity of inheritance caused by multiple inheritance.

Let's look at the following diagram:


The deep red mark in the figure is the main problem, the amphibious car class inherits from the car class and boat class properties and methods, car class and boat similar to the Amphibiancar class base class, On memory allocation, Amphibiancar obtains the setweight () member function from two classes when we call a. Setweight (3) The computer does not know how to choose the Setweight () of a class member function that is repeatedly owned by two base classes.

For example, in the code above, we want to get a copy of the Amphibiancar class both vehicle and share the data members and member functions of the car class and the boat class by using the virtual inheritance technology provided by C + +.

When the car class and the boat class inherit the vehicle class, you can implement virtual inheritance with the virtual keyword, and when the system encounters multiple inheritance, it automatically joins a vehicle copy. When a vehicle copy is requested again, it is ignored, guaranteeing the uniqueness of the inherited class member function.

The Modified code:

The correct code after adding virtual #include <iostream> using namespace std;class vehicle{public:vehicle (int weight = 0) {Vehicle::we ight = weight;cout << "Load vehicle class constructors" << Endl; void setweight (int weight) {cout << reset weight << Endl; Vehicle::weight = weight;} virtual void ShowMe () = 0;protected:int weight;}; Class Car:virtual public vehicle//car, here is virtual inheritance {public:car (int weight = 0, int aird = 0): Vehicle (weight) {Car::aird = Aird ; cout << "Loading car class constructors" << Endl;} void ShowMe () {cout << "I'm a car!" "<< Endl;} Protected:int Aird;}; Class Boat:virtual public vehicle//ship, here is virtual inheritance {public:boat (int weight = 0, float tonnage = 0): Vehicle (weight) {Boat::tonn Age = Tonnage;cout << "Load Boat class constructor" << Endl;} void ShowMe () {cout << "I'm a boat!" "<< Endl;} Protected:float tonnage;}; Class Amphibiancar:p ublic car, public boat//amphibious vehicle, multi-inheritance embodiment {public:amphibiancar (int weight, int aird, float tonnage): Veh Icle (weight), Car (weight, Aird), Boat (weight, tonnage)//Multiple inheritance to pay attention to calling the baseClass Constructor {cout << "load Amphibiancar class constructor" << Endl;} void ShowMe () {cout << "I'm an amphibious car!" "<< Endl;} void Showmembers () {cout << "weight:" << weight << "Dayton," << "Air Displacement:" << Aird << "CC," <&lt ; "Displacement:" << tonnage << "Dayton" << Endl;}}; int main () {Amphibiancar A (4, 1.35f); A.showme (); A.showmembers (); A.setweight (3); a.showmembers (); System ("Pause") ;}
Output:


Although there are some similarities between virtual inheritance and imaginary functions, it is important to remember that there is absolutely no connection between them!

*************************************************************************************************************** *********

*************************************************************************************************************** *********

Summarize some key points and considerations for multiple inheritance:

    • In the case of multiple inheritance, the likelihood of ambiguity will be greater, and the compiler will not attempt to convert between different base classes based on derived classes, converting to each base class as well as the following code:

Class Zooanimal{};class Bear:public Zooanimal{};class endangered{};class panda:public Bear, public Endangered{};
If there are two overloaded versions of the print function:
void print (const bear&); void print (const endangered&);

This call will go wrong, and the compiler will point out that there is a two justification for getting rid of it.


    • Assuming that the same member is defined in multiple base classes of a class, it will result in two semantics, and if a member function with the same name is defined, even if the function parameter is different, it will result in two semantics, the code is as follows:

Class Zooanimal{};class bear:public zooanimal{public:void print (int x) {cout<< "print" <<endl;}}; Class Endangered{public:void print () {cout<< "print" <<endl;};}; Class Panda:public bear, public endangered{};
If print is defined in zooanimal and there is no definition in the bear, there is also a two ambiguity. The best way to avoid ambiguity is to specify the scope of the function, such as:
Bear::p rint (x);


The end!



Multiple inheritance of C + +

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.