Why and when is a virtual destructor needed?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
write knight tour problem which is present in datastructure
#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.
Difference between vector and array
What do we mean by a hidden argument in C++?
why to use operator overloading
Can we have inheritance without polymorphism?
what is the use of template classes in c++
what is code for call by value and call by reference?
What is a unary operator?
Program to check whether a word starts with a capital letter or not.
what is the difference between class and object?
the difference between new and malloc