第一个所谓的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;
}
#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;
}
相关文章推荐
- C++中接口与实现分离
- C++中接口与实现分离的技术
- C++中接口与实现分离的技术
- C++中接口与实现分离的技术 ZZ
- 再论C++中接口与实现分离的技术
- C++中接口与实现分离的技术
- 接口和实现分离(C++)
- 体验C++中接口与实现分离的技术
- C++中接口与实现分离的技术
- C++中接口与实现分离的技术
- C++ 接口与实现分离技术---如何将文件间的编译关系降至最低
- C++中接口与实现分离的技术
- c++:成员对象,基类初始化列表 接口与实现分离
- C++中接口与实现分离的技术
- C++中接口与实现分离的技术
- C++接口与实现分离的2种方法
- 『C/C++』一个用C/C++分别实现接口与实现相分离的设计原则的例子
- C++中接口与实现分离的技术
- 体验C++中接口与实现分离的技术
- c++程序书写规范----接口与实现的分离