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
what type of questions
Explain the concepts involved in Object Oriented programming.
What is destructor give example?
officer say me - i am offered to a smoking , then what can you say
Can we have inheritance without polymorphism?
What is overloading in oop?
Can an interface inherit a class?
Can we create object of abstract class?
to find out the minimum of two integer number of two different classes using friend function
What is difference between abstraction and encapsulation?
Why do pointers exist?
Advantage and disadvantage of routing in telecom sector
What is abstraction oop?
What are the 3 principles of oop?
any one please tell me the purpose of operator overloading