C++程序,请朋友帮忙解释一下!

来源:百度知道 编辑:UC知道 时间:2024/07/07 20:40:25
#include<iostream>
#include<stdio.h>

using namespace std;

template<class T>
class stack
{
public:
stack(int t=0);
void push(T a);
T pop();

protected:

private:
T stacks[100];
int topNum;
};

template<class T>
stack<T>::stack(int t)
{
topNum=t;
}

template<class T>
void stack<T>::push(T a)
{
if (topNum==100)
{
cout << "stack is full.\n";
}
else
{
stacks[topNum]=a;
++topNum;
}
return;

}

template<class T>
T stack<T>::pop()
{
if(topNum==0)
{
cout << "stack is empty.\n";

}
else
{
--topNum;
}
return stacks[topNum];
}

void main(void)
{
stack <int> s1,s2; //实例

这是用模板来实现一个"stack"类,即栈类
类可以理解为用户自定义类型,它将数据表示和操纵数据的方法集合在一起,类的实例化就是对象.(好像很笼统~~)
push()是用来把一个元素压入栈顶.
如果不知道栈是什么,建议你去看一下数据结构的书