Coupon Accepted Successfully!


Constructors in Derived Class

When a class is declared, a constructor is also declared inside the class in order to initialize data members. It is not possible to use a single constructor for more classes. Every class has its own constructor and destructor with a similar name as the class. When a class is derived from another class, it is possible to define a constructor in the derived class, and the data members of both base and derive classes can be initialized. It is not essential to declare a constructor in a base class. Thus, the constructor of the derived class works for its base class; such constructors are called constructors in the derived class or common constructors.

11.29 Write a program to initialize member variables of both base and derived class using a constructor of derived class.
x = 1 y = 2 z = 3
Explanation: In the above program, the classes A B are declared. The class B is derived from the class A. The constructor of the class B initializes member variables of both classes. Hence, it acts as a common constructor of both base and derived classes.

Test Your Skills Now!
Take a Quiz now
Reviewer Name