Objectives

This section introduces the notion of generic types. Generic types come in two flavors, as polymorphic types that result from inheritance; and as template classes. The void* pointer may be considered as a degenerate form of a generic type. The examples of a generic list and a generic stack are used to illustrate the differences between the two approaches.

Points to emphasize

Possible difficulties

The use of void* as a generic type pointer requires some understanding of the notion of types as constraints. You may also need to explain the use of typedef.

Hints

Explain the meaning of a type as the characterization of a set of individual elements. Subtypes delimit a subset thereof. The intuition is that the more information you get, the more precisely may you delimit the set of individuals belonging to the type.

Question

  1. What are generic types? Why are they useful? Explain how C++ supports generic types. Give an example.

Comments

You may comment on the fact that version 2.0 of C++ does not support template classes, whereas parametrized classes were available in Eiffel from the start. In fact, many C++ libraries still offer generic classes as macros. See section libraries.