首页 > 代码库 > c++ simple class template example: Stack
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 ©): data(copy.data), ID(num++) { cout << "Box" << ID << " copy cons" << endl; }13 Box& operator=(const Box &b)14 {15 this->data =http://www.mamicode.com/ 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 = http://www.mamicode.com/1;31 b2.data = http://www.mamicode.com/2;32 b3.data = http://www.mamicode.com/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): data(_data), next(NULL) {} 50 Link(const TYPE &_data, Link *_next): data(_data), next(_next) {} 51 ~Link() { 52 next = NULL; 53 } 54 55 }; 56 57 Link<T> *head; 58 59 size_t sz; // size, to avoid name conflict with Stack::size() 60 61 }; 62 63 template <typename T> 64 Stack<T>::Stack(): head(NULL), sz(0) {} 65 66 template <typename T> 67 Stack<T>::~Stack() { 68 Link<T> *ptr = head; 69 while (ptr != NULL) { 70 ptr = head->next; 71 delete head; 72 head = ptr; 73 } 74 sz = 0; 75 } 76 77 /** 78 Inserts a new element at the top of the stack, 79 above its current top element. 80 The content of this new element is 81 initialized to a copy of val. 82 @param val value to which the inserted element is initialized 83 */ 84 template <typename T> 85 void Stack<T>::push(const T &val) 86 { 87 head = new Link<T>(val, head); 88 ++sz; 89 } 90 /** 91 @return a reference to the top element in the stack 92 */ 93 template <typename T> 94 T& Stack<T>::top() 95 { 96 if (head == NULL) 97 throw std::runtime_error("empty stack"); 98 return head->data; 99 100 }101 /**102 @return a const reference to the top element in the stack103 */104 template <typename T>105 const T& Stack<T>::top() const106 {107 if (head == NULL)108 throw std::runtime_error("empty stack");109 return head->data;110 }111 /**112 Removes the element on top of the stack.113 This calls the removed element‘s destructor.114 */115 template <typename T>116 void Stack<T>::pop()117 {118 if (head == NULL)119 throw std::runtime_error("empty stack");120 Link<T> *ptr = head->next;121 delete head;122 head = ptr;123 --sz;124 }125 126 /**127 @return the number of elements in the stack.128 */129 template <typename T>130 size_t Stack<T>::size() const {131 return sz;132 }133 134 template <typename T>135 bool Stack<T>::empty() const136 {137 return (sz == 0);138 }139 140 #endif // STACK_H
c++ simple class template example: Stack
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。