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
What is difference between rand () and srand ()?
What is the difference between strcpy() and strncpy()?
What is the use of turbo c++?
Write a Program to find the largest of 4 no using macros.
Explain the difference between realloc() and free() in c++?
If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?
What is vector pair in c++?
What problems might the following macro bring to the application?
What is a unnitialised pointer?
Explain abstraction.
What would happen on forgetting [], while deallocating an array through new?
What is runtime errors c++?
What is the difference between an external iterator and an internal iterator?
Explain the use of vtable.
What are the stages in the development cycle?