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 ? | 11 Yes | 0 No |
Post New Answer View All Answers
Is data hiding and abstraction same?
What is a class in oop?
Can a destructor be called directly?
What is destructor oops?
What is meant by oops concept?
Can static class have constructor?
Is enum a class?
What is class encapsulation?
what are the realtime excercises in C++?
What is polymorphism and why is it important?
What is protected in oop?
What is the fundamental idea of oop?
What is the diamond problem in inheritance?
What is inheritance write a program to show use of inheritance?
What is destructor example?