In the past, we have always been engaged in the inheritance of a single class, but in real life, some new things often have the attributes of two or more things. To solve this problem, C ++ introduces the concept of multi-inheritance,C ++ allows you to specify multiple base classes for a derived class. This inheritance structure is called multi-inheritance..
For example, a vehicle and a ship can be derived from a subclass. However, a vehicle and a ship share the same characteristics as an amphibious vehicle and must inherit the common attributes of a vehicle and a ship.
It is not difficult to come up with the following legends and code:
To use multiple inheritance for a derived class, you must list the class names of all the base classes after the derived class name and colon, and use them to separate them.
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
Usingnamespacestd;
ClassVehicle
{
Public:
Vehicle (intweight = 0)
{
Vehicle: weight = weight;
}
VoidSetWeight (intweight)
{
Cout <"resetting weight" < Vehicle: weight = weight;
}
VirtualvoidShowMe () = 0;
Protected:
Intweight;
};
ClassCar: publicVehicle // car
{
Public:
Car (intweight = 0, intaird = 0): Vehicle (weight)
{
Car: aird = aird;
}
VoidShowMe ()
{
Cout <"I'm a car! "< }
Protected:
Intaird;
};
ClassBoat: publicVehicle // ship
{
Public:
Boat (intweight = 0, floattonnage = 0): Vehicle (weight)
{
Boat: tonnage = tonnage;
}
VoidShowMe ()
{
Cout <"I'm a ship! "< }
Protected:
Floattonnage;
};
ClassAmphibianCar: publicCar, publicBoat // dual-purpose vehicle, multiple inheritance embodiment
{
Public:
AmphibianCar (intweight, intaird, floattonnage)
: Vehicle (weight), Car (weight, aird), Boat (weight, tonnage)
// Pay attention to calling the base class constructor for multiple inheritance
{
}
VoidShowMe ()
{
Cout <"I'm a dual-purpose vehicle! "< }
};
Intmain ()
{
AmphibianCar a (4,200, 1.35f); // Error
A. SetWeight (3); // Error
System ("pause ");
}
On the surface, the above Code does not show obvious syntax errors, but it cannot be compiled. Why?
This is caused by the ambiguity of inheritance caused by multiple inheritance.
First look at the figure below:
In the figure, the primary problem lies in the deep red mark. The amphibious vehicle class inherits the attributes and methods from the Car class and the Boat class, and the Car class and the Boat class are the same as the base class of the AmphibianCar class, on the memory allocation, AmphibianCar obtains the SetWeight () member function from two classes. When we call. when SetWeight (3) is used, the computer does not know how to select the repeated class member function SetWeight () that belongs to two base classes ().
Because of this fuzzy problem, AmphibianCar a (4,200, 1.35f) is also caused. If the execution fails, the system will generate a Vehicle "not a base or member error.
The above code is used as an example. To make the AmphibianCar class get a copy of Vehicle, in addition, data members and member functions of the Car and Boat classes must be shared throughVirtual inheritanceTechnology.
We inherit the Vehicle class from the Car class and Boat class, and add the virtual keyword before it to implement virtual inheritance. After using virtual inheritance, when the system encounters multiple inheritance, it will automatically add a copy of Vehicle. When a copy of Vehicle is requested again, it will be ignored to ensure the uniqueness of the inherited class member functions.
The modified code is as follows. Observe the changes:
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
Usingnamespacestd;
ClassVehicle
{
Public:
Vehicle (intweight = 0)
{
Vehicle: weight = weight;
Cout <"loading Vehicle class constructor" < }
VoidSetWeight (intweight)
{
Cout <"resetting weight" < Vehicle: weight = weight;
}
VirtualvoidShowMe () = 0;
Protected:
Intweight;
};
ClassCar: virtualpublicVehicle // car, where virtual inheritance
{
Public:
Car (intweight = 0, intaird = 0): Vehicle (weight)
{
Car: aird = aird;
Cout <"loading Car class constructor" < }
VoidShowMe ()
{
Cout <"I'm a car! "< }
Protected:
Intaird;
};
ClassBoat: virtualpublicVehicle // ship, which is a virtual inheritance
{
Public:
Boat (intweight = 0, floattonnage = 0): Vehicle (weight)
{
Boat: tonnage = tonnage;
Cout <"loading Boat class constructor" < }
VoidShowMe ()
{
Cout <"I'm a ship! "< }
Protected:
Floattonnage;
};
ClassAmphibianCar: publicCar, publicBoat // dual-purpose vehicle, multiple inheritance embodiment
{
Public:
AmphibianCar (intweight, intaird, floattonnage)
: Vehicle (weight), Car (weight, aird), Boat (weight, tonnage)
// Pay attention to calling the base class constructor for multiple inheritance
{
Cout <"loading AmphibianCar class constructor" < }
VoidShowMe ()
{
Cout <"I'm a dual-purpose vehicle! "< }
VoidShowMembers ()
{
Cout <"Weight:" < };
Intmain ()
{
AmphibianCar a (4,200, 1.35f );
A. ShowMe ();
A. ShowMembers ();
A. SetWeight (3 );
A