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
- encapsulation -- constructors, const, conversions, friends
- inheritance -- abstract and concrete shapes
- assertions -- pre- and post-conditions
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
- Explain the role of constructors.
What role do destructors play?
- What is the meaning of const? Give some examples.
- Characterize the two kinds of type conversion supported by C++.
- 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.