What is a virtual destructor? Explain the use of it.

- If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.

- Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.

 

Output:

Base Constructor
Derived Constructor
Base Destructor


- By Changing:
Output :


Base Constructor
Derived Constructor
Derived Destructor
Base Destructor

No comments:

Post a Comment

The advantages of Outsourcing Web Development Services

In the present digital world, acquiring a solid web presence is essential for companies to flourish. A well designed site is much much more ...