Third, multi-layer inheritance of derived classes
1. a derived class of a multilayer secondary can simply write the constructor of the direct base class in the constructor's initialization list
1 class student 2 {3 Public:4 student (int n, string nam) 5 { 6 num = n; name =
Nam;
7
}
8 };9 classStudent1: PublicStudentTen { One Public: A student1 (int n, string nam, int a): Student (n, nam) {age = A;} - }; - classStudent2: PublicStudent1 the { - Public: - student2 (int n, string nam, int A, int s): Student1 (n, Nam, a) {score = s;} -};
Base class: Student (int n, string nam)
First-Class derivation: student1 (int n, string nam, int a): Student (N,nam)
Secondary derivation: student2 (int n, string nam, int a, int s): Student1 (N,nam,a)
2. Attention!! Do not write:
Student2 (int n, string nam, int a, int s): Student (n, NAM), Student1 (n, Nam, a)//error!!!
Iv. summary of constructors of derived classes
1.student (all formal parameter lists are written here ): What can be written here is only (1) the base class constructor (2) An initialization list of inline objects (3) The new data member of the class { What can be written in a function body there are (1) assignments of data in the base class that can be accessed in a derived class (2) Assignment of new data members to derived classes (3) Other statements to add}
2. The order of execution in the initialization list is (1) the direct base class constructor (the system automatically calls an parameterless constructor if it is not explicitly written out) (2) The constructor of the class that corresponds to the inline object (which depends on the argument form of the object) (3) Other statements
3. Essentially, the function of the constructor is to initialize the data members in the class, but there are many ways to achieve the same purpose, such as I can write a function in the class, assign a value to the data member, but the simplest and most reliable way is to call the constructor, All constructors are ultimately initialized with all data members. (What about data members that are not accessible after they have been inherited?) )
The constructor of the "C + + Learning path" derived class (iii)