// Stack in C++ language #include using namespace std; #include "Person.hpp" // access Person class class Stack { private: // list all privates const static int EMPTY_TOS = -1; // constant TOS const static int STACK_SIZE = 100; // constant array dimension int top; // current top, hidden in Stack Person *stack[STACK_SIZE]; // stack array, hidden in Stack public: // list all public prototypes Stack(); // constructor void push(Person*); // push a Person onto Stack Person* pop(); // pop a Person from the Stack int isEmpty(); // is it empty? int isFull(); // is it full? }; Stack::Stack() { // constructor implementation, init top top = EMPTY_TOS; } void Stack::push(Person* p) { // push implementation if (!isFull()) // here is the code is similar to C stack[++top] = p; } Person* Stack::pop() { // pop implementation if (!isEmpty()) return stack[top--]; return NULL; } int Stack::isEmpty() { // isEmpty implementation return top == EMPTY_TOS; } int Stack::isFull() { // isFull implementation return top == STACK_SIZE-1; } int main(int argc, char *argv[]){ Stack* s = new Stack(); // construct a Stack s->push(new Person("Name 1",10)); // push 3 Persons onto stack s->push(new Person("Name 2",20)); // note: C did push(s,p) rather than s->push(p) s->push(new Person("Name 3",30)); while (!s->isEmpty()) // empty the Stack cout << (s->pop())->toString() << endl; // display contents }