编译器编译类的过程:
1.扫描类名
2.扫描所有的成员名称,原型,不编译方法体
3.最后扫描所有的方法体和形参的默认值
class CSqstack
{
public:
CSqstack();
~CSqstack();
int pop();
void push(int data);
int top(); //获取栈顶元素
void show();
bool empty(){ return _top == -1; };
bool full(){ return _top == EN_STACK - 1; };
private:
enum{EN_STACK = 10}; //枚举本身是常量
int _stack[EN_STACK];
int _top;
};
CSqstack::CSqstack()
{
_top = -1;
}
void CSqstack::push(int data)
{
if (full())
return;
_stack[++_top] = data;
}
int CSqstack::pop()
{
if (empty())
throw "";
return _stack[_top--];
}
void CSqstack::show()
{
if (empty())
throw "";
for (int i = 0; i <= _top; i++)
{
cout << _stack[i] << " ";
}
cout << endl;
}
CSqstack::~CSqstack()
{
}
int CSqstack::top()
{
if (empty())
throw "";
return _stack[_top];
}
int main()
{
//客户代码使用的时候不需要知道如何实现的
CSqstack s1;
for (int i = 0; i < 15; i++)
{
s1.push(i);
}
cout<<s1.pop()<<endl;
s1.show();
s1.pop();
s1.pop();
s1.push(100);
s1.push(999);
s1.show();
return 0;
}
执行结果:
9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 100 999
请按任意键继续. . .