A problem can occur while using polymorphism to process dynamically located object of a class hierarchy. If an object is destroyed explicitly by applying 'delete' operator to a base class pointer to the object, only the base class destructor function is called on the object.
But we are interested to delete whole memory allocated to the base class as well as derived classes. There is simple solution to this problem , declare the virtual base class destructor. This automatically makes all derived class destructor virtual even though they do not have the same name as the base class destructor.
Program to demonstrate the need of virtual destructor
#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}
Output:-
This is class B and getdata( ) method
This is a class A destuctor
Program to demonstrate the use of virtual destructor
#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
virtual ~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}
Output:-
This is class B and getdata( ) method
This is a class B destructor
This is a class A destructor
But we are interested to delete whole memory allocated to the base class as well as derived classes. There is simple solution to this problem , declare the virtual base class destructor. This automatically makes all derived class destructor virtual even though they do not have the same name as the base class destructor.
Program to demonstrate the need of virtual destructor
#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}
Output:-
This is class B and getdata( ) method
This is a class A destuctor
Program to demonstrate the use of virtual destructor
#include<conio.h>
#include<iostream.h>
class A
{
public:
virtual void getdata( )
{
cout<<"\n\tThis is class A and getdata( ) method";
}
virtual ~A( )
{
cout<<"\n\tThis is a class A destructor";
}
};
class B:public A
{
public:
void getdata( )
{
cout<<"\n\tThis is class B and getdata( ) method";
}
~B( )
{
cout<<"\n\tThis is a class B destructor";
}
};
void main( )
{
clrscr( );
A *ptr;
ptr=new B;
ptr->getdata( );
delete ptr;
getch( );
}
Output:-
This is class B and getdata( ) method
This is a class B destructor
This is a class A destructor
0 Comments:
Post a Comment