Function Overriding:

       If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding

Requirements for Overriding:

  • Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.
  • Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.

Example:

#include<iostream.h>

#include<conio.h>

class Base

{
    protected:
        int a,b;
    public:
        void getdata()
        {
            cout<<"enter two values:";
            cin>>a>>b;
        }
        void disp()
        {
            cout<<"The value are:"<<a<<" "<<b;
        }

};
class Sub : public Base
{
    prootected:
        int c,d;
    public:
        void getdata()
        {
            cout<<"enter four values:";
            cin>>a>>b>>c>>d;
        }
        void disp()
        {
            cout<<"The value are:"<<a<<" "<<b<<" "<<c<<" "<<d;
        }
};
void main()
{
    clrscr();
    Sub s1;
    s1.getdata();
    s1.disp();
    getch();
    
}