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
- virtual functions -- scoping, recursion
- invariant properties -- hidden features
- the diamond structure -- hiding realizations
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
- Discuss the interplay between scoping and recursion
for virtual functions.
- 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.
- 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.