top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What happens when a derived-class object is created and destroyed in C++?

+1 vote
615 views
What happens when a derived-class object is created and destroyed in C++?
posted Dec 19, 2014 by Emran

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself)

The base class's constructor is called to initialize the data members inherited from the base class

The derived class's constructor is then called to initialize the data members added in the derived class

The derived-class object is then usable

When the object is destroyed (goes out of scope or is deleted) the derived class's destructor is called on the object first

Then the base class's destructor is called on the object

Finally the allocated space for the full object is reclaimed .

answer Dec 23, 2014 by Mohammed Hussain
...