Destructor in Inheritance
As we know destructor are used to free memory which is occupied by the object. A we have seen , the constructor are executed from base class to the derived class. In contrast the destructor are executed in the reverse order that is from most derived class to the base class.
#include<iostream.h>
#include<conio.h>
class A
{
public:
~A( )
{
cout<<"\n\n\t Hi I am from---> A";
}
};
class B:public A
{
public:
~B( )
{
cout<<"\n\n\t Hi I am from---> B";
}
};
class C:public B
{
public:
~C( )
{
cout<<"\n\n\t Hi I am from---> C";
}
};
void main( )
{
//clrscr( );
C c1;
getch( );
}
Output:-
Hi I am from---> A
Hi I am from---> B
Hi I am from---> C
As we know destructor are used to free memory which is occupied by the object. A we have seen , the constructor are executed from base class to the derived class. In contrast the destructor are executed in the reverse order that is from most derived class to the base class.
#include<iostream.h>
#include<conio.h>
class A
{
public:
~A( )
{
cout<<"\n\n\t Hi I am from---> A";
}
};
class B:public A
{
public:
~B( )
{
cout<<"\n\n\t Hi I am from---> B";
}
};
class C:public B
{
public:
~C( )
{
cout<<"\n\n\t Hi I am from---> C";
}
};
void main( )
{
//clrscr( );
C c1;
getch( );
}
Output:-
Hi I am from---> A
Hi I am from---> B
Hi I am from---> C
0 Comments:
Post a Comment