Tuesday 19 May 2015

c++ program to demonstrate the usage of overriding of functions

#include<iostream>
using namespace std;
int n,s[20],top=-1;
class stack1
{
        protected:int item,del;
        public: void push()
                {
                        top=top++;
                        cout<<"enter item to be inserted"<<endl;
                        cin>>item;
                        s[top]=item;
                }
                void pop()
                {
                        del=s[top];
                        cout<<"the deleted item is"<<del;
                        top=top--;
                }
                void display()
                {
                        int i;
                        cout<<"the stack is\n";
                        for(i=top;i>=0;i--)

                        cout<<s[i]<<"\t";

                }
};
class stack2 : public stack1
{
        public:
                void push()
                {
                        if(top==n-1)
                        {
                                cout<<"stack is full"<<endl;
                                return;
                        }

                        stack1::push();
                }
                void pop()
                {
                        if(top==-1)
                        {
                                cout<<"stack empty"<<endl;
                                return;
                        }

                        stack1::pop();
                }
                void display()
                {
                        if(top==-1)
                        {
                                cout<<"stack empty"<<endl;
                                return;
                        }
                                        stack1::display();
                }
};
int main()
{
        int ch;
        stack2 s2;
        cout<<"enter stack size\n";
        cin>>n;
        for(;;)
        { cout<<"\n1.push\n 2.pop\n 3.display\n enter your choice\n";
          cin>>ch;
                switch(ch)
                {
                        case 1 : s2.push();
                                 break;
                        case 2 : s2.pop();
                                 break;
                        case 3 : s2.display();
                                 break;
                        default : exit(0);
                }
        }
return 0;
}

No comments:

Post a Comment