Thought:
In the class inheritance of C + +,
When an object is created, the constructor of the base class is called first, and then the constructor of the next derived class is called, and so on;
When the object is reconstructed, its order is exactly the opposite of the construction;
Example:
#include <iostream>using namespace Std;class shape{public:void Draw () {cout<< "Base::D Raw ()" <<ENDL;} void Erase () {cout<< "Base::erase ()" <<ENDL;} Shape () {Draw ();} ~shape () {Erase ();}};/ /-------------------------------------------------class Polygon:public Shape{public:polygon () {Draw ();} void Draw () {cout<< "Polygon::D Raw ()" <<ENDL;} void Erase () {cout<< "Polygon Erase ()" <<ENDL;} ~polygon () {Erase ();}};/ /--------------------------------------------------class Rectangle:public Polygon{public:rectangle () {Draw ();} void Draw () {cout<< "Rectangle::D Raw ()" <<ENDL;} void Erase () {cout<< "Rectangle Erase ()" <<ENDL;} ~rectangle () {Erase ();}};/ /--------------------------------------------------class Square:public Rectangle{public:square () {Draw ();} void Draw () {cout<< "Square::D Raw ()" <<ENDL;} void Erase () {cout<< "Square Erase ()" <<ENDL;} ~square () {ErasE ();}};/ /--------------------------------------------------int main () {Polygon C; Rectangle s; Square T; cout<< "------------------------------------------" <<endl; return 0;}
Results:
(c + +) constructors and destructors in C + + class inheritance