Coupon Accepted Successfully!


Execution Sequence Of Constructor And Destructor

Overloaded new and delete operators functioning within the class are always static. We know that the static function can be invoked without specifying the object. Hence, this pointer is absent in the body of static functions. The compiler invokes the overloaded new operator and allocates memory before executing the constructor. The compiler also invokes the overloaded delete operator function and deallocates memory after execution of the destructor. The following program makes the concept clearer:

14.13 Write a program to display the sequence of execution of constructor and destructor in classes when operators new and delete are overloaded.





class boy


char name[10];

public :

void *operator new (size_t );

void operator delete (void *q);




char limit[sizeof(boy)];



cout<<endl<<“In Constructor”;



{ cout<<endl<<“In Destructor”; }

void *boy ::operator new (size_t s )


cout<<endl<<“In boy ::new operator”;

return limit;


void boy::operator delete (void *q)

{ cout<<endl<<“In boy ::delete operator”; }

void main()



boy *e1;

e1=new boy;

delete e1;


In boy ::new operator
In Constructor
In Destructor
In boy ::delete operator

In this program, the new and delete operators are overloaded. The class also has a constructor and a destructor. The overloaded new operator function is executed first followed by the class constructor. We know that the constructor is always used to initialize members. The constructor also allocates memory by calling the new operator implicitly. Here, the new operator is overloaded. As soon as control of the program reaches the constructor, before executing any statement in the constructor it invokes the overloaded new operator.

Similarly, when an object goes out of scope, the destructor is executed. The destructor invokes the overloaded delete operator to release the memory allocated by the new operator.

Test Your Skills Now!
Take a Quiz now
Reviewer Name