Class Templates

Inheritance (public, private, or protected) and containment aren't always the answer to a desire to reuse code. Consider, for example, the Stack class (Chapter 10), the Queue class (Chapter 12), and the ArrayDb class (this chapter). These are all examples of container classes, which are classes designed to hold other objects or data types. The Stack class, for example, stored unsigned long values. You could just as easily define a stack class for storing double values or String objects. The code would be identical other than for the type of object stored. However, rather than writing new class declarations, it would be nice if you could define a stack in a generic (that is, type-independent) fashion and then provide a specific ...

Get C++ Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.