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 vehicle and a ship. However, a vehicle and a ship have 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.
# 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;
Protected:
Int weight;
};
Class car: public vehicle // car
{
Public:
Car (INT Weight = 0, int AIRD = 0): Vehicle (weight)
{
Car: AIRD = AIRD;
}
Void showme ()
{
Cout <"I'm a car! "<Endl;
}
Protected:
Int AIRD;
};
Class boat: public vehicle // boat
{
Public:
Boat (INT Weight = 0, float Tonnage = 0): Vehicle (weight)
{
Boat: Tonnage = tonnage;
}
Void showme ()
{
Cout <"I'm a ship! "<Endl;
}
Protected:
Float tonnage;
};
Class amphibiancar: Public car, public boat // dual-purpose vehicle, multiple inheritance embodiment
{
Public:
Amphibiancar (INT weight, int AIRD, float tonnage)
: Vehicle (weight), car (weight, AIRD), boat (weight, tonnage)
// Pay attention to calling the base class constructor for multiple inheritance
{
}
Void showme ()
{
Cout <"I'm a dual-purpose vehicle! "<Endl;
}
};
Int main ()
{
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:
# Include <iostream>
Using namespace STD;
Class Vehicle
{
Public:
Vehicle (INT Weight = 0)
{
Vehicle: Weight = weight;
Cout <"loading vehicle class constructor" <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, where virtual inheritance
{
Public:
Car (INT Weight = 0, int AIRD = 0): Vehicle (weight)
{
Car: AIRD = AIRD;
Cout <"loading car class constructor" <Endl;
}
Void showme ()
{
Cout <"I'm a car! "<Endl;
}
Protected:
Int AIRD;
};
Class boat: virtual public vehicle // ship, where virtual inheritance
{
Public:
Boat (INT Weight = 0, float Tonnage = 0): Vehicle (weight)
{
Boat: Tonnage = tonnage;
Cout <"load boat class constructor" <Endl;
}
Void showme ()
{
Cout <"I'm a ship! "<Endl;
}
Protected:
Float tonnage;
};
Class amphibiancar: Public car, public boat // dual-purpose vehicle, multiple inheritance embodiment
{
Public:
Amphibiancar (INT weight, int AIRD, float tonnage)
: Vehicle (weight), car (weight, AIRD), boat (weight, tonnage)
// Pay attention to calling the base class constructor for multiple inheritance
{
Cout <"loading amphibiancar class constructor" <Endl;
}
Void showme ()
{
Cout <"I'm a dual-purpose vehicle! "<Endl;
}
Void showmembers ()
{
Cout <"Weight:" <weight <"pause," <"Air Displacement:" <AIRD <"cc," <"displacement: "<tonnage <" Don "<Endl;
}
};
Int main ()
{
Amphibiancar A (4,200, 1.35f );
A. showme ();
A. showmembers ();
A. setweight (3 );
A. showmembers ();
System ("pause ");
}
Observe the construction sequence of the class constructor.
Although Virtual inheritance is similar to virtual functions, readers must remember that there is absolutely no connection between them!