1 / 10

A Simple Stack Class Templates

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.

devi
Download Presentation

A Simple Stack Class Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Simple Stack ClassTemplates Eric Roberts CS 106B April 27, 2009

  2. 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.

  3. 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 {

  4. 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 {

  5. /* * 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();

  6. 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();

  7. 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

  8. 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:

  9. 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.

  10. The End

More Related