缺省函数主要包括:拷贝构造函数、析构函数、构造函数、赋值语句
构造函数:创建对象,并对对象初始化;有返回值
析构函数:释放;有返回值;
对于面向对象来说:空间与对象是分离的;
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
template<class Type>
class SeqStack
{
private:
Type* data;
int maxsize;
int top;
public:
SeqStack(int sz = 100) :maxsize(sz), top(-1)
{
data = (Type*)malloc(sizeof(Type) * maxsize);
if (data == NULL)exit(1);
}
~SeqStack()
{
free(data);
data = NULL;
maxsize = 0;
top = -1;
}
int GetSize() const
{
return top + 1;
}
bool Is_Empty() const
{
return GetSize() == 0;
}
bool Is_Full() const
{
return GetSize() == maxsize;
}
bool Push(const Type& x)
{
if (Is_Full())return false;
data[++top] = x;
return true;
}
Type& GetTop()
{
return data[top];
}
const Type& GetTop()const
{
return data[top];
}
void Pop()
{
--top;
}
void Clear()
{
top = -1;
}
};
int main()
{
SeqStack <int> ist = SeqStack<int>();
ist.Push(12);
ist.Push(23);
ist.Push(34);
ist.Push(45);
while (!ist.Is_Empty())
{
int x = ist.GetTop();
ist.Pop();
cout << x << endl;
}
return 0;
}