Parsing the constructors of multiple-layered derivations in C + + and some special forms of _c languages

Source: Internet
Author: User
Tags constructor

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.

Special forms of C + + derived class constructors
When you use a derived class constructor, you have the following special form.

1 When you do not need to perform any initialization operations on the new members of the derived class, the function body of a derived class constructor can be empty, that is, the constructor is an empty function, and when the function body is empty, the number of arguments for the derived class constructor equals the sum of the number of parameters of the base class constructor and child objects. All parameters of a derived class constructor are passed to the base class constructor and child objects and are not initialized to the derived class's data members when the derived class constructor is invoked. This derived class constructor functions only to pass parameters to the base class constructors and child objects, and to call the base class constructor and the child object constructor when the derived class constructor is executed. This usage is common in practical work.

2 If you do not define a constructor in the base class, or if you define a constructor with no parameters, the base class constructor is not written when you define a derived class constructor. Because the derived class constructor does not have a task that passes parameters to the base class constructor. When a derived class constructor is invoked, the system automatically calls the default constructor of the base class first.

You do not have to explicitly define a derived class constructor if you do not define a constructor with parameters in the declaration of the base class and child object type, and you do not have to initialize the data members of your derived class. Because the derived class constructor does not have either a task to pass parameters to the base class constructor and the child object constructor, nor does it have a task initialized to the derived class data member.

When a derived class object is established, the system automatically invokes the default constructor of the system-supplied derived class and, during the execution of the derived class default constructor, invokes the default constructor of the base class and the default constructor for the child object type.

If you define a constructor with parameters in a declaration of a base class or a child object type, you must explicitly define the derived class constructor and write the constructor of the base class or child object type and its parameter table in the derived class constructor.

If you define both a parameterless constructor in a base class and a parameterized constructor (constructor overload), you can include both the base class constructor and its arguments, or not the base class constructor, when you define a derived class constructor.

When a derived class constructor is invoked, it is determined by the contents of the constructor to invoke the parameter constructor of the base class or the parameterless constructor. Programmers can decide which method to use based on the needs of the derived class.

Related Article

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.