
模板类以这样的代码开头:template
template
class看作是变量的类型名,该变量接受类型作为其值,把Type看作是该变量的名称。
将模板信息放在一个头文件中,建立stacktp.h
NetShopForge是一款强劲的B2C的网上购物软件,利用她我们能建立起强劲的、自由的、安全的购物平台。 维博软件以有这样的软件无比自豪,系统基于ASP.NET 2.0及SqlServer开发,充分享受新技术带来的乐趣。 软件综合了卖家,买家,程序员,设计者的头脑风暴,目的就是用户能建立风格不同的电子商务系统,使它显得更加与众不同。 如果您寻求一款能按您的思想随意发挥的网上购物软件,那么Net
#ifndef STACKTP_H_ #define STACKTP_H_ // 建立模板 templateclass 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=10; } template bool Stack ::isempty() { return top==0; } template bool Stack ::isfull() { return top==MAX; } template bool Stack ::push(const Type &item) { if(top 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 namespace std; int main() { Stack st;// 创建一个空的stack,和前面的模板联系起来 char ch; string po; cout<<"Please enter A to add a purchase order.\n" <<"P to precess a PO,or Q to quit."< >ch && toupper(ch)!='Q' ) { while(cin.get()!='\n') { continue; } if(!isalpha(ch)) { cout<<'\a'; continue; } switch(ch) { case 'A': case 'a':cout<<"Enter a PO number to add:"< >po; if(st.isfull()) { cout<<"stack already full"<









