Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


Why would you make a destructor virtual?

Answers were Sorted based on User's Feedback



Why would you make a destructor virtual?..

Answer / chandra

Vitual destructor is used

(1) whenever the base class object pointer points to
derived class object,and is being created using new.
i.e class base - base class
in main - base bptr;
class derived - derived class
in main - bptr = new derived();

Then whenever we use "delete bptr" at runtime always the
memory of bptr is freed but not derived because here the
pointer is of type base and not of derived.

Note: The destructor that gets invoked is the one that
associated with the type of the object.

Simple rule of thumb: Make your destructor virtual if your
class has any virtual functions.

Example:
#include <iostream.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
virtual ~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;
}


Is This Answer Correct ?    9 Yes 0 No

Why would you make a destructor virtual?..

Answer / chandra sekhar

what chandra said is correct.but it is not 100% correct.for
that answer i want to add one point.

whenever
1)there is a pointer in the base class and we allocated
with the help of new in base class and anthoer pointer in
derived class and agin we allocated with the help of new.
2)whenever the base class object pointer points to
derived class object,and is being created using new.
i.e class base - base class
in main - base bptr;
class derived - derived class
in main - bptr = new derived();

this will be illustrated with an example given below


#include <iostream.h>
class Base
{ protected:
int *ptr;
public:
Base()
{ ptr = new int[10];
cout<<"Constructor: Base"<<endl;
}
virtual ~Base()
{
cout<<"Destructor : Base"<<endl;
delete []ptr;
}
};
class Derived: public Base
{
protected:
int *ptr1;
public:
Derived()
{
ptr1=new int[10];
cout<<"Constructor: Derived"<<endl;
}
~Derived()
{
delete []ptr1;
cout<<"Destructor : Derived"<<endl;
}
};
void main()
{
Base *Var = new Derived;
delete Var;
}

if we are not placing virtual in base class, only base
class destructor will be called because "var is of type
Base".so,
if we are not placing virtual compiler will see only "type
of the pointer,not the object which it is holding".so,if we
place virtual it will see the object it is holding.

now memory allocated for ptr,ptr1 in heap was destroyed by
destructors of the respective class.

Is This Answer Correct ?    4 Yes 0 No

Why would you make a destructor virtual?..

Answer / sunny bogawat

we make destructure as virtual because when we derived a
class from base class then destructor called from derived
to base so for this happening correctly we haveto make base
class destructor as avirtual.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Explain the uses oof nested class?

0 Answers  


How do we implement inheritance in c++?

0 Answers  


Write a program which is required to process the time of a clock in hours and minutes, entered from the keyboard. With this program, there are two requirements for any data entered by a user: 1. The data must be of the correct type (in this case, two ints). 2. The data must be in the correct range: this means that, for the minutes, negative numbers and any number above 59 must be rejected; for the hours, negative numbers and any number above 23 must be rejected. Output error message for invalid data input. Output the time one and a half hour after the time input. i.e. Hour: 22 Min: 32 One and a half hour after 22:32 is 00:02

0 Answers  


What is else syntax in c++?

0 Answers  


How do you declare A pointer to function which receives an int pointer and returns a float pointer

0 Answers  






What do you mean by vtable and vptr in c++?

0 Answers  


What does scope resolution operator do?

0 Answers  


Which is better turbo c++ or dev c++?

0 Answers  


Is overriding possible in c++?

0 Answers  


What does the ios::ate argument do?

0 Answers  


write a program that takes two numbers from user that prints the smallest number

2 Answers  


Is java made in c++?

0 Answers  


Categories