9.7 BASE AND DERIVED CLASS CONSTRUCTORS

It is important to note that the constructors are not inherited in the inheritance process, unlike other members which are inherited. Syntax of derived class constructor is special. The reason is this constructor not only initializes its own data members but also those of the base class. For this, it has to allot arguments for base class constructor too. In prototype, constructor looks normal, but in definition, it has a peculiar syntax. Let us first see a simple program.

Program: Write a program to define a class clock. It should be derived from class time. The program should demonstrate the calling of constructors clearly.

Solution: See Program 9.4.

Program 9.4 Derived class constructor

// constru1.cpp ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.