100 likes | 270 Views
A Simple Stack Class Templates. Eric Roberts CS 106B April 27, 2009. stack.size(). stack.push(ch). Returns the number of characters pushed onto the stack. Pushes a new character onto the stack. stack.isEmpty(). stack.pop(). Returns true if the stack is empty.
E N D
A Simple Stack ClassTemplates Eric Roberts CS 106B April 27, 2009
stack.size() stack.push(ch) Returns the number of characters pushed onto the stack. Pushes a new character onto the stack. stack.isEmpty() stack.pop() Returns true if the stack is empty. Removes and returns the top character from the stack. stack.peek() stack.clear() Returns the top character from the stack without removing it. Removes all characters from the stack. Exercise: Define a Stack of Characters • Write a class that implements the basic stack functions for a stack that contains characters. This exercise will introduce several design issues that we will cover in more detail next week.
The charstack.h Interface /* * File: charstack.h * ----------------- * This interface defines the CharStack class, which implements * the stack abstraction for characters. */ #ifndef _charstack_h #define _charstack_h /* * Class: CharStack * ---------------- * This interface defines a class that models a stack of characters. * Characters are added and removed only from the top of the stack. * The fundamental stack operations are push (add to top) and pop * (remove from top). */ class CharStack {
public: /* * Constructor: CharStack * Usage: CharStack cstk; * ---------------------- * Initializes a new empty stack that can contain characters. */ CharStack(); /* * Destructor: ~CharStack * Usage: (usually implicit) * ------------------------- * Deallocates storage associated with this cstk. This method is * called whenever a CharStack instance variable is deallocated. */ ~CharStack(); The charstack.h Interface /* * File: charstack.h * ----------------- * This interface defines the CharStack class, which implements * the stack abstraction for characters. */ #ifndef _charstack_h #define _charstack_h /* * Class: CharStack * ---------------- * This interface defines a class that models a stack of characters. * Characters are added and removed only from the top of the stack. * The fundamental stack operations are push (add to top) and pop * (remove from top). */ class CharStack {
/* * Method: size * Usage: nElems = cstk.size(); * ---------------------------- * Returns the number of characters in this stack. */ int size(); /* . . . */ bool isEmpty(); /* . . . */ void clear(); /* . . . */ void push(char ch); /* . . . */ char pop(); /* . . . */ char peek(); The charstack.h Interface public: /* * Constructor: CharStack * Usage: CharStack cstk; * ---------------------- * Initializes a new empty stack that can contain characters. */ CharStack(); /* * Destructor: ~CharStack * Usage: (usually implicit) * ------------------------- * Deallocates storage associated with this cstk. This method is * called whenever a CharStack instance variable is deallocated. */ ~CharStack();
private: // Fill this in when you design the data structure }; #endif The charstack.h Interface /* * Method: size * Usage: nElems = cstk.size(); * ---------------------------- * Returns the number of characters in this stack. */ int size(); /* . . . */ bool isEmpty(); /* . . . */ void clear(); /* . . . */ void push(char ch); /* . . . */ char pop(); /* . . . */ char peek();
template <typename placeholder> where placeholder is an identifier that is used to stand for a specific type when the definition following the template specification is compiled. Templates • One of the most powerful features in C++ is the template facility, which makes it possible to define functions and classes that work for a variety of types. • The most common form of a template specification is
template <typename ElemType> void Sort(ElemType array[], int n) { for (int i = 0; i < n; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (array[j] < array[minIndex]) minIndex = j; } ElemType temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } } Templates in Functions • Templates can be used before a function definition to create a generic collection of functions that can be applied to values of various types. • The following code, for example, creates a template for invoking selection sort on arrays of any element type:
Templates in Class Definitions • Templates are more commonly used to define generic classes. When they are used in this way, the template keyword must appear before the class definition and before each of the implementations of the member functions. • The most inconvenient aspect of using templates to create generic classes is that the compiler cannot process them correctly unless it has access to both the interface and the implementation at the same time. • To emphasize the conceptual separation between the interface and the associated implementation, it is useful to use the #include keyword to add the code to the interface file without forcing the casual reader to pay attention to those details.