Inheritance: Constructors and Destructors
- A derived class inherits the members of its base class. Therefore, when a
derived class object is instantiated, its base class members must be initialized
in addition to its own members
- The base class constructor is called to initialize the base class members
of the derived class object. The base class constructor, similar to component
objects in a composite class, is called before the derived class object's
constructor. Destructors are called in the reverse order.
Example...
- In case of a derived class that also contains other class objects as
components, in addition to inheriting its base class members, the base class
constructor is called first. Next constructors of each component class are
invoked. Finally, the constructor of the derived class is invoked.
Once again, destructors are called in the reverse order
- Argument values may be passed to the base class and derived class constructors.
Member initializer lists must be used to accomplish this.
Example...
- In a multi-level inheritance hierarchy, member initializer lists are used
to initialize each derived class's base class. In other words each derived class
is responsible for the initialization of its base class.
Example...
Back to Previous Page
Document:
Last Modified On: