Introduction:
Inheritance saves us a lot of time and work, but because subclasses are derived from base classes, we still need to initialize the base class when we create objects of derived classes. The reason is not difficult to understand, because the subclass inherits all the members of the base class, so that the inherited member also appears in the subclass, so we will inevitably invoke the constructor of the base class when we construct the object of a subclass.
When you create a constructor for a derived class, there are two ways to initialize the data.
The first is to create a constructor in a derived class, and then initialize the data (data that inherits from the base class), as well as the subclasses. This method is obviously superfluous because the base class already has the proper constructor, and it is not advisable to initialize the base class in a derived class.
Second: Create a constructor in a derived class, and then use that constructor to call the constructor of the base class and pass the initial value to the constructor.
Obviously, the second method is more efficient and less likely to make mistakes.
Here we use an example to explain:
The first approach is to assign values to the data members of the subclass object in the constructor of the subclass.
Code 1:
#include <iostream>
#include <string>
using namespace Std;
Class Father
{
Public
Father ()
{
cout<< "? Construct the base class object \ n";
}
~father ()
{
cout<< "destructor base class object \ n";
}
void print ()
{
cout<<name<< "Height:" <
}
Protected
String name;
int height;
};
Class Son:public Father
{
Public
Son (String A,int i,int J)//The first argument assigns a value to the name, the second parameter to the height assignment, and the third parameter to the weight assignment, which is the data member of the subclass object created by the constructor of the subclass
{
name=a;//the data member inherited from the parent class object
height=i;//the data member inherited from the parent class object
weight=j;//the data member added in the subclass
cout<< "construct subclass object \ n";
}
~son ()
{
cout<< "destructor Subclass object \ n";
}
void Print1 ()
{
Print (),//Call subclass inherits from the parent class's print function, that is, the child class's print function, except that the print function inherits from the parent class
cout<<name<< "Weight::" <<weight<< "kg" <<endl;
}
Protected
int weight;
};
int main ()
{
Son Mary ("Mary", 160,50);
Mary.print1 ();
return 0;
}
The disadvantage of this method is that when invoking the constructor of the parent class, there is nothing to do with creating the subclass object, the invocation of the parent class constructor is not useless, and the overhead of the system is increased. A high quality code should not be generated or should be avoided as much as possible to avoid unnecessary overhead. We solved the problem by means of method. The relevant program code is as follows:
Code 2:
#include <iostream>
#include <string>
using namespace Std;
Class Father
{
Public
Father (String n,int h)
{
Name=n;
Height=h;
cout<< "? Construct the base class object \ n";
}
~father ()
{
cout<< "? destructor base class object with two parameters \ n";
}
void print ()
{
cout<<name<< "Height:" <
}
Protected
String name;
int height;
};
Class Son:public Father
{
Public
Son (String A,int i,int J): Father (A,i)//The constructor of the parent class is called with two arguments in the constructor of the subclass, and two arguments are passed for it
{
Weight=j;
cout<< "? construct subclass object \ n";
}
~son ()
{
cout<< "? destructor Subclass object \ n";
}
void Print1 ()
{
Print ();
cout<<name<< "Weight::" <<weight<< "cm" <<endl;
}
Protected
int weight;
};
int main ()
{
Son Mary ("Mary", 160,50);
Mary.print1 ();
cout<< "End of Procedure";
return 0;
}
This way, before we construct the subclass object, the constructor with two arguments to invoke the parent class completes the assignment to the two member name and height of the parent class. This way the class object is no longer assigned to the two members after inheriting the two members, but only the data members (weight) in the child class are required to assign the value. This allows the constructor of the parent class to be fully exploited. In addition, calling the constructor of the parent class to make the code more readable when someone else is in this code, you can see that the value of the first two arguments in the subclass constructor is assigned to the two members defined in the parent class by the constructor of the parent class, not to the members defined in the subclass. Subclasses are only two members that inherit from the parent class. This method of assigning a parent member and a subclass member separately can make the code much more readable, while reducing the likelihood of error.