Stack implementation with operator overloading in C++

#include <conio.h>
#include <iostream.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
 
int  top=0;
 
//class definition
 
class stack
{
    public:
	    int a[];
	    int n;
 
	    stack ();
	    stack operator << (int);
	    stack operator +  (int);
	    stack operator --(void);
}r;
 
//definig the constructor to   initialize
 
    stack :: stack ()
    {
	    clrscr();
	    cout <<"Enter the size of the stack: ";
	    cin >> r.n;
 
	    a[n];
	    top=0;
     }
 
//operator to print
     stack stack::operator << (int top)
     {
	    clrscr();
 
	    if(top == 0)
	    {
		 cout<<"The stack is empty\n";
		 //getch();
	    }
 
	    else
	    {
		 cout<<"The contents of the stack...\n";
 
		 for(int i=top; i>=1; i--)
		 {
			cout<< "------\n";
			cout<< "| " << r.a[i] <<" |\n";
			cout<< "------\n";
		 }
	    }
 
	    return(r);
    }
 
//operator to push to the stack
    stack stack::operator + (int z)
    {
	    if(top == r.n)
	    {
		  cout<<"THE STACK IS FULL YOU CANNOT PUSH..!!\n";
		  getch();
	    }
	    else
	    {
		  top++;
		  a[top] = z;
 
		  r << top;
	    }
 
	    return(r);
     }
 
//operator to pop from	the stack
     stack stack::operator --(void)
     {
	  if(top==0)
	  {
		cout<<"THE STACK IS EMPTY SO CANNOT POP..!!";
	       //getch();
	  }
	  else
	  {
	       cout <<"Element poped is  |" << r.a[top];
	       top = top-1;
	       r << top;
	  }
	  return(r);
     }
 
 
    // main function
 
     void main()
     {
	   int choice;
	   char ch='y';
 
	   while(ch=='Y'||ch=='y')
	   {
		 clrscr();
 
		 cout<<"*** STACK OPERATIONS ***\n" << endl;
		 cout<<" 1-> PUSH    \n";
		 cout<<" 2-> POP     \n";
		 cout<<" 3-> DISPLAY \n";
		 cout<<" 4-> EXIT    \n";
 
		 cin>>choice;
 
		 switch(choice)
		 {
		     case 1:
			    {  int z;
 
			       cout<<"\nEnter the element to be pushed: ";
			       cin>> z;
 
			       r = r + z;  // + operator is overloaded
			       r << top;
 
			       //getch();
			    }
			    break;
 
		     case 2:
			    {
			       //clrscr();
			       r = --r;   // --operator is overloaded
			       r << top;
 
			       (flushall());
			       //getch();
			    }
			    break;
 
		    case 3:
			    {
			       r << top; // << operator is overloaded
 
			       (flushall());
			       //getch();
			    }
			    break;
 
		   case 4:
			    cout<<"IF EXHAUSTED TRY LATER..!!!!";
			    break;
 
		   default:
			    {
			     cout<<"INVALID CHOICE...!!\n";
			     cout<<"Enter choice between 1 and 4 \n";
			    }
			    break;
		}
 
		cout<<"\nDo you want to continue [y/n]?";
		cin>>ch;
	 }
     }

Enter the size of the stack:3
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed: 56
The contents of the stack…
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed: 67
The contents of the stack…
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed: 50
The contents of the stack…
——
| 50 |
——
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed:90
THE STACK IS FULL YOU CANNOT PUSH..!!

The contents of the stack…
——
| 50 |
——
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
2

The contents of the stack…
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
2

The contents of the stack…
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
2

Stack is empty

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
3

Stack is empty

Do you want to continue [y/n]?n

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.