Q. While creation of object, constructor of base class calls first and after that constructor of derived class. But while deletion of object, destructor of derived class calls first then destructor of base class, why?
A: At the time of creation of object, memory is allocated in the stack. First memory has been created for base class then derived class.
As we know, stack is having "LIFO" (Last in First out) behaviour. Here derived class entered at the last in the stack, while creating object, so while deleting, it will be deleted first. And that is the reason, destructor of derived class calls first and then derived of bse class.
No comments:
Post a Comment