Function Overloading in C++ | Function Overriding in C++

In this article, we will learn about function overloading and function overriding in C++. We will also discuss the differences in both. Let’s start!!!

Function Overloading in C++

In C++, two or more functions can have the same name if the number and/or type of parameters are different, this is called function overloading. Thus, overloaded functions are functions that have the same name but different parameters.

However, if in two or more functions, only return type is different keeping number and type of parameters same, it is not considered function overloading. In this case, the compiler gives an error.

Function overloading is achieved during compile time. It is a form of compile time polymorphism.

Examples of overloaded functions

int add(int, int);
int add(int, int, int); //number of parameters different
double add(double, double); //type of parameters different

Example to illustrate function overloading in C++

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a+b;
}

int add(int a, int b, int c) {
    return a+b+c;
}

double add(double a, double b) {
    return a+b;
}

int main() {
  int x = 3, y = 7, z = 12;
  double n1 = 4.56, n2 = 13.479;
  
  cout<<"x+y = "<<add(x,y)<<endl;
  cout<<"x+y+z = "<<add(x,y,z)<<endl;
  cout<<"n1+n2 = "<<add(n1,n2);
  
  return 0;
}

Output

x+y = 10
x+y+z = 22
n1+n2 = 18.039

Function Overriding in C++

When a member function of a base class is redefined in its derived class with the same parameters and return type, it is called function overriding in C++. Now, if we use an object of the derived class to call this function, the function defined in the derived class is invoked. The base class function is said to be overridden.

Function overriding is achieved during runtime. It is a form of runtime polymorphism.

Example to illustrate function overriding in C++

#include <iostream>
using namespace std;

class base {
    public:
    virtual void display() {
        cout<<"Function of base class"<<endl;
    }
};
class derived : public base {
    public:
    void display() {
        cout<<"Function of derived class"<<endl;
    }
};

int main() {
  derived d1;
  d1.display();
  return 0;
}

Output

Function of derived class

We use virtual keyword with the base class function to override it rather than access it.

Difference between Function Overloading and Function Overriding

Basis Function Overloading Function Overriding
Inheritance Can happen without inheritance Happens only when a class inherits from another class
Signature Function signatures of overloaded functions must be different  Same function signatures
Polymorphism Compile time  Runtime
Scope Overloaded functions are in the same scope Functions are in different scope
Purpose To have multiple functions with same name that act differently depending on parameters To perform additional or different tasks than the base class function
Number of times A function can be overloaded multiple times A function is overridden single time in its derived class

Summary

This article covers function overloading and function overriding in C++. We learnt how to implement these using examples. Then, we listed down the differences between function overloading and function overriding.