Example of constructor and destructor invocation order in C + + class inheritance

Source: Internet
Author: User

/* When an object is established, the constructor of the base class is called first, and then the next derived class's
constructor, and so on, until you reach the constructor of the most derived class that derives the most number of times.
In short, objects are constructed from the "bottom up" start. Because, when a constructor is initially constructed, it is always
To invoke the constructor of its base class before it starts executing its constructor body, when the direct base class constructor is called,
If there is no specific description, the default constructor for the direct base class is called. When an object is refactored, its order is reversed.
The following procedure illustrates the problem.
//-------------------------------------------------
#include <iostream>
using namespace Std;
Class Shape
{
Public
void Draw () {cout<< "Base::D Raw ()" &LT;&LT;ENDL;}
void Erase () {cout<< "Base::erase ()" &LT;&LT;ENDL;}
Shape () {Draw ();}//base class constructor, call the draw function body above
Virtual ~shape () {Erase ();} Base class destructor, call the Erase function body above
};
//-------------------------------------------------
Class Polygon:public Shape
{
Public
Polygon () {Draw ();}
void Draw () {cout<< "Polygon::D Raw ()" &LT;&LT;ENDL;}
void Erase () {cout<< "Polygon Erase ()" &LT;&LT;ENDL;}
~polygon () {Erase ();}
};
//--------------------------------------------------
Class Rectangle:public Polygon
{
Public
Rectangle () {Draw ();}
void Draw () {cout<< "Rectangle::D Raw ()" &LT;&LT;ENDL;}
void Erase () {cout<< "Rectangle Erase ()" &LT;&LT;ENDL;}
~rectangle () {Erase ();}
};
//--------------------------------------------------
Class Square:public Rectangle
{
Public
Square () {Draw ();}
void Draw () {cout<< "Square::D Raw ()" &LT;&LT;ENDL;}
void Erase () {cout<< "Square Erase ()" &LT;&LT;ENDL;}
~square () {Erase ();}
};
//--------------------------------------------------
int main ()
{
Polygon C;
Rectangle s;
Square T;
cout<< "------------------------------------------" <<endl;
return 0;
}
//------------------------------------------

Operation Result:

Base::D Raw ()
Polygon::D Raw ()
Base::d Raw ()
Polygon::D Raw ()
Rectangle::D Raw ()
Base::d Raw ()
Polygon::D Raw ()
Rectangle::D Raw ()
Square::D Raw ()
------------------------------------------
Square Erase ()
Rectangle Erase ()
Polygon Erase ()
Base::erase ()
Rectangle Erase ()
Polygon Erase ()
Base::erase ()
Polygon Erase ()
Base::erase ()
Press any key to continue

Example of constructor and destructor invocation order in C + + class inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.