:-)
Code:
#include <iostream>
using namespace std;
namespace ansley {
class stack {
char *base;
int top;
int limit;
public:
explicit stack(int);
~stack();
void push(char);
char pop();
bool empty() const;
bool full() const;
};
stack::stack(int init_limit)
: limit(init_limit), top(0)
{
base = new char[limit];
}
stack::~stack()
{
delete [] base;
}
void stack::push(char new_item)
{
if (!full())
{
base[top++] = new_item;
}
else
{
cerr<<"Stack is full"<<endl;
}
}
char stack::pop()
{
if (!empty())
{
return base[--top];
}
else
{
cerr<<"Stack is empty"<<endl;
return 0; // Return a safe value
}
}
bool stack::empty() const
{
return top == 0;
}
bool stack::full() const
{
return top == limit;
}
}
int main()
{
ansley::stack mystack(3);
mystack.push('a');
mystack.push('b');
mystack.push('c');
mystack.push('d');
cout<< mystack.pop() <<endl;
cout<< mystack.pop() <<endl;
cout<< mystack.pop() <<endl;
cout<< mystack.pop() <<endl;
}