The constructor of the "C + + Learning path" derived class (iii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.