Inheritance

Instructor's Guide
intro, inheritance, delegation, cooperation, events,

Objectives

This section highlights some of the issues and problems that may arise when using inheritance. It deals with the interplay between virtual functions and scoping, how to maintain invariance when refining classes by inheritance and the diamond structure for realizing compound objects. Attention is paid, in particular, to the problem of preserving behavioral invariance properties in derived classes.

Points to emphasize

Hints

Overriding virtual functions results, from an operational viewpoint, in dispatching via the virtual function table. Ask some questions to establish that your students are able to predict the behavior of an instance of derived classes.

Questions

  1. Discuss the interplay between scoping and recursion for virtual functions.
  2. How would you express the invariant behavior of an employee class? Explain what problems may occur when adding a permanent bonus to the salary of a derived manager class.
  3. Explain how compound objects may be constructed by employing the diamond structure.

Comments

The diamond structure provides a useful idiom for separating the logical behavior of an object from its realization. When employing virtual constructors the realization may be completely hidden behind a stable interface.