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

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

  1. Explain how inheritance may jeopardize encapsulation. Can you think of a solution?
  2. Give an example of a class allowing external clients access to private data.
  3. 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.