博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ simple class template example: Stack
阅读量:5019 次
发布时间:2019-06-12

本文共 4334 字,大约阅读时间需要 14 分钟。

main.cpp

1 #include "Stack.h" 2  3 #include 
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 = 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 #include 
5 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

 

转载于:https://www.cnblogs.com/qrlozte/p/4108911.html

你可能感兴趣的文章
Leetcode-5051 Valid Boomerang(有效的回旋镖)
查看>>
Codeforces Gym 100513M M. Variable Shadowing 暴力
查看>>
Codeforces Gym 100015G Guessing Game 差分约束
查看>>
使用常量 变量 时间上的区别
查看>>
大脑皮层是如何工作的 《人工智能的未来》(<On intelligence>)读书笔记
查看>>
重力感应操控(unity iphone)
查看>>
JDBC
查看>>
HTTP错误500.19 - Internal Server Error解决方法
查看>>
python的协程和_IO操作
查看>>
浅谈 Mybatis中的 ${ } 和 #{ }的区别
查看>>
CNN 笔记
查看>>
版本更新
查看>>
SQL 单引号转义
查看>>
start
查看>>
实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端
查看>>
PHP socket客户端长连接
查看>>
7、shell函数
查看>>
【转】Apache Jmeter发送post请求
查看>>
Nginx 基本 安装..
查看>>
关于WPF中Popup控件的小记
查看>>