Why and when is a virtual destructor needed?

Answers were Sorted based on User's Feedback



Why and when is a virtual destructor needed?..

Answer / 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

Why and when is a virtual destructor needed?..

Answer / guest

Any class that may act as the base of another class should
have a virtual destructor. This ensures that when an object
of the derived class is destroyed that the derived class
dtor will be invoked to destroy it. If the destructor is not
virtual, under some common circumstances, only the base
class' destructor will be invoked, regardless of the class
actually being destroyed. For practical purposes this means
that a class which does, could or should have virtual member
functions, should also have a virtual destructor.

Is This Answer Correct ?    18 Yes 3 No

Why and when is a virtual destructor needed?..

Answer / pradeep

This example fully describe the need of Virtual Destructor
in base class:-
----------------------------------------------------------
#include <iostream.h>
#include <stdio.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
};
void main()
{
Base *Var = new Derived();
delete Var;
getch();
}

-----------------------------------------------------------
When it will be executed..it will show only that Base Class
destructor executed not the Derived.
But if we make Base class destructor "virtual"
(i.e. virtual ~Base(){ cout<<"Destructor : Base"<<endl;} )
then we can verify that Destructor execute into this order:--
1. Derived class destructor
2. Base class destructor

---If there is any mistake kindly let me know.
Thanks...!!!

Is This Answer Correct ?    11 Yes 0 No

Why and when is a virtual destructor needed?..

Answer / girish audichya

Virtual destroctor needed to delete the occupied space by
derived class using base class

Is This Answer Correct ?    3 Yes 1 No

Why and when is a virtual destructor needed?..

Answer / hemlata selokar

At the time of inheritance, when we are deleting the object
of derived class with the help of base class pointer that
time virtual destructors are used... After making base class
destructor as virtual, the derived class destructor is
called first followed by base class destructor...With the
help of this proper sequence is maintained and helps in
proper execution.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More OOPS Interview Questions

What is a class oop?

0 Answers  


what is runtime polymorphism? For the 5 marks.

3 Answers  


program for insertion ,deletion,sorting in double link list

0 Answers  


Write a program to accept a number and to print numbers in pyramid format? for eg:for a no. 5 1 212 32123 4321234 543212345

7 Answers  


Which language is not a true object oriented programming language?

0 Answers  


some one give d clear explanation for polymorphism

3 Answers  


WHEN A COPY CONSTER IS CALL ?

4 Answers  


What do you mean by Encapsulation?

0 Answers   Ittiam Systems,


WILL I GET A guaranteed JOB AFTER DOING bsc()IT) and GNIIT from an NIIT CENTRE??

21 Answers   Biocon, MIT, NIIT,


What is difference between function overloading and overriding?

1 Answers   emc2,


What is virtual function?where and when is it used?

2 Answers   Sitel,


write a code for this:trailer recordId contains a value other than 99, then the file must error with the reason ‘Invalid RECORD_ID’(User Defined Exception).

0 Answers  


Categories