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 |
How const functions will be treated by compiler?
Ask to write virtual base class code?
What is rvalue?
Can we use pointers in c++?
What are shallow and deep copies?
class base { public: int fun(int) {} }; class base2 { public: int fun(float) { } }; so here qustion is both function either function overloading or over riding;
Is c++ high level programming language?
What is c++ try block?
What are different types of loops in c++?
Explain the auto storage classes in c++.
Write a program and call it sortcheck.cpp which receives 10 numbers from input and checks whether these numbers are in ascending order or not. You are not allowed to use arrays. You should not define more than three variables
Is c++ built on c?