In the inheritance relationship, if the six member functions are not defined in the subclass, the compiler will synthesize the functions by default
Implement a default member function in an inheritance relationship
class person { public: person(const char* name) :_name(name) { cout << "person()" << endl; } person(const person& p) :_name(p._name) { cout << "person(const person())" << endl; } person& operator=(const person& p) { cout << "person operator=()" << endl; if (this != &p) { _name = p._name; } return *this; } ~person() { cout << "~person" << endl; } protected: string _name; }; class student :public person { public: //The constructor of a subclass is synthetic, which combines the parent's and its own student(const char* name, int num)//If the constructor of a subclass does not pass parameters, the construction of the parent class should be defined as the default :person(name)//The constructor of a subclass can only initialize its own members, and the part of the parent class calls its own ,_num(num) { cout << "student()" << endl; } //The copy constructor of a subclass is synthetic, which calls the copy constructor of the parent class student(const student& s) :person(s) , _num(s._num) { cout << "student(const student &s)" << endl; } student& operator=(const student& s) { cout << "student& operator=()" << endl; if (this != &s) { person::operator=(s);//Be sure to specify the scope, otherwise it will be recursive all the time, resulting in stack overflow _num = s._num; } return *this; } ////It can't be defined in this way, which will make it hidden, because all destructors will be rewritten into the same names during compilation //~student() //{ // ~person(); //} //The destructor of the subclass does not need to call the parent class. It will clean up itself. The subclass destructs and out of scope. The parent class calls its own destructor ~student() { cout << "~studnet" << endl; } private: int _num; };
1. Constructor:
- The constructor of a subclass is synthetic, which combines the parent's and its own
- The constructor of a subclass can only initialize its own members, and the part of the parent class calls its own
- If the constructor of a subclass does not pass parameters, the construction of the parent class should be defined as the default
2. Copy constructor:
- The copy constructor of a subclass is synthetic, which calls the copy constructor of the parent class
3. assignment operator overload:
- The overloaded function of the assignment operator of the subclass is synthetic. To call the overloaded function of the assignment operator of the parent class
- When calling the assignment operator of the parent class, you must specify the scope, otherwise it will be recursive all the time, resulting in stack overflow
4. Destructor:
- The destructor does not need to call the parent class, it will clean up itself;
- After the scope is out, the parent class calls its own analysis; first construct the parent class, then construct the child class, first analyze the child class, then analyze the parent class
- The function names of all destructors will be rewritten to be the same during compilation, so the parent and child classes will be hidden
- If you want to call the destructor of the parent class in the destructor of the subclass, specify the scope.
~student()
{
person::~person();
}