Explain the need for "Virtual Destructor"?
Answers were Sorted based on User's Feedback
Answer / lylez00
If A is a base class, and from that, B is derived, and a
dynamically allocated object of type B is deleted via a
pointer of type A, then B's destructor will not be invoked
unless A's destructor is virtual.
A *a = new B();
delete a; // won't invoke B's destructor unless A's
destructor is virtual
| Is This Answer Correct ? | 12 Yes | 3 No |
Answer / p govind rao
A destructor can be declare virtual. virtual destructor is
mainly useful during inheritance.
class base
{
public:
base(){}
virtual ~base(){}
};
class derv
{ char *p;
public :
derv(){ptr=nes char[2];}
~derv(){delete ptr;}
} ;
main()
{
base *baseptr=new derv();
delete baseptr;
}
If base class, and derived class, and a dynamically
allocated object of type derived is deleted via a pointer
of type base, then derived's destructor will not be invoked
unless base's destructor is virtual.
base *baseptr = new derv();
delete baseptr; // won't invoke B's destructor unless A's
destructor is virtual
| Is This Answer Correct ? | 6 Yes | 0 No |
What do you mean by const correctness?
Give example of a pure virtual function in c++?
How do you declare A pointer to a function which receives nothing and returns nothing
reading material is provided 3 books for c++ if u need more do let me know thnx i hve lots of material do let me know if u want it
What is else syntax in c++?
Name some pure object oriented languages?
What is a map in c++?
Define what is constructor?
Difference between an inspector and a mutator
What is size of a empty class?
7 Answers Microsoft, Tata Elxsi, Wipro,
You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. New() and malloc()
When should we use multiple inheritance?