Function Overriding In C++

Function Overriding In C++

With the aid of examples, we will learn about function overriding in C++ in this article.

In C++, function overriding occurs when a derived class defines the same function as its base class. It is employed in order to achieve runtime polymorphism

function overriding allows us to use a function in the child class that is already existing in the parent class.

It allows you to give a specific implementation of a function that its base class already provides.

As we are all aware, inheritance is an OOP feature that enables us to derive classes from base classes. The base class’s features are passed down to the derived classes.

Assume that the derived class and the basic class both define the same function. Now, if we use an object from the derived class to invoke this function, the derived class function is used.

Function override is an object-oriented programming language concept in which we have one method in a parent class and override that method in the child class with the same signature, i.e.

the same method name, number of parameters, and return type. We’ve provided our own implementation of this function for the kid class, thus we can say that the method can be implemented in two separate classes.

this is referred to as function overriding. The base class function is replaced with the function in the derived class.

How does Function Overriding in C++ work? (Function Overriding)

If we wish to implement function overriding in our application,

we must first establish some form of relationship between two classes since we cannot implement function overriding within the same class; if we do,

we will be implementing function overloading. So, for function overriding, we need inheritance between two classes, i.e. a parent-child relationship.

However, the question of why we utilize function overriding emerges.

The key reason for this is that we may offer a class-specific implementation of our method. Function overriding is possible at runtime because which method is called is determined only at runtime.

We may also utilize the parent class reference to call methods. There are a few examples that demonstrate which class method will be invoked based on the object created:

In this example, the child class method will be called since the parent class reference holds an instance of the child class. However, if the method is not present in the child class, the parent class method is run.

Child c = Child();:: In this scenario, child class methods will also be given preference because they hold the

Child c = Parent();:: If we do this, a compile-time error will occur.

Binding occurs when we bind a method call to its body. Static and dynamic bindings are available.

Rule Of Function Overriding:

  • Between the two classes, there must be a parent-child relationship.
  • The method signature must match. The method name, method parameters, and return type cannot be changed; everything must be the same as the parent class signature.
  • We can only override the method in the child class and give an alternative implementation.
  • In C++, we can also override a parent class member with the same signature as the class.
  • We can’t override static, private, or final methods because their scope is limited to the class.
  • We can’t even change the method’s access while overriding from a parent class.
    The same is true for the return type; we cannot change it.

Method overriding has some advantages, which are listed below:

Assume we have a parent class with a large number of children. However, in the child class, they wish to provide their own version of a single method,

therefore function overriding is the best option. We can give a new implementation of the same method using function override without affecting the code of the parent class.

For example, consider a bank and the various interest rates it offers. Banks’ interests differ from one another.

So the bank is the parent class here, and each branch is a child, with the rate of interest() as the overriding method that displays the rate of interest to consumers.

Example 1: C++ Function Overriding

// 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

Here, the Base and Derived classes both define the same function print().

As a result, when print() is called from the Derived object derived1, the Base method is replaced with the print() from Derived.

As we can see, the function was overridden because it was called from a Derived class object.

The print() function would not have been overridden if it had been invoked from an object of the Base class.

// Call function of Base class
Base base1;
base1.print(); // Output: Base Function

Access Function Overriding in C++

We utilize the scope resolution operator:: to access the overridden function of the base class.

We can alternatively invoke the overridden function by using a base class pointer to point to an object of the derived class and then call the function from that pointer.

Example 2: C++ Access Function Overriding to the Base Class

// 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

This statement is used here.

derived2.Base::print();

accesses the Base class’s print() function

Example 3: C++ Call Function Overriding 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 application, we called the overridden function from within the Derived class.

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

Take note of the code Base::print();, which invokes the override function contained within the Derived class.

Example 4: C++ Call Function Overriding 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 constructed a pointer of type Base called ptr. This pointer will take you to the Derived object derived1.

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

When we use ptr to invoke the print() function, it calls the overridden function from Base.

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

This is because, despite the fact that ptr points to a Derived object, it is of Base type. As a result, it calls the Base member function.

We must use virtual functions in the Base class to override the Base function rather than accessing it.

you may like :

Friend Function and Friend Classes In C++

Leave a Reply