C + + multi-tier derivation constructors
Not only can a class derive a derived class, a derived class can also continue to derive from it, forming a derived hierarchy. Based on the above description, it is not difficult to write out the constructors of derived classes in multilevel derivation.
The following procedure allows readers to understand how to define a constructor for a derived class in a multilevel derivation. I believe everyone can understand this procedure.
[Example] The constructor of a derived class from a multilevel derivation.
#include <iostream> #include <string> using namespace std;
Class student//declares the base class {public://common part Student (int n, string nam)//base class constructor {num=n;
Name=nam;
void display ()/Output base class data member {cout<< "num:" <<num<<endl;
cout<< "Name:" <<name<<endl;
The protected://protection part int num;//base class has two data member string name;
}; Class Student1:public student//declare public derived class Student1 {public:student1 (int n,char nam[10],int a): Student (N,nam)//derived class constructor { Age=a;} This only initializes void show ()//output Num,name and age {display () to the new data member of the derived class (),/output num and name cout<< "Age:" <<age<
<endl; private://the private data int age of the derived class;
Add a data member}; Class Student2:public Student1//Declaration indirect public derived class Student2 {public://Below is an indirect derived class constructor Student2 (int n, string nam,int a,int s): Stude
Nt1 (n,nam,a) {score=s;} void Show_all ()//output all data members {show ();//output num and name cout<< "Score:" <<score<<endl; Output age} Private:int score;
Add a data member}; int main () {Student2 sTud (10010, "Li", 17,89); Stud.show_all ();
Output all data of student return 0;
}
The output at run time is as follows:
num:10010
name:li
age:17
score:89
Notice how the constructor for the base class and two derived classes are written.
The first part of the base class's construction function:
Student (int n, string nam)
The first part of the constructor of the derived class Student1:
Student1 (int n, String nam],int a): Student (N,nam)
The first part of the constructor of the derived class Student2:
Student2 (int n, string nam,int a,int s): Student1 (N,nam,a)
Be careful not to write:
Student2 (int n, string nam,int a,int s): Student1 (N,nam), Student1 (n, Nam, a)
Instead of listing the constructors for each layer of derived classes, simply write out the constructor of the derived class (that is, its direct base class) on the previous layer. When declaring a Student2 class object, call the Student2 constructor, call the Student1 constructor when the Student2 constructor is executed, and invoke the base class Student1 constructor when the student constructor is executed. The order of initialization is:
Initializes the data member Num and name of the base class.
Initializes the STUDENT1 data member age.
Finally, the Student2 data member score is initialized.
Access properties for C + + class-Multilevel derivations
In actual project development, there are often multilevel derivations. The derivation relationship as shown in Figure 11.9: Class A is the base class, and Class B is a derived class of Class A. Class C is a derived class of Class B, and Class C is also a derived class of Class A; Class B is called a direct derived class of Class A, Class C is called an indirect derived class of Class A, and Class A is the direct base class of Class B, the indirect base class of Class C.
In the case of multilevel derivation, the access properties of each member are determined by the above principle.
In order to make the multiple inheritance more detailed, please look at the following several inherited classes.
[Example] if the following classes are declared:
Class A//base class
{public
:
int i;
Protected:
void F2 ();
Int J;
Private:
int k;
};
Class B:public A//public mode
{public
:
void F3 ();
Protected:
void F4 ();
Private:
int m;
};
Class c:protected B//protected mode
{public
:
void F5 ();
Private:
int n;
};
Class A is the common base class for Class B, and Class B is the protection base class for Class C. The access properties of each member in different classes are as follows:
Based on the above analysis, the member function F5 of Class C can only be accessed outside of derived class C, and no other members can be accessed. member functions of derived Class C F5 can access members of the base class A, F2, J, and member of derived Class B F3, F4. member functions of derived Class B F3, F4 can access the members I, F2, and J of base Class A.
From the above analysis, you can see that no matter which inheritance method cannot access a private member of a base class in a derived class, a private member can only be accessed by a member function of this class, after all, the derived class is not the same class as the base class.
If you use common inheritance when you derive from multiple levels, the derived class will have access to the public and protected members of the base class until the last level.
If you are using private inheritance, after several derivations, all members of the base class have become inaccessible.
If you are using protected inheritance, you cannot access any members of a derived class outside of a derived class.
And after several derivations, it is difficult to remember clearly which members are accessible and which members are inaccessible, making it easy to make mistakes. Therefore, in practice, common inheritance is commonly used.