C ++. QT learning notes -- how to call the constructor of the parent class
The derived subclass inherits all the members of the base class, except the constructor and destructor. That is to say, sub-classes cannot inherit the constructor and destructor of the parent class. therefore, for member variables inherited from the parent class, if you do not want to write your own constructor initialization, you can only initialize the member variables in the parent class before inheriting them. In the following procedure:
The constructor in the student1 subclass only initializes the newly added int age and string ADDR members. There is no initialization statement for the member num, name, and sex inherited from the parent class, however, the subclass cannot inherit the constructor of the parent class, or you can write another initialization statement. This operation can cause repetitive statements. In this case, student1 (int n, string Nam, char S, int A, string AD): Student (n, NAM, S) {age = A; ADDR = aD;} initialize the parent class member variable first, in this way, the member variables inherited from the parent class are indirectly initialized.
# Include <iostream>
# Include <string>
Using namespace STD;
Class student
{
Public:
Student (int n, string Nam, char S)
{
Num = N;
Name = Nam;
Sex = s;
}
~ Student (){}
Protected:
Int num;
String name;
Char sex;
};
Class student1: Public student
{
Student1 (int n, string Nam, char S, int A, string AD): Student (n, NAM, S)
{
Age =;
ADDR = aD;
}
Void show ()
{
}
PRIVATE:
Int age;
String ADDR;
};
Int main ()
{
Student1 stud1 (10010, "Wang", 'F', 19, "Beijing Road, Shanghai ");
......
......
}
The above is the definition of the base class student and the sub-class student1.
Note the first line of the derived subclass constructor:
Student1 (int n, string Nam, char S, int A, string AD): Student (n, NAM, S)
The general form is
Derived class constructor name (total parameter list): base class constructor name (parameter list)
{
New data member initialization statement in the derived class;
}
The front part of the colon is the backbone of the derived class constructor. Its total parameter list includes the parameters required for the base class constructor and the parameters required for initializing the new data members of the derived class. The part after the colon is the base class constructor to be called and its parameters.
There are only parameters but no parameter types in the parameter table column following the base class constructor, because the base class constructor is called instead of defining the base class constructor, therefore, these parameters are real parameters rather than form parameters. They can be constants, global variables, and parameters in the aggregate parameter table of the derived class constructor.
As you can see from the above: in the main function, when the object stud1 is created, there are five parameters, the first three of which are used to pass the base class constructor, the last two are used to initialize the data members added to the derived subclass.
In the preceding example, the constructor of the derived class can be defined outside the class,
Student1: student1 (int n, string Nam, char S, int A, string AD): Student (n, NAM, S)
{
Age =;
ADDR = aD;
}
Note: It is not required to declare the derived subclass constructor in the class: Student (n, NAM, S), which is used only by the definition.
In the above example, the real parameters used to call the base class constructor are obtained from the total parameter table of the subclass constructor. You can also directly use constants or global variables without passing them. For example:
Student1 (string Nam, char S, int A, string AD): Student (10010, NAM, S)
In this way, the N of the parent class is initialized to 10010 by default, and the subclass directly inherits n = 10010, so no write is required.