24.5 Class Templates

I l @ ve RuBoard

Class templates are a little more complex than function templates. Declaring them is easy; they are defined just like function templates. Example 24-2 shows the stack class from Chapter 13, written as a template.

Example 24-2. max-t/stack1.cpp
 #include <cstdlib> #include <iostream> #include <assert.h> const int STACK_SIZE = 100;     // Maximum size of a stack /********************************************************  * Stack class                                          *  *                                                      *  * Member functions                                     *  *      stack -- initalize the stack.                   *  *      push -- put an item on the stack.               *  *      pop -- remove an item from the stack.           *  ********************************************************/ // The stack itself template<typename kind> class stack {     private:         int count;              // Number of items in the stack         kind data[STACK_SIZE];  // The items themselves     public:         // Initialize the stack         stack(  ) {             count = 0;  // Zero the stack         }         // Push an item on the stack         void push(const kind item) {             assert(count >= 0);             assert(count < sizeof(data)/sizeof(data[0]));             data[count] = item;             ++count;         }         // Pop an item from the stack         kind pop(  ) {             // Stack goes down by one             --count;             assert(count >= 0);             assert(count < sizeof(data)/sizeof(data[0]));             // Then we return the top value             return (data[count]);         } }; 

There is a problem, however. To use this class we need to declare an instance of it. In the past, we've been able to declare a stack with the statement:

 stack a_stack;    // This won't work 

The problem is that stack is now a generic template. The stack can now contain anything. When C++ sees this declaration, it's going to ask, "A stack of what?" We must specify the type of data we are storing. The new declaration is:

 stack<int> a_stack;    // A stack of integers 

The <int> tells C++ to use int for kind throughout the stack. We can now use the new class variable:

 a_stack.push(1); x = a_stack.pop(  ); 

In the stack class, we defined all the member functions inside the class definition. We could just as well have specified the procedures outside the class. To do so, we must put the template clause template<class kind> in front of each procedure and put the template parameter ( <kind> ) in the name of the class. For example, the push routine would look like this:

 /********************************************************  * stack::push -- push an item on the stack             *  *                                                      *  * Warning: We do not check for overflow                *  *                                                      *  * Parameters                                           *  *      item -- item to put on the stack                *  ********************************************************/ template<typename kind> inline void stack<kind>::push(const kind item) {     assert(count >= 0);     assert(count < sizeof(data)/sizeof(data[0]));     data[count] = item;     ++count; } 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net