when can we use virtual destructor?
Answer Posted / 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 ? | 14 Yes | 1 No |
Post New Answer View All Answers
Explain explicit container.
What are move semantics?
To which numbering system can the binary number 1101100100111100 be easily converted to?
What are the c++ access specifiers?
What is c++ course?
Should I learn c or c++ first?
Distinguish between new and malloc and delete and free().
write a program that withdrawals,deposits,balance check,shows mini statement. (using functions,pointers and arrays)
What are references in c++? What is a local reference?
What is the meaning of string in c++?
List down the guideline that should be followed while using friend function.
Explain how the virtual base class is different from the conventional base classes of the opps.
What is iterator c++?
How do I use turbo c++?
Who created c++?