1. multiple inheritance in C + +
(1) A subclass can have multiple parent classes
(2) Subclasses have member variables for all parent classes
(3) Subclass inherits member functions from all parent classes
(4) A subclass object can be used as any parent class object
(5) syntax rules for multiple inheritance
Class Derived: Public Basea, public baseb, public basec{...};
2. Multiple succession issues one
(1) objects obtained through multiple inheritance can have "different addresses"!!!
(2) explanation scheme: None
(3) Cause analysis
"Programming Experiment" multiple inheritance Problem one
#include <iostream>using namespacestd;classbasea{intMa; Public: Basea (inta) {ma=A; } intGeta () {returnMa; }};classbaseb{intMB; Public: Baseb (intb) {MB=b; } intGetb () {returnMB; }};//Multiple InheritanceclassDerived: PublicBasea, Publicbaseb{intMC; Public: Derived (intAintBintc): Basea (a), Baseb (b) {MC=C; } intGetC () {returnMC; } voidprint () {cout<<"ma ="<< Geta () <<", "<<"MB ="<< Getb () <<", "<<"MC ="<< MC <<Endl; }};intMain () {cout<<"sizeof (Derived) ="<<sizeof(Derived) << Endl;// ADerived D (1,2,3); D.print (); cout<<"D.geta () ="<< D.geta () << Endl;//1cout <<"D.getb () ="<< D.getb () << Endl;//2cout <<"d.getc () ="<< d.getc () << Endl;//3cout<<Endl; Basea* PA = &D; Baseb* PB = &d;//Note that the above two lines are assigned the address of object D to a pointer//the surface looks like the PA pointer should be equal to the PB pointer, but it doesn't actually//the PA points to the Basea sub-object in the D object, and PB points to D//Baseb the part of a child object in an object. cout <<"PA ="<< PA << Endl;//0x23fe9ccout <<"PB ="<< PB << Endl;//0x23fea0 void* PAA =PA; void* PBB =PB; if(PAA = =PBB) {cout<<"Point to the same object!"<<Endl; } Else{cout<<"Error"<< Endl;//the line is output! } cout<<"paa ="<< paa << Endl;//0x23fe9ccout <<"PBB ="<< PBB << Endl;//0x23fea0 return 0;}
3. Multiple Succession Issues II: Members that may generate redundancy
(1) The problem of data redundancy arises when multiple inheritance relationships are closed!!!
(2) Solution: Virtual Inheritance
① virtual inheritance can solve data redundancy problem
② Middle-tier parent class no longer cares about initialization of the top-level parent class
③ The final subclass must call the constructor of the top-level parent class directly
(3) There is a problem: when inheritance is required in the schema design, it is not possible to determine whether to use direct or virtual inheritance !!
"Programming Experiment" multiple inheritance problem two
#include <iostream>#include<string>using namespacestd;classpeople{stringMname; intMAge; Public: People (stringNameintAge ) {Mname=name; MAge=Age ; } voidprint () {cout<<"Name ="<< Mname <<", "<<"Age ="<< MAge <<Endl; }};//Intermediate class with virtual inheritanceclassTeacher:Virtual Publicpeople{ Public: Teacher (stringNameintAge ): People (name, age) {}};//Intermediate class with virtual inheritanceclassStudent:Virtual Publicpeople{ Public: Student (stringNameintAge ): People (name, age) {}};//PhD (a doctor may be a teacher, another student), using direct succession//if the teacher and student of the middle tier do not use virtual inheritance, then the Doctor//The class will have two copies of the member variables from the people class, such as Mname, Mage, and the data//redundancy, and in sub-class doctor, if direct Mname = 1, there will be two semantic errors//because the compiler does not know whether Mname is from the teacher class or the student class.classDoctor: PublicTeacher, Publicstudent{ Public: //Notice that the last initialization in the constructor is people, that is, with virtual inheritance,//The final child still needs to call the constructor of the top-level parent class, which is also a big problem for virtual inheritance, because//in practical development, it is sometimes difficult to determine the top-most base classDoctor (stringNameintAge ): Teacher (Name,age), Student (Name,age), people (Name,age) {}};intMain () {Doctor D ("Santaclaus", -); D.print (); return 0;}
4. multiple inheritance may produce more than one virtual function table
"Programming Experiment" multiple inheritance issues three
(1) When a virtual function table exists in a class, it is recommended to use modern type conversion keywords in C + + if forced type conversions are required!!!
(2) Solution: dynamic_cast
5. Correct use of multiple inheritance
(1) "Multiple inheritance" mode in engineering development: single inheritance + multi-interface .
(2) Some useful engineering advice:
① inherits from a parent class, then implements multiple interfaces
The ② parent class provides a equal () member function that determines whether the pointer points to the current object
③ forced type conversions related to multiple inheritance are completed with dynamic_cast
The correct multi-inheritance mode of "programming experiment"
6. Summary
(1) C + + supports multiple inheritance programming methods
(2) Multiple inheritance is prone to problems, such as "the same object's address is different","Data redundancy" problem and so on.
(3) multiple virtual function table pointers may appear in multiple inheritance
(4) forced type conversions related to multiple inheritance are completed with dynamic_cast
(5) Using multiple inheritance in the way of " single inheritance + multi-interface " in engineering development
(6) The parent class provides a member function to determine whether the pointer points to the current object
The multiple inheritance of the abandonment of lesson 54th (bottom)