/* 4. Write a C++ program to create a class called as STACK using array of
integers. Implement the following operations by overloading the operators + and --. Also display the status and contents of the stack after each operation
by overloading the operator <<.
(i) s1=s1+element; where s1 is a object of the class STACK and element is an
integer to be pushed on top of the stack.
(ii)s1=s1--; where s1 is a object of the STACK, -- operator pops the element.
Handle the STACK empty and STACK full conditions. */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 5
class STACK
{
int top,a[MAX];
public:
STACK()
{
top = -1;
}
STACK operator +(int element);
STACK operator --();
friend ostream& operator <<(ostream& Out,STACK s);
};
STACK STACK::operator +(int item)
{
if (top >= MAX-1)
cout<<"\nERROR : Stack Overflow";
else
a[++top] = item;
return (*this);
}
STACK STACK::operator --()
{
if (top < 0)
cout<<"\nERROR : Stack Underflow";
else
cout<<"\nPopped item is "<<a[top--];
return (*this);
}
ostream& operator <<(ostream& Out,STACK s)
{
if (s.top == -1)
Out<<"\nERROR : Stack is empty";
else
{
Out<<"\nStack contents is as follows :\n";
for (int i=s.top; i>=0; i--)
Out<<s.a[i]<<endl;
}
return Out;
}
void main()
{
STACK s1;
int ch,element;
clrscr();
while(1)
{
cout<<"\nMENU\n1. Push\n2. Pop\n3. Display\n4. Exit\nYour Choice = ? ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter element to be pushed on to the stack : ";
cin>>element;
s1 = s1 + element;
break;
case 2:
s1 = s1--;
break;
case 3:
cout<<s1;
break;
case 4:
exit(0);
default:
cout<<"\nInvalid Choice. Try again";
}
}
getch();
}