Objectives
This section reflects on the benefits
and pitfalls
of the use of encapsulation and inheritance
as supported in C++.
In particular, it discusses the distinction
between objects, pointers and references
and gives an example of how protection
may unvoluntarily be violated.
Further, it discusses the potential
benefits and cost of inheritance.
Also, the behavior of constructors and destructors,
which play an important role in memory management, is described.
Points to emphasize
- protection -- in relation with inheritance
- object, pointers, references -- the
sneaky example
- inheritance -- advantages and disadvantages
- memory management -- constructors and destructors
Hints
The relation between encapsulation and
inheritance can best be explained by
distinguishing between ordinary clients
and descendant clients, which are instances
of derived classes.
You may discuss the issues concerning
the cost of inheritance according to your taste.
Questions
- Explain how inheritance may jeopardize
encapsulation.
Can you think of a solution?
- Give an example of a class allowing external
clients access to private data.
- Discuss the advantages and disadvantages
of inheritance.
Comments
The topics discussed in this section
require some knowledge of the material
presented in the appendices.
This section and the following section,
discussing extensions to the basic object
model, introduce issues that will
be developed more fully later on.