C++ Overriding

In a derived class, the data and function members can have the same name as of the base class. Process of creating members in the derived class with same name as that of the visible members of the base class is called overriding.

Unlike overloading, the functions in the derived class can have same function name and same number and type of arguments.

It is called overriding because the new name overrides( hides or displaces) the old name inherited from base class. Hence when the members are accessed with the overridden names in derived class or through the object of derived class, the derived class members are accessed.

Here is an example program to illustrate the overriding in C++.

//example program to illustrates the function overrding
#include<iostream>
using namespace std;
class base
{
protected:
    int num;
public:
    void readdata()
    {
        cout<<"Enter number in base: ";
        cin>>num;
    }
    void showdata()
    {
        cout<<"Number in base = "<<num<<endl;
    }
};

class derived: public base
{
private:
    int num;
public:
    void readdata()
    {
        cout<<"Enter number in derived: ";
        cin>>num;
    } void showdata()
    {
        cout<<"Number in derived = "<<num<<endl;
    }
};
int main()
{
    derived d1;
    d1.readdata();
    d1.showdata();
    return 0;
}

The output of the program is:

oputupHere only the function in derived class is called because it is overriding functions in base class.

 

Two ways to access the base class variable members in derived class

  • From member function of derived class
  • Form object of derived class

1. Base class member invocation from derived class
The following modification in derived class has to be made in order to utilize the inherited feature from the base class as well as new features added in derived class.

class derived: public base
{
private:
    int num;
public:
    void readdata()
    {
        base::readdata();
        cout<<"Enter number in derived: ";
        cin>>num;
    } void showdata()
    {
        base::showdata();
        cout<<"Number in derived = "<<num<<endl;
    }
};

2. Base class member invocation from object
The overridden base class functions can be accessed from the object of the derived class The following modification is required in main function.

int main()
{
    derived d1;
    d1.base::readdata(); //reads base::num 
    d1.readdata(); //reads derive class num 
    d1.base::showdata(); //displays base::num 
    d1.showdata(); 
    return 0; 
}

Overriding the function when functions are overloaded in base

If we override a function name in derived class which is in overloaded form in base class that non of the functions of base class are accessible after overriding without using the base class name i.e. base class name and scope resolution operator before function name has to be used.

//example of overriding the function when there is
//function overloading in base
#include<iostream>
using namespace std;
class base
{
public:
    void display(char c)
    {
        cout<<"display() in Base with char argument:"<<endl;
    } void display(int i)
    {
        cout<<"display() in Base with int argument:"<<endl;
    }
};
class derived:public base
{
public:
    void display()
    {
        cout<<"display() in derived without argument:"<<endl;
    }
};
int main()
{
    derived d1;
    d1.display(); //calls derived class display without argument
    //d1.display('a'); //error: function will be hidden after overriding
    //d1.display(5); //error: function will be hidden after overriding
    d1.base::display('a'); //calls base class display() with char argument
    d1.base::display(5); //calls base class display() with char argument
}
C++ Inheritance
C++ Inheritance Types