1. parent class and Child class 1. Mutual conversion between parent class and Child class
The object of the derived class can be assigned to the Base class, but not vice versa.
The pointer of the base class can point to the derived class, but not vice versa.
The base class can be initialized as the object of the derived class.
The pointer of a derived class must be forcibly converted to the base class pointer before it can be directed to the base class.
Converting a base class pointer to a derived class pointer may cause collapse errors.
The reference or derivation of a virtual base class cannot be converted to a derived class.
Class father {}; class son: Public father {}; int main () {father f; son s; F = s; // correct S = F; // error father * pF = new son; // correct son * PS = new father; // error father & Rf = s; // correct father & rs = F; // error return 0 ;}
2. Impact of inheritance relationships on base class members
|
Public Member |
Protect members |
Private member |
Public inheritance |
Public |
Protection |
Inaccessible |
Protection inheritance |
Protection |
Protection |
Inaccessible |
Private inheritance |
Private |
Private |
Inaccessible |
Member Functions |
1 |
1 |
1 |
Object |
1 |
0 |
0 |
Subclass |
1 |
1 |
0 |
1: accessible. 0: inaccessible.
When all the members become inaccessible, it does not make sense to derive further. 2. Subclass Construction and Analysis 1. when constructing a derived class object, first execute the constructor of the base class, and then execute the constructor of the subclass.
class father{public:father(){cout<<"father construct"<<endl;}~father(){cout<<"father delete"<<endl;}};class son : public father{public:son(){cout<<"son construct"<<endl;}~son(){cout<<"son delete"<<endl;}};int main(){son s;return 0;}
Output:
Father construct
Son construct
Son Delete
Father Delete
2. For multi-inheritance, the construction order of the base class is in the given order.
class father{public:father(){cout<<"father construct"<<endl;}~father(){cout<<"father delete"<<endl;}};class mother{public:mother(){cout<<"mother construct"<<endl;}~mother(){cout<<"mother delete"<<endl;}};class son : public father, public mother{public:son(){cout<<"son construct"<<endl;}~son(){cout<<"son delete"<<endl;}};int main(){son s;return 0;}
Output:
Father construct
Mother construct
Son construct
Son Delete
Mother Delete
Father Delete
3. Construct subclass using constructors of base classes, which is more efficient
class father{int x;public:father(int a):x(a){cout<<"father construct:"<<x<<endl;}};class son : public father{int y;public:son(int a, int b):father(a), y(b){cout<<"son construct:"<<y<<endl;}};int main(){son s(1, 2);return 0;}
Output:
Father construct: 1
Son construct: 2
Iii. Multi-Inheritance
1. The root cause of the ambiguity of multiple continuation is
If a has test (), B and c both have test (), so d produces the ambiguity.
Class A {public: void test () {cout <"A" <Endl ;}}; Class B {public: void test () {cout <"B" <Endl ;}}; Class C: Public A, public B {}; int main () {C; C. test (); // error C. a: Test (); // correct. Output: AC. b: Test (); // correct. Output: breturn 0 ;}
2. compilers usually search from the directory tree closest to themselves.
The test () of the subclass overwrites the test () of the base class. It does not mean that the test () of the base class disappears, but cannot be accessed directly.
Class A {public: void test () {cout <"A" <Endl ;}}; Class B {public: void test () {cout <"B" <Endl ;}}; Class C: Public A, public B {void test () {cout <"C" <Endl ;}}; int main () {C; C. test (); // correct, output: CC. a: Test (); // correct. Output: AC. b: Test (); // correct. Output: breturn 0 ;}
3. For a single inheritance, whether the subclass can access the parent class of the parent class depends only on the Inheritance Method.
For multi-inheritance, child classes cannot directly access the parent class of the parent class.
4. Use virtual to avoid ambiguity.
Class B: virtual public.
Iv. Inheritance and inclusion 1. The member variable list of a class contains the object of another class, which is called inclusion (inclusive ). 2. Differences between private and private inheritance:
Includes:
1) make the program look clearer and easier to understand
2) There is no Inheritance Problem
3) It can include multiple objects of another class.
Private inheritance:
1) access the protection members of the base class
2) You can redefine virtual functions to achieve polymorphism.