C++

C++ Function Overriding

In this article, we will cover function overriding in the C++ programming language. C++ supports runtime polymorphism.

In function overriding, the child class redefines the function which is already defined in its parent class.

It enables the programmer to provide derived class-specific functionality (even though it is defined in the base class).

Now, we will see a few examples and understand the function overriding concept in C++.

Example-1

I have defined two classes in the below example code – a base class and a derived class. The base class has a member function, i.e., disp(). The “Derived_Class” is inherited from the “Base_Class.” The “disp()” function is present in “Base_Class” and then redefined in the “Derived_Class.”

In the main() function, an object of Derived_Class, i.e., “d,” is created. Then, we call the disp() function on the derived class object, so the function in the derived class will be invoked. The base class version is ignored here. You can see below the output as – “disp() function in the derived class.”

#include <iostream>

using namespace std;

//base class
class Base_Class
{
public:
    void disp()
    {
        cout << "disp() function in base class." << endl;
    }
   
};

//derived class
class Derived_Class:public Base_Class
{
public:
    void disp()
    {
        cout << "disp() function in derived class" << endl;
    }
};

int main()
{
    Derived_Class d;
    d.disp();       //disp() function on derived class object
        return 0;
}

Example-2

In this example, we will see how to deliberately call the base class version of the function through the derived class object. In the derived class function definition, we use the scope resolution operator [::] to call the base class version of the function.

#include <iostream>

using namespace std;

class Base_Class
{
public:
    void disp()
    {
        cout << "disp() function in base class." << endl;
    }
   
};

class Derived_Class:public Base_Class
{
public:
    void disp()
    {
        Base_Class::disp(); //Call the base class version of disp()
    }
};

int main()
{
    Derived_Class d;
    d.disp();
        return 0;
}

Example-3

This is another example of function overriding. In this example, we have created a parent class – Animal and two derived classes – Duck and Dog. The base class, i.e., the Animal class, has a member function sound().

In the derived class/child class, i.e., Dog, we have redefined the same function, i.e., sound(), to override its base class definition. Similarly, in the other derived class, i.e., Duck, we have redefined the same function, i.e., sound().

In the main() function, we have created the “dog” object of “Dog” and “duck” of “Duck.” So, now when we call the sound() function for dog and duck, the derived class version of the sound() function will call. You can see the output of the program as shown below. So, with the help of the function overriding, we can invoke the derived class-specific function.

#include <iostream>

using namespace std;

class Animal
{
public:
    void sound()
    {
        cout << "Animal sound!" << endl;
    }
   
};

//Derived class – Dog class
class Dog: public Animal
{
public:
    void sound()
    {
        cout << "Dog sound - bark." << endl;
    }
};

//Derived class – Duck class
class Duck: public Animal
{
public:
    void sound()
    {
        cout << "Duck sound - quack." << endl;
    }
};


int main()
{
    Dog dog;
    Duck duck;

    dog.sound();    //sound() of dog
    duck.sound();   //sound() of duck
   
        return 0;
}

Conclusion

In this article, I have explained function overriding in C++. The C++ language supports runtime polymorphism. Function overriding helps to achieve the run time polymorphism in C++. This article looked at the concept of function overriding and how to achieve runtime polymorphism using function overriding.

About the author

Sukumar Paul

I am a passionate software engineer and blogger. I have done my Masters in Software Engineering from BITS PILANI University, India. I have very good experience in real-time software development and testing using C, C++, and Python. Follow me at thecloudstrap.com.