C++: Constructors And Destructors 13

Example:2
Give the output of the following program code?

#include<iostream>
using namespace std;
class A
{
public:
	A()
	{
	  cout<<"Constructor of class A"<<endl;
	}
	~A()
	{
	  cout<<"Destructor of class A"<<endl;
	}
};
class B
{
	A a1;
public:
	B()
	{
	  cout<<"Constructor of class B"<<endl;
	}
	~B()
	{
	  cout<<"Destructor of class B"<<endl;
	}
};
class C
{
	A a1,a2;
	B b1;
public:
	C()
	{
	  cout<<"Constructor of class C"<<endl;
	}
	~C()
	{
	  cout<<"Destructor of class C"<<endl;
	}
};

int main()
{
	C c1;
 	return(0);
}

Output:

Constructor of class A
Constructor of class A
Constructor of class A
Constructor of class B
Constructor of class C
Destructor of class C
Destructor of class B
Destructor of class A
Destructor of class A
Destructor of class A

Example:3
Give the output of the following program code?

#include<iostream>
using namespace std;
class A
{
public:
	A()
	{
	  cout<<"Constructor of class A"<<endl;
	}
	~A()
	{
	  cout<<"Destructor of class A"<<endl;
	}
};
class B
{
	A a1;
public:
	B()
	{
	  cout<<"Constructor of class B"<<endl;
	}
	~B()
	{
	  cout<<"Destructor of class B"<<endl;
	}
};
class C
{
	A a1,a2;
	B b1;
public:
	C()
	{
	  cout<<"Constructor of class C"<<endl;
	}
	~C()
	{
	  cout<<"Destructor of class C"<<endl;
	}
};

int main()
{
	B a1;
  	C b1;
  	A c1;
 	return(0);
}

Output:

Constructor of class A
Constructor of class B
Constructor of class A
Constructor of class A
Constructor of class A
Constructor of class B
Constructor of class C
Constructor of class A
Destructor of class A
Destructor of class C
Destructor of class B
Destructor of class A
Destructor of class A
Destructor of class A
Destructor of class B
Destructor of class A