Loading....
Coupon Accepted Successfully!

 

Constructors, Destructors, and Inheritance

The constructors are used to initialize member variables of the object, and the destructor is used to destroy the object. The compiler automatically invokes constructors and destructors. The derived class does not require a constructor, if the base class contains a zero-argument constructor. In case the base class has a parameterized constructor, then it is essential for the derived class to have a constructor. The derived class constructor passes arguments to the base class constructor. In inheritance, normally derived classes are used to declare objects. Hence, it is necessary to define constructors in the derived class. When an object of a derived class is declared, the constructors of the base and derived classes are executed.

Constructors and destructors in base and derived classes

In inheritance, destructors are executed in reverse order of constructor execution. The destructors are executed when an object goes out of scope. To know the execution of constructors and destructors, let us study the following program:

11.12 Write a program to show sequence of execution of constructor and destructor in multiple inheritance.
 
OUTPUT
Zero-argument constructor of base class A
Zero-argument constructor of base class B
Zero-argument constructor of derived class C
Destructor of the class C
Destructor of the class B
Destructor of the class A

Explanation:
In the above program, classes A and b are two classes. The class C is derived from classes A and B. The constructors of the base classes are executed first followed by the derived class. The destructor of the derived class is executed first followed by the base class.

11.13 Write a program to use constructor and destructor in all classes (base and derived class). Read and display the data.
 
OUTPUT
Name : Ajay
Age : 20
Height : 4.5
Weight : 40
Sex : M
Sex : M
Height : 4.5
Weight : 40
Name : Ajay
Age : 20

Explanation:
In the above program, classes A1, A2, and A3 have their own constructors and destructors. Here, the class A1 is the base class of A2, and A2 is the base class of A3 or, in other words, A3 is a derived class of A2 and A2 is a derived class of A1.

Execution of constructors: The execution of constructors takes place from the base class to the derived class. Thus, the sequence of the execution of constructors is A1(), A2(), and A3().

Execution of destructors: The execution of destructors is in opposite order as compared with constructors, that is, from the derived class to the base class. Thus, the sequence of the execution of destructors is A3(), A2(), and A1(). We learnt in Chapter 9 that the object created last destroyed first. The same rule is applicable in the execution of constructors and destructors in inheritance.

11.14 Write a program to create derived class from base classes. Use constructor and destructors.
OUTPUT
Constructor in_t()
Constructor floa_t()
Constrcutor cha_r()
i= 1
f= 1.5
c= A
Destructor cha_r()
Destructor floa_t()
Destructor in_t()
 
Explanation: In the above program, three classes in_t, floa_t, and cha_r are defined, and each contains one protected variable of type int, float, and char, respectively. Each class contains a constructor and a destructor. In function main(), a is an object of class derived class chr_r. The member function show() is invoked by the object a to display the contents of member variables of all the classes. The constructors are executed from the base to the derived class, and destructors are loaded from the derived to the base class. The following program explains the execution of constructors and destructors in classes in greater detail:

 

Base and derived classes without constructors

In the absence of constructors in base and derived classes, the implicit constructors and destructors are executed when objects are declared. Consider the following program:

11.15 Write a program to derive a class. The base and derive classes should be without constructors.
 
OUTPUT
X = 4 y = 5
 
Explanation: In the above program, class I is a base class, and class II is derived from I. Both classes are without constructors. The class II has used member functions set() and show() to assign and display data.

Base class with constructors and derived class without constructors

11.16 Write a program to declare a base class with constructor and a derived class without constructor.
 

OUTPUT
In base class constructor
 

Explanation: The base class I has a constructor, and the derived class II does not have a constructor. The i is an object of the derived class II. When i is declared, the constructor of the base class is executed. The derived class is without a constructor; hence, only the base class constructor is executed.

Base class without constructors and derived class with constructors

11.17 Write a program to declare base class without constructor and derived class without constructor.
 
OUTPUT
In derived class constructor

Explanation: In the above program the base class I has no constructor. The derived class II has a constructor. The constructor is executed when the object of class II is declared.

Base and derived classes with constructors

11.18 Write a program to declare both base and derived classes with constructor.
 
OUTPUT
In base class constructor
In derived class constructor
 
Explanation: In the above program, both the base and derived classes contain constructors. When the object of the derived class type is declared and constructors of both the classes are executed, the constructor of the base class is executed first followed by the constructor of the derived class.

Base class with various constructors and derived class with one constructor

11.19 Write a program to declare multiple constructors in base class and single constructor in derived class.
 
OUTPUT
Zero argument base class constructor
One argument derived class constructor
 
Explanation: In the above program, the class I has a zero- and a one-argument constructor. The class II has only a one-argument constructor. The object i is declared with an integer. The zero-argument constructor of the base class and the one-argument constructor of the derived class are executed.

Base and derived classes without default constructors

11.20 Write a program to declare base and derived class without default constructor.

#include<iostream.h>

#include<conio.h>

class I

{

public:

int x;

I ( int k)

{

x=k;

cout<<“\nOne argument base class constructor”;}

};

class II : public I

{

int y;

public:

II (int j):I(j)

{

cout<<“\nOne argument derived class constructor”;

y=j;

}

};

int main()

{
clrscr();
II i(2);
return 0;
}

OUTPUT
One argument base class constructor
One argument derived class constructor
 
​Explanation: In the above program, no default constructor is declared. In the class II one argument, a constructor is declared, and the base class constructor is explicitly invoked. When object i is declared, a one-argument constructor of both the classes is executed. In the absence of an explicit call of the base class constructor, the compiler will display the error message “Cannot find default constructor to initialize base class I.’”

Constructors and multiple inheritance

11.21 Write a program to derive a class using multiple base classes. Observe the execution of constructor when the object of derived class is declared.
 

OUTPUT
Zero argument constructor of base class II
Zero argument constructor of base class I
Zero argument constructor of base class III
 
Explanation: The classes I and II are the base classes of the derived class III. In function main(), i is an object of the derived class III. The execution of constructors depends on the sequence given while deriving a class as per the following statement:
 
class III : public II,I
 
Here, the class II is the first base class, and class I is the second base class; the execution sequence of the constructors will be as shown in the output.

Constructors in multiple inheritance with explicit calls

11.22 Write a program to derive a class using multiple base classes. Invoke the constructors of base classes explicitly.
 


OUTPUT

Zero argument constructor of base class II
Zero argument constructor of base class I
Zero argument constructor of base class III
 
​​Explanation: In this program in the class III, explicitly the constructor of both the base classes II and I are invoked. The execution sequence can be observed in the output as shown above. The execution sequence of the constructors depends on the sequence of the base class and not on the explicit calls.

Multiple inheritance and virtual class

11.23 Write a program to derive a class using multiple base classes. Invoke the constructors of base classes explicitly. Declare any one base class as virtual.
 
OUTPUT
Zero argument constructor of base class I
Zero argument constructor of base class II
Zero argument constructor of base class III

Explanation:
In this program, the base class I is declared a virtual class while deriving the class. The constructor of the virtual class is executed first. The execution of the constructor here is not according to the sequence of the base class.

Execution of constructors in multilevel inheritance

11.24 Write a program to derive a classes using multilevel inheritance and observe the execution sequence of constructors.
 


OUTPUT

Zero argument constructor of base class I
Zero argument constructor of base class II
Zero argument constructor of base class III
 

Explanation: In this program, the class II is derived from the class I. The class III is derived from the class II. The class II is a base as well as a derived class. In function main(), ii is an object of the class III. The constructors are executed from base to derived classes as shown in the output. Table shows the order of execution of the constructors.

Table: Execution sequences of constructors
 

Statements

Sequence of execution

Remarks

Class II : public I

I() – Base class constructor

II() – Derived class constructor

Single inheritance

Class III: public I, II

I() – Base class constructor

II() – Base class constructor

III() – Derived class constructor

Multiple inheritance

Class III: public I, virtual II

II() – Virtual class constructor

I() – Base class constructor

III() – Derived class constructor

Multiple inheritance

class II : public I {};

class III: public II {};

I() – First base class constructor

II() – Second base class constructor

III() – Derived class constructor

Multilevel inheritance





Test Your Skills Now!
Take a Quiz now
Reviewer Name