Objectives

This section deals with the language support for encapsulation and inheritance in C++. Support for encapsulation includes aspects such as access protection, widening and narrowing conversions, overloading and friends. Simple examples are used to illustrate these concepts. The support for inheritance in C++ is illustrated by a hierarchy of graphical shapes. In addition, we show how to develop robust code by using assertions.

Points to emphasize

Possible difficulties

Some knowledge, and preferably experience with C++, is required to appreciate the examples. In particular the notion of const, which may be used to protect the result of a member function and as a means by which to characterize a member function as safe, may need some additional study.

Questions

  1. Explain the role of constructors. What role do destructors play?
  2. What is the meaning of const? Give some examples.
  3. Characterize the two kinds of type conversion supported by C++.
  4. Why do you need friends?

Comments

Undoubtly, C++ has many features. In class, I relate the complexity of C++ to the common opinion that small is beautiful. There is a dual perspective here; the size and complexity of a language on the one hand, and the size and complexity of the programs written in that language on the other.