Why and when is a virtual destructor needed?

Answer Posted / uma sankar pradhan

A virtual destructor is needed when we are deleting a
object of derived class using a base class pointer.
i.e.,
base *b=new derived;
delete(b);
Let's say,we have allocated memory dynamically in derived
class constructor to a pointer data member and we
deallocated it in the destructor to avoid memory leakage
When the object is deleted through base class pointer,
only the base class destructor is invoked.consequently,the
dynamically allocated space remains unreleased.so it leads
to memory leak

Is This Answer Correct ?    34 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a function in oop?

821


What is oops in simple words?

802


What is stream in oop?

1033


What is the renewal class?

2404


What is difference between abstraction and encapsulation?

803


What is difference between inheritance and polymorphism?

769


Question: Write a program that prints a paycheck. Ask the program user for the name of the employee, the hourly rate, and the number of hours worked. If the number of hours exceeds 40, the employee is paid “time and a half”, that is, 150 percent of the hourly rate on the hours exceeding 40. Be sure to use stepwi se refine ment and break your solution into several functions. Use the int_name function to print the dollar amount of the check.

941


Why do we use polymorphism?

756


What is overloading in oops?

826


Why interface is used?

739


What is polymorphism what are the different types of polymorphism?

738


Can we have inheritance without polymorphism?

780


write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.

1977


What is constructor in oop?

814


What is encapsulation with example?

781