博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Primer Plus学习笔记 第十四章 类模板
阅读量:4127 次
发布时间:2019-05-25

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

跟函数模板差不多 我直接上代码吧

stacktp.h

#ifndef STACKTP_H_#define STACKTP_H_template
class Stack{ private: enum {MAX = 10}; Type items[MAX]; int top; public: Stack(); bool isempty(); bool isfull(); bool push(const Type & item); bool pop(Type & item);};template
Stack
::Stack(){ top = 0;}template
bool Stack
::isempty(){ return top == 0;}template
bool Stack
::isfull(){ return top == MAX;}template
bool Stack
::push(const Type & item){ if (top < MAX) { items[top++] = item; return true; } else { return false; }}template
bool Stack
::pop(Type & item){ if (top > 0) { item = items[--top]; return true; } else return false;}#endif

stacktem.cpp

#include 
#include
#include
#include "stacktp.h"using std::cin;using std::cout;int main(){ Stack
st; char ch; std::string po; cout << "Please enter A to add a purchase order,\n" << "P to process a PO, or Q to quit.\n"; while(cin >> ch && std::toupper(ch) != 'Q') { while (cin.get() != '\n') continue; if (!std::isalpha(ch)) { cout << 'a'; continue; } switch(ch) { case 'A': case 'a': cout << "Enter a PO number to add: "; cin >> po; if(st.isfull()) cout << "stack already full\n"; else st.push(po); break; case 'p': case 'P': if (st.isempty()) cout << "stack already empty\n"; else { st.pop(po); cout << "PO #" << po << " popped\n"; break; } } cout << "Please enter A to add a purchase order,\n" << "P to process a PO, or Q to quit.\n"; } cout << "Bye\n"; return 0;}

这不适用于指针

关于指针栈明天说 

 

转载地址:http://enepi.baihongyu.com/

你可能感兴趣的文章
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>
hdu 3788 hdoj 3788
查看>>
zju 1003 zoj 1003
查看>>
zju 1004 zoj 1004
查看>>
zju 1005 zoj 1005
查看>>
zju 1006 zoj 1006
查看>>
【虚拟机】虚拟化架构与系统部署(Windows系统安装)
查看>>
字节跳动安卓开发实习生面试分享
查看>>
好书分享之——《能力陷进》
查看>>
阅读笔记《c++ primer》
查看>>
阅读笔记《C++标准程序库》
查看>>
基于mirror driver的windows屏幕录像
查看>>
C语言8
查看>>
Qt实现简单延时
查看>>
qml有关矩形说明
查看>>
在qt中使用QSplitter设置初始比例setStretchFactor失效的解决方法
查看>>