1. Constructors and destructors are not inherited; [1]
Not all functions can be automatically inherited from a base class into a derived class. constructors and destructors are used to manipulate the creation and destruction of objects, and they only know what to do with objects at their particular level. Therefore, all constructors and destructors in the entire hierarchy must be called, i.e., constructors and destructors cannot be inherited.
In addition, operator= cannot be inherited, because it completes activities similar to constructor functions.
2. When a constructor of a derived class is called, one of the constructors of the base class is called first, because the base class constructor is called in the constructor of the derived class by initializing the table, and by default it is called a constructor that can pass no arguments in the base class.
3. When the derived class object is disposed, the derived class destructor is executed before the base class destructor is executed.
#include <iostream>using namespacestd; #include<string>classbasic{ Public: stringM_name; Basic (); Basic (stringname); Basic (Basic&BC); ~Basic (); Basic&operator=(Constbasic&BC) {cout<<"basic::operator= () \ n"; This->m_name =Bc.m_name; return* This; }}; Basic::basic () {cout<<"Basic::basic ()"<<Endl;} Basic::basic (stringname) {M_name=name; cout<<"basic::basic (String)"<<"Name:"<<m_name<<Endl;} Basic::basic (Basic&BC) { This->m_name =Bc.m_name; cout<<"Basic::basic (basic&)"<<"Name:"<<bc.m_name<<Endl;} Basic::~Basic () {cout<<" This is"<<m_name <<"Basic::~basic ()"<<Endl;;}classDerived: Publicbasic{ Public: intM_DX; Derived (); Derived (stringname);//M_name~Derived (); voidshow ();};D Erived::D erived () {cout<<"Derived::D erived ()"<<Endl;} Derived::D erived (stringname): Basic (name) {cout<<"Derived::D erived (String)"<<"Name:"<<m_name<<Endl;} Derived::~Derived () {cout<<" This is"<<m_name <<"derived::~derived ()"<<Endl;;}voidderived::show () {cout<<"Name:"<<m_name<<Endl;}voidTest () {Derived DC1 ("DC1"); cout<<""<<Endl; Derived DC2 ("DC2");//M_BXcout<<""<<Endl; DC1=DC2; cout<<"Next is Dc1.show ():"; Dc1.show (); cout<<""<<Endl;}intMain () {test (); while(1);}/**basic::basic (String) name:dc1derived::D erived (String) name:dc1basic::basic (String) NAME:DC2//Generate derived class object, First call the constructor of the base class Derived::D erived (String) NAME:DC2//In the call itself constructor basic::operator= ()//Call the base class operator=, and correctly to Derived::m_ The name is assigned to the value.
When testing, if the operator= of the base class is implemented as an empty function, then the derived object cannot be re-assigned to the Derived::m_name. Unless manually implementedDerived's operator=。
Operator= only one, drived if implemented manually, will overwrite the base class =. That is, the operator= of the base class is not executed.
Next is Dc1.show (): Name:dc2 the IS dc2de
**/
4. How the first line of the derived class constructs the function:
classbasic{ Public: intM_number; stringM_name; CharM_sex;//' m ' W 'Basic (intNstringName,Chars);}; Basic::basic (intNstringName,Chars): M_number (n), M_name (name), M_sex (s) {//this->m_name = name;//this->m_number = n;//this->m_sex = s;}classDerived: Publicbasic{ Public: intM_age; stringm_addr; Derived (intNstringNameCharSintAstringaddr);}; Derived::D erived ( int N,string name,Char s,int A, string addr): Basic (N,name,s), M_age (a), m_addr (addr) {}
Reference:
1. Constructs and destructors and operator= cannot be inherited
Http://www.cnblogs.com/findumars/p/3695340.html
C + +, constructors and destructors for derived classes