Whats in this post?
- Background and Explanation of the code
To copy code: Click “View Source” on the right of this code snippet.
Background and Explanation of the code
In this program the statement delete ptr; in the main ( ) invokes the derived class’s destructor as compiler dynamically binds to the destructor , depending upon the object to which base class ptr point.(Currently ptr points to derive’s object). After executing the derive class destructor ,the base class destructor is executed as destructors are executed in the reverse order of execution in inheritance.
Thus any memory allocated using the derived class constructor is freed.
Derive constructor allocating 10 bytes of memory
Derive destructor frees 10 bytes of memory