template模板类使用

Stack.h

#ifndef STACK_H
#define STACK_H

template 
class Stack
{
public:
	//构造函数
	Stack();
	//判断是否为空
	bool isempty();
	//判断是否为满
	bool isfull();
	//插入数据
	bool push (const T &item);
	//删除数据
	bool pop  (T &item);

private:
	enum {MAX=10};
	T items[MAX];
	int top;
};

//构造函数
template 
Stack::Stack()
{
	top=0;
}

//判断是否为空
template 
bool Stack::isempty()
{
	return top==0;
}

//判断是否为满
template 
bool Stack::isfull()
{
	return top==MAX;
}

//插入数据
template 
bool Stack::push(const T &item)
{
	if (top
bool Stack::pop(T &item)
{
	if (top>0)
	{
		item=items[--top];
		return true;
	}
	else
		return false;
}


#endif


 

 

main.cpp

#include 
#include 
#include "Stack.h"
using namespace std;



int main (void)
{
	Stack st;
	char ch;
	string po;
	cout<<"Please enter A to add apurchase order,\n"
		<<"p to process aPO ,or Q to quit.\n"<>ch && toupper(ch) !='Q')
	{
		while(cin.get()!='\n') continue;
		if (!(ch))
		{
			cout<<'\a'<>po;
				if (st.isfull())
				{
					cout<<"stack already full"<


 

你可能感兴趣的:(C/C++)