Function Overriding

Hello there! Hope got a good idea about inheritance in C++. In this article, we will learn Function Overriding concept in C++.

What is Function Overriding?

As we have already read, inheritance is an OOP concept that helps to inherit the features of one class into another. These classes are most commonly referred to as the base class (the one that is being inherited) and the derived class (the one which is inheriting the features).
 

Now, let us suppose that the same function is defined in both the derived class and the base class. If we call the function using the object of the derived class the function inside the derived class is called and executed.

This concept is known as Function Overloading in C++. The function defined in the derived class overrides the function in the base class.

Let us understand this better with an example:

// C++ program to demonstrate function overriding

#include <iostream>
using namespace std;

class Base {
   public:
    void print() {
        cout << "Base Function" << endl;
    }
};

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;
    }
};

int main() {
    Derived derived1;
    derived1.print();
    return 0;
}

Output:

Derived Function

In the above example, the same function is defined in both the base and the derived classes.

So, when we call the function with the derived class object, the function inside the derived class is executed by overriding the function of the base class.

Below is the image showing the working of Function Overriding.

See also  COMPILER VS INTERPRETER.

As we can see the function is overriding because we called the function from the object of the Derived class.

If we had called the function from the base class object, the function would not have been overridden.

Example:

#include <iostream>
using namespace std;

class Base {
   public:
    void print() {
        cout << "Base Function" << endl;
    }
};

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;
    }
};

int main() {
    Base base1;
    base1.print();
    return 0;
}

Output:

Base Function

In the above example, we have created an object for the base class and called the function (print()) so the function is not overridden.

Accessing the Overridden Function

To access the overridden function of the base class using the object of the derived class we use the scope resolution operator (: 🙂.

We can also access the overridden function of the base class using a pointer of the base class to point to an object of the derived class and then call the function using the pointer.

Example: Access Overridden Function using the scope resolution operator

// C++ program to access overridden function
// in main() using the scope resolution operator ::

#include <iostream>
using namespace std;

class Base {
   public:
    void print() {
        cout << "Base Function" << endl;
    }
};

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;
    }
};

int main() {
    Derived derived1, derived2;
    derived1.print();

    // access print() function of the Base class
    derived2.Base::print();

    return 0;
}

Output:

Derived Function
Base Function

In the above program, the following statement accesses the print() function of the Base class.

  derived2.Base::print();

Here is an image to understand how this works.

See also  STATIC MEMBERS IN C++

Now let us see another way of accessing the base class function from the derived class

Example: Call Overridden Function From Derived Class

// C++ program to call the overridden function
// from a member function of the derived class

#include <iostream>
using namespace std;

class Base {
   public:
    void print() {
        cout << "Base Function" << endl;
    }
};

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;

        // call overridden function
        Base::print();
    }
};

int main() {
    Derived derived1;
    derived1.print();
    return 0;
}

Output:

Derived Function
Base Function

In this program, we have called the overridden function inside the Derived class itself.

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;
        Base::print();
    }
};

We can see Base::print( ); ,which calls the overridden function inside the Derived class.

Here is an Image to understand better.

Now let us see how can we access the overridden function using a pointer

Example: Call Overridden Function Using Pointer

// C++ program to access overridden function using pointer
// of Base type that points to an object of Derived class

#include <iostream>
using namespace std;

class Base {
   public:
    void print() {
        cout << "Base Function" << endl;
    }
};

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;
    }
};

int main() {
    Derived derived1;

    // pointer of Base type that points to derived1
    Base* ptr = &derived1;

    // call function of Base class using ptr
    ptr->print();

    return 0;
}

Output:

Base Function

In this program, we have created a pointer of the Base type named ptr. This pointer points to the Derived object derived1.

// pointer of Base type that points to derived1
Base* ptr = &derived1;

When we call the print() function using ptr, it calls the overridden function from Base.

// call function of Base class using ptr
ptr->print();

This is because even though ptr points to a Derived object, it is actually of Base type. So, it calls the member function of Base.

See also  UNORDERED SET in Cpp

Hope you got a good understanding of function overriding in C++.

Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.