Resolves the constructor and some special forms for multiple levels of derivation in C++

  • 2020-05-05 11:35:23
  • OfStack

Constructor
++ when multiple levels are derived Not only can a class derive a derived class, derived classes can continue to derive, forming a derived hierarchy. On the basis of the above, it is not difficult to write the constructor of the derived class in the case of multi-level derivation.

The following program shows you how to define a constructor for a derived class in a multilevel derivation situation. I believe that you can fully understand this program.

Constructor of a derived class in the case of multiple levels of derivation.


#include <iostream>
#include<string>
using namespace std;
class Student// Statement of the base class 
{
public:// Common parts 
  Student(int n, string nam)// Base class constructor 
  {
   num=n;
   name=nam;
  }
  void display( )// Output base class data members 
  {
   cout<<"num:"<<num<<endl;
   cout<<"name:"<<name<<endl;
  }
protected:// Protect part 
  int num;// The base class has two data members 
  string name;
};
class Student1: public Student// Declare a common derived class Student1
{
public:
  Student1(int n,char nam[10],int a):Student(n,nam)// Derived class constructor 
  {age=a;}// Only the new data members of the derived class are initialized here 
  void show( ) // The output num . name and age
  {
   display( ); // The output num and name
   cout<<"age: "<<age<<endl;
  }
private:// Private data of a derived class 
  int age; // Add a data member 
};
class Student2:public Student1 // Declares an indirect common derived class Student2
{
public:// The following is the indirectly derived class constructor 
  Student2(int n, string nam,int a,int s):Student1(n,nam,a) {score=s;}
  void show_all( ) // Output all data members 
  {
   show( ); // The output num and name
   cout<<"score:"<<score<<endl; // The output age
  }
private:
  int score; // Add a data member 
};
int main( )
{
  Student2 stud(10010,"Li",17,89);
  stud.show_all( ); // Output all the data of the students 
  return 0;
}

The run-time output is as follows:


num:10010
name:Li
age:17
score:89

Notice how the constructors for the base class and the two derived classes are written.

Constructor header of the base class:


  Student(int n, string nam)


Constructor header for the derived class Student1:


  Student1(int n, string nam],int a):Student(n,nam)


Constructor header for 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, just write out the constructors for the layer above them (that is, its immediate base class). When declaring an Student2 class object, the Student2 constructor is called. When the Student2 constructor is executed, the Student1 constructor is first called. When the Student1 constructor is executed, the base class Student constructor is called first. The order of initialization is:
The data members num and name of the base class are initialized first.
The data member age of Student1 is then initialized.
Finally, the data member score of Student2 is initialized.

C++ special form of the derived class constructor
When using derived class constructors, there are the following special forms.

1) when I do not need any initialization the new members of the derived class, the derived class constructor function body can is empty, that is, the constructor is an empty function, the function body is empty, the number of the parameters of the derived class constructor is equal to the base class constructor and the number of the parameters of the object, the sum of all the parameters are passed to the derived class constructor is the base class constructor and the child object, when invoking the derived class constructor does not initialize the data members of the derived class. The purpose of this derived class constructor is simply to pass arguments to the base class constructor and child objects, and to call the base class constructor and child object constructor when the derived class constructor is executed. This is a common usage in practice.

2) if a constructor is not defined in the base class, or if a constructor is defined without arguments, the base class constructor is not written when defining a derived class constructor. This is because the derived class constructor has no task to pass arguments to the base class constructor. When the derived class constructor is called, 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 arguments in the declarations of the base class and child object types, and you do not need to initialize the data members of the derived class itself. This is because the derived class constructor has neither the task of passing arguments to the base class constructor and the child object constructor, nor the task of initializing the derived class data member.

When a derived class object is created, the system automatically calls the default constructor of the derived class provided by the system, and during the execution of the default constructor of the derived class, the default constructor of the base class and the default constructor of the subobject type are called.

If a constructor with an argument is defined in the declaration of a base class or subobject type, the derived class constructor must be explicitly defined and the constructor of the base class or subobject type and its argument table must be written in the derived class constructor.

If you define both a parameter-free constructor and a parameter-free constructor (constructor overloading) in a base class, you can define a derived class constructor with or without a base class constructor.

When a derived class constructor is called, the argument or no-argument constructor of the base class is called based on the content of the constructor. The programmer can decide which approach to take based on the needs of the derived classes.


Related articles: