Defining a Class Template
Let’s use the Stack
class from Chapter 10 as a model from which to build a template. Here’s the original class declaration:
typedef unsigned long Item;class Stack{private: enum {MAX = 10}; // constant specific to class Item items[MAX]; // holds stack items int top; // index for top stack itempublic: Stack(); bool isempty() const; bool isfull() const; // push() returns false if stack already is full, true otherwise bool push(const Item & item); // add item to stack // pop() returns false if stack already is empty, true otherwise bool pop(Item & item); // pop top into item};
The template approach will replace the Stack
definition with a template definition and the ...
Get C++ Primer Plus 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.