what is virtual destructor

Answers were Sorted based on User's Feedback



what is virtual destructor..

Answer / isha

Dear friend , you have done a mistake..
Example:

class Employee {
virtual ~Employee() {}
};

class Manager : public Employee {
~Manager() {}
}

Employee * m = new Manager();//here was the mistake
delete m; // <--

Is This Answer Correct ?    9 Yes 0 No

what is virtual destructor..

Answer / kamil mohamed

virtual destructor is recommended when you want to destroy
(delete) an object through it's parent pointer. This good
habit enforces proper cleanup of derived classes

Example:

class Employee {
virtual ~Employee() {}
};

class Manager : public Employee {
~Manager() {}
}

Manager * m = new Employee();
delete m; // <--

Is This Answer Correct ?    6 Yes 1 No

what is virtual destructor..

Answer / rock

When a derived class object pointed to by a base class
pointer dynamically is deleted only the base class
destructor is invoked inorder to even invoke derived class
destructor we use virtual destructor.

class shape
{
virtual ~shape(){}
};
class circle:public shape
{
~circle(){}
};
void main()
{
shape *sh = new circle;
delete sh; //both the destructors are invoked
}

If virtual keyword is not added to the base class
destructor only the base class destructor is called.

Is This Answer Correct ?    3 Yes 0 No

what is virtual destructor..

Answer / pankajkolte

Virtual Destructor is used basically to ensure proper the
sequence of call to destructor.

class Employee {
virtual ~Employee() {}
};

class Manager : public Employee {
~Manager() {}
}

Employee * m = new Manager();//here was the mistake
delete m; // <-

In this case base class destructor if marked as virtual so
as derived class destructor will get called first then base
class destructor.

Is This Answer Correct ?    2 Yes 0 No

what is virtual destructor..

Answer / suresh.k (portblair)

Using virtual destructors, you can destroy objects without
knowing their type - the correct destructor for the object
is invoked using the virtual function mechanism. Note that
destructors can also be declared as pure virtual functions
for abstract classes.

if someone will derive from your class, and if someone will
say "new Derived", where "Derived" is derived from your
class, and if someone will say delete p, where the actual
object's type is "Derived" but the pointer p's type is your
class.

Is This Answer Correct ?    2 Yes 4 No

what is virtual destructor..

Answer / rahul

virtual destructor is just to remove the variable without
knowing its type.....

Is This Answer Correct ?    0 Yes 3 No

what is virtual destructor..

Answer / maria alex

Normally virtual destructor is used to destroy a base class
object without knowing it's type...

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More OOPS Interview Questions

What is object in oop?

0 Answers  


What is virtual constructors/destructors?

4 Answers   IBS,


what is virtual function?

26 Answers   Aspire, HP, Infosys, RoboSoft, TCS,


#include <string.h> #include <stdio.h> #include <stdlib.h> #include<conio.h> void insert(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); insert(s, strlen(s)); printf("The sorted string is: %s.\n", s); getch(); return 0; } void insert(char *items, int count) { register int a, b; char t; for(a=1; a < count; ++a) { t = items[a]; for(b=a-1; (b >= 0) && (t < items[b]); b--) items[b+1] = items[b]; items[b+1] = t; } } design an algorithm for Insertion Sort

0 Answers  


tell about copy constructor

3 Answers   Siemens,






i ahve v low % in 12th n BSC which is aroun 50 coz science was imposed on me......nw m doin MCA n my aggregate in above 74%,what shud i say if asked about low previous percentage??????

4 Answers  


Can we have a private virtual method ?

8 Answers   Ness Technologies,


What makes a language oop?

0 Answers  


What is basic concept of oop?

0 Answers  


How do you answer polymorphism?

0 Answers  


Tell me the scenario,Where we can use interfaces or Abstract class.And What is the difference between interfaces and abstract class?

5 Answers  


Round up a Decimal number in c++.. example Note = 3.5 is as 4 3.3 is as 3

3 Answers   Accenture, Cognizant, IBM,


Categories