您的位置:首页 > 编程语言 > C语言/C++

第一个所谓的c++接口与实现的分离

2014-03-31 19:50 344 查看
Stack.h

#ifndef Stack1_h

#define Stack1_h

class Stack1

{

private:

int *data;

int count;

int size;

public:

Stack1(int stacksize=10);

~Stack1();

void Push(int x);

int Pop();

int howmany();

};

#endif

stack.cpp

#include "Stack.h"

#include <iostream>

using namespace std;

Stack1::Stack1(int stacksize)

{

int i;

if(stacksize>0)

{

size=stacksize;

data=new int[stacksize];

for(i=0;i<size;i++)

data[i]=0;

}

else

{

data=0;

size=0;

}

count=0;

}

Stack1::~Stack1()

{

delete []data;

}

void Stack1::Push(int x)

{

if(count<size)

{

data[count]=x;

count++;

}

else

cout << "栈已满,不能再压入了!" << endl;

}

int Stack1::Pop()

{

if(count<=0)

{

cout << "堆栈已空" << endl;

}

count--;

return data[count];

}

int Stack1::howmany()

{

return count;

}

main

#include <iostream>

#include "Stack.h"

using namespace std;

int main()

{

Stack1 s1;

s1.Push(1);

s1.Push(2);

s1.Push(3);

s1.Push(4);

int x1=s1.Pop();

int x2=s1.Pop();

int x3=s1.Pop();

int x4=s1.Pop();

cout << x1 << "\t" << x2 << endl;

cout << x3 << "\t" << x4 << endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: