In the previous example, the derived class had only one base class, called single inheritance. In addition, C + + supports multiple inheritance, meaning that a derived class can have two or more base classes.
Multiple inheritance easy to make code logic complex, thinking confusion, has been controversial, small and medium-sized projects are less used, and later Java, C #, PHP, and so simply cancel the multiple inheritance. Readers who want to learn C + + quickly do not have to read it carefully.
The syntax for multiple inheritance is also simple, separating multiple base classes with commas. For example, Class A, Class B, and Class C have been declared, so you can declare a derived class D:
class Public Private protected c{ // Class D newly added member }
D is a derived class of multiple inheritance that inherits Class A in a common way, inheriting class B in a private way, inheriting class C in a protected manner. D Gets the members in a, B, and C according to different inheritance methods, and determines the access rights of the members of each base class in the derived class.
Constructors under multiple inheritance
A constructor for a multiple-inheritance derived class is basically the same as a single-inheritance class, except that it contains more than one base class constructor. Such as:
Class D constructor name (total parameter table column): A constructor (argument table column), Class B Constructor (argument table column), Class C Constructor (argument table column) { new member initialization statement}
Each base class is arranged in any order.
The constructor of the derived class is executed in the same order: the constructor of the base class is called first, and then the derived class constructor is called. The order in which the base class constructors are called is the order in which the base class appears when the derived class is declared.
The following defines two base classes, the Basea class and the Baseb class, and then derives the sub class in a multi-inheritance manner.
#include <iostream>using namespacestd;//base classclassbasea{protected: intA; intb; Public: Basea (int,int);}; Basea::basea (intAintb): A (a), B (b) {}//base classclassbaseb{protected: intC; intD; Public: Baseb (int,int);}; Baseb::baseb (intCintd): C (c), D (d) {}//Derived ClassesclassSub: PublicBasea, Publicbaseb{Private: inte; Public: Sub (int,int,int,int,int); voiddisplay ();}; Sub::sub (intAintBintCintDinte): Basea (A, B), Baseb (c, D), E (e) {}voidSub::d isplay () {cout<<"a="<<a<<Endl; cout<<"b="<<b<<Endl; cout<<"c="<<c<<Endl; cout<<"d="<<d<<Endl; cout<<"e="<<e<<Endl;}intMain () {(NewSub (1,2,3,4,5)),display (); return 0;}
Member variables inherited from base classes Basea and Baseb can be accessed in Sub::d isplay ().
Naming conflicts
When a member of the same name exists in two base classes, a naming conflict is generated, and the member cannot be accessed directly, with the class name and the domain resolver required.
If there is a member function display () in both the base class Basea and the Baseb, then the following statement is incorrect:
Sub Obj;obj.display ();
Since both Basea and BASEB have display (), the system will not be able to determine which class to call the function, so the error.
You should add the class name and the domain resolver as follows:
Sub Obj;obj. Basea::d isplay (); obj. BASEB::d isplay ();
By this example, you can see that in multiple inheritance, some duplicate data is inherited from different base classes. If you have more than one base class, the problem is more prominent, so when designing a derived class, consider its data members carefully, minimizing data redundancy.
Multiple inheritance for C + + learning class 19