C++ program to illustrate Function Overriding

//function with same name and same signature in base class as well as derived class

#include <iostream>
using namespace std;
class Base
{
    protected:
        int x;
    public:
        void read()
        {
            cout<<"You are in Base Class."<<endl;
            cout<<"Enter x: ";
            cin>>x;
        }
        void show()
        {
            cout<<"You are in Base Class."<<endl;
            cout<<"The entered x is "<<x;
        }
};
class Derived:public Base
{
    protected:
        int y;
    public:
        void read()
        {
            cout<<"You are in Derived Class."<<endl;
            cout<<"Enter x and y: ";
            cin>>x>>y;
        }
        void show()
        {
            cout<<"You are in Derived Class."<<endl;
           
cout<<"The entered x and y are "<<x<<" and "<<y<<"."<<endl;
        }
};
int main()
{
    Derived d;
    d.read();
    d.show();
    //to call base class
    d.Base::read();
    d.Base::show();
    return 0;
}

No comments:

Post a Comment