main.cpp
1 #include "Stack.h" 2 3 #include4 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 = 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 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:
1 #ifndef STACK_H 2 #define STACK_H 3 4 #include5 6 template 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 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 *head; 57 58 size_t sz; // size, to avoid name conflict with Stack::size() 59 60 }; 61 62 template 63 Stack ::Stack(): head(NULL), sz(0) {} 64 65 template 66 Stack ::~Stack() { 67 Link *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 84 void Stack ::push(const T &val) 85 { 86 head = new Link (val, head); 87 ++sz; 88 } 89 /** 90 @return a reference to the top element in the stack 91 */ 92 template 93 T& Stack ::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 stack102 */103 template 104 const T& Stack ::top() const105 {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 115 void Stack ::pop()116 {117 if (head == NULL)118 throw std::runtime_error("empty stack");119 Link *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 129 size_t Stack ::size() const {130 return sz;131 }132 133 template 134 bool Stack ::empty() const135 {136 return (sz == 0);137 }138 139 #endif // STACK_H