Constructor, Destructor and Virtual Destructor
Comment either base or derived objects in the main function and see the variation in the output. For virtual destructor concept remove the virtual keyword for base class destructor and see the output.
#include "iostream"
using namespace std;
class BaseClass
{
public:
BaseClass()
{
cout<<"Base Class constructor"< }
virtual ~ BaseClass()
{
cout<<"Base Class Destructor"< }
};
class DerivedClass :public BaseClass
{
public:
DerivedClass()
{
cout<<"Derived Class Constructor"< }
~ DerivedClass()
{
cout<<"Derived Class Destructor"< }
};
int main()
{
char ch;
BaseClass *bc = new BaseClass() ;
DerivedClass *dc = new DerivedClass();
BaseClass *bcd = new DerivedClass();
delete bc;
delete dc;
delete bcd;
cin>>ch;
}
#include "iostream"
using namespace std;
class BaseClass
{
public:
BaseClass()
{
cout<<"Base Class constructor"<
virtual ~ BaseClass()
{
cout<<"Base Class Destructor"<
};
class DerivedClass :public BaseClass
{
public:
DerivedClass()
{
cout<<"Derived Class Constructor"<
~ DerivedClass()
{
cout<<"Derived Class Destructor"<
};
int main()
{
char ch;
BaseClass *bc = new BaseClass() ;
DerivedClass *dc = new DerivedClass();
BaseClass *bcd = new DerivedClass();
delete bc;
delete dc;
delete bcd;
cin>>ch;
}
Comments
Post a Comment