A) Write a Program in C++ to illustrate the order of execution of constructors and destructors in inheritance.
AIM: Write a Program in C++ to illustrate the order of execution of constructors and destructors in inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class BaseClass
{
public:
BaseClass()
{
cout<<"Base Class constructor called."<<endl;
}
~BaseClass()
{
cout<<"Base class destructor called."<<endl;
}
};
class DerivedClass:public BaseClass
{
public:
DerivedClass()
{
cout<<"Derived Class constructor called."<<endl;
}
~DerivedClass()
{
cout<<"Derived class destructor called."<<endl;
}
};
int main()
{
DerivedClass obj;
}
OUTPUT:
Base Class constructor called.
Derived Class constructor called.
Derived class destructor called.
Base class destructor called.
AIM: Write a Program in C++ to illustrate the order of execution of constructors and destructors in inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class BaseClass
{
public:
BaseClass()
{
cout<<"Base Class constructor called."<<endl;
}
~BaseClass()
{
cout<<"Base class destructor called."<<endl;
}
};
class DerivedClass:public BaseClass
{
public:
DerivedClass()
{
cout<<"Derived Class constructor called."<<endl;
}
~DerivedClass()
{
cout<<"Derived class destructor called."<<endl;
}
};
int main()
{
DerivedClass obj;
}
OUTPUT:
Base Class constructor called.
Derived Class constructor called.
Derived class destructor called.
Base class destructor called.