Constructor & Destructor Tutorial in C++


// Using a constructor and destructor.
#include <iostream.h>
#include <conio.h>
#define SIZE 100
// This creates the class stack.
class stack
{
      int stck[SIZE];
      int tos;
      public:
      stack(); // constructor
      ~stack(); // destructor
      void push(int i);
      int pop();
};

// stack's constructor function
stack::stack()
{
      tos = 0;
      cout << "Stack Initialized\n";
}
// stack's destructor function
stack::~stack()
{
      cout << "Stack Destroyed\n";
}
void stack::push(int i)
{
      if(tos==SIZE) {
      cout << "Stack is full.\n";
      return;
}

      stck[tos] = i;
      tos++;
}

int stack::pop()
{
      if(tos==0)
      {
            cout << "Stack underflow.\n";
            return 0;
      }
      tos--;
      return stck[tos];
}

int main()
{
      clrscr();
      stack a, b; // create two stack objects
      a.push(1);
      b.push(2);
      a.push(3);
      b.push(4);
      cout << a.pop() << " ";
      cout << a.pop() << " ";
      cout << b.pop() << " ";
      cout << b.pop() << "\n";
      getch();
      return 0;
}

Custom Search