c++ simple class template example: Stack

main.cpp

 1 #include "Stack.h"
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Box {
 8 public:
 9     Box():data(0), ID(num++) { cout << "Box" << ID << " cons" << endl; }
10     // Notice that copy constructor and operator= must be implemented in a pairwise way.
11     // Because if you need Copy constructor, you almost definitely need to implement operator=
12     Box(const Box &copy): data(copy.data), ID(num++) { cout << "Box" << ID << " copy cons" << endl; }
13     Box& operator=(const Box &b)
14     {
15         this->data = b.data;
16         return *this;
17     }
18     ~Box() { cout << "Box" << ID << " des" << endl; }
19     int data;
20 private:
21     static int num;
22     const int ID;
23 };
24 
25 int Box::num = 1;
26 
27 int main()
28 {
29     Box b1,b2,b3;
30     b1.data = 1;
31     b2.data = 2;
32     b3.data = 3;
33     Stack<Box> bstack;
34     bstack.push(b1);
35     bstack.push(b2);
36     bstack.push(b3);
37     while (!bstack.empty()) {
38         cout << bstack.top().data << endl;
39         bstack.pop();
40     }
41     return 0;
42 }

 

Stack.h // Why there's no cpp file for Stack to hide implementation? See: http://www.cnblogs.com/qrlozte/p/4108807.html

  1 #ifndef STACK_H
  2 #define STACK_H
  3 
  4 #include <stdexcept>
  5 
  6 template <typename T>
  7 class Stack
  8 {
  9     public:
 10         Stack();
 11         ~Stack();
 12         /**
 13             Inserts a new element at the top of the stack,
 14             above its current top element.
 15             The content of this new element is
 16             initialized to a copy of val.
 17             @param val value to which the inserted element is initialized
 18         */
 19         void push(const T &val);
 20         /**
 21             @return a reference to the top element in the stack
 22         */
 23         T& top();
 24         /**
 25             @return a const reference to the top element in the stack
 26         */
 27         const T& top() const;
 28         /**
 29             Removes the element on top of the stack.
 30             This calls the removed element's destructor.
 31         */
 32         void pop();
 33         /**
 34             @return the number of elements in the stack.
 35         */
 36         size_t size() const;
 37 
 38         bool empty() const;
 39 
 40     private:
 41 
 42         template <typename TYPE>
 43         class Link {
 44             public:
 45 
 46                 TYPE data;
 47                 Link *next;
 48 
 49                 Link(const TYPE &_data, Link *_next = NULL): data(_data), next(_next) {}
 50                 ~Link() {
 51                     next = NULL;
 52                 }
 53 
 54         };
 55 
 56         Link<T> *head;
 57 
 58         size_t sz; // size, to avoid name conflict with Stack::size()
 59 
 60 };
 61 
 62 template <typename T>
 63 Stack<T>::Stack(): head(NULL), sz(0) {}
 64 
 65 template <typename T>
 66 Stack<T>::~Stack() {
 67     Link<T> *ptr = head;
 68     while (ptr != NULL) {
 69         ptr = head->next;
 70         delete head;
 71         head = ptr;
 72     }
 73     sz = 0;
 74 }
 75 
 76 /**
 77     Inserts a new element at the top of the stack,
 78     above its current top element.
 79     The content of this new element is
 80     initialized to a copy of val.
 81     @param val value to which the inserted element is initialized
 82 */
 83 template <typename T>
 84 void Stack<T>::push(const T &val)
 85 {
 86     head = new Link<T>(val, head);
 87     ++sz;
 88 }
 89 /**
 90     @return a reference to the top element in the stack
 91 */
 92 template <typename T>
 93 T& Stack<T>::top()
 94 {
 95     if (head == NULL)
 96         throw std::runtime_error("empty stack");
 97     return head->data;
 98 
 99 }
100 /**
101     @return a const reference to the top element in the stack
102 */
103 template <typename T>
104 const T& Stack<T>::top() const
105 {
106     if (head == NULL)
107         throw std::runtime_error("empty stack");
108     return head->data;
109 }
110 /**
111     Removes the element on top of the stack.
112     This calls the removed element's destructor.
113 */
114 template <typename T>
115 void Stack<T>::pop()
116 {
117     if (head == NULL)
118         throw std::runtime_error("empty stack");
119     Link<T> *ptr = head->next;
120     delete head;
121     head = ptr;
122     --sz;
123 }
124 
125 /**
126     @return the number of elements in the stack.
127 */
128 template <typename T>
129 size_t Stack<T>::size() const {
130     return sz;
131 }
132 
133 template <typename T>
134 bool Stack<T>::empty() const
135 {
136     return (sz == 0);
137 }
138 
139 #endif // STACK_H

 

posted @ 2014-11-19 18:16  rldts  阅读(362)  评论(0编辑  收藏  举报