C++

C++ Function Overloading

C++ is a flexible general-purpose programming language. This programming language was originally created by Bjarne Stroustrup, a Danish computer scientist, back in 1985. C++ supports polymorphism, inheritance, and more. This article covers function overloading to achieve compile-time polymorphism in the C++ programming language.

What is a Function?

A function is nothing more than a specific piece of code that performs a specific task based on inputs provided, and it returns the requested results to the user in the form of an output. Functions are used to eliminate repetitive code in large codebases.

After defining a function, you can reuse it at a later point in time, either in the same program or in a different program.

Function Syntax

A function in C++ has the following syntax:

returnType functionName(parameter_list)

{

        …………………

        …………………

        return return_value;
}

The returnType, parameter_list, and return statement are optional. A function in C++ can return a maximum of one value. If a function does not return any value, the returnType should be defined as void.

What is Function Overloading?

In C++, multiple function definitions can have the same function name, but with different parameters. This is called function overloading. With the help of the function overloading feature, compile-time polymorphism can be achieved in C++.

Functions can be overloaded in the following ways:

  1. The number of parameters can be different
  2. The data type of the parameters can be different
  3. The sequence of the parameters can be different

However, the return value is not considered for function overloading. 

The following functions are overloaded:

  1. int addition (int a, int b)
  2. float addition (float f, gloat g)
  3. float addition (float f, int i)
  4. float addition (int i, float f)
  5. int addition (int a, int b, int c)
  6. float addition (float f, float g, float h)

As you can see, with the help of the function overloading feature in C++, there can be multiple definitions/functionalities with the same function name and in the same scope.

Without the function overloading feature, you would need to write a separate function [for example, addition_1(), addition_2() etc] for each variation. For example, you may have to write addition_1() to add two integers, addition_2() to add two floats, and so on. However, as you can see above, the function overloading feature can be used to define multiple variations of the “addition()” function while still keeping the same function name.

The following functions are not considered to be overloaded because the only difference between these two is the return type (return type is not considered for function overloading in C++):

  1. int addition (int a, int b)
  2. float addition (int a, int b)

Examples

Now that you understand the concept of function overloading, we will go through a couple of working example programs to understand this concept more clearly. We will cover the following examples:

  1. Example 1: Simple Function
  2. Example 2: Simple Addition Function
  3. Example 3: Function Overload (1)
  4. Example 4: Function Overload (2)
  5. Example 5: Function Overload (3)

The first two examples explain how normal functions work in C++, while the last three examples demonstrate the function overloading feature in C++.

Example 1: Simple Function

In this example, we will demonstrate how a simple function can be defined and called in C++. We will define a class called “Display” and a public function called “display().” From the “main()” function, we will call the “display()” function with the help of the “Display” class object (d).

#include <iostream>
using namespace std;

class Display
{

public:
        void display()
        {
                cout << "Hello World!" << endl;
        }
};


int main()
{
        Display d;
        d.display();
        return 0;
}

Example 2: Simple Addition Function

In this example, we will demonstrate how to define a simple “addition()” function in C++. We will define a class called “DemoAdd” and a public function called “addition().” From the “main()” function, we will call the “addition()” function with the help of the “DemoAdd” class object (d).

In this example, the current implementation of the “addition()” function accepts only two integer parameters. That means that the current “addition()” function is capable of adding only two integers.

To add three integers instead of two, a function with a different name, such as “addition_1(),” can be defined. In C++, a function can be overloaded, meaning that another definition of the “addition()” function can be defined to add three integers and keep the same name, i.e., “addition().” In the next example, we will look at how to overload the “addition()” function.

#include <iostream>

using namespace std;

class DemoAdd
{

public:
        int addition(int a, int b)
        {
                int result;

                result = a + b;

                return result;
        }
};

int main()
{        
        DemoAdd d;

        int i1 = 10, i2 = 20, res;

        res = d.addition(i1, i2);

        cout << "Result = " << res << endl;

        return 0;

}

Example 3: Function Overload (1)

In the previous example, we defined the “addition()” function to add two integers and return the computed result. Now, in this example, we will overload the “addition()” function to add three integers. So, we will be able to call the “addition()” function with two integer arguments, as well as three integer arguments.

Without the function overloading feature, we would have to write another function with a different name.

#include <iostream>
using namespace std;

class DemoAdd
{
public:

        // First function definition of addition()
        int addition(int a, int b)
        {
                int result;

                result = a + b;
       
                return result;
        }

        // Overloaded version of addition() function
        int addition(int a, int b, int c)
        {

                int result;
                result = a + b + c;      
                return result;
        }
};


int main()

{        
        DemoAdd d;
        int i1 = 10, i2 = 20, i3 = 30, res1, res2;
       
        res1 = d.addition(i1, i2);     // addition() with 2 parameters
        res2 = d.addition(i1, i2, i3); // addition() with 3 parameters

        cout << "Result = " << res1 << endl;
        cout << "Result = " << res2 << endl;
     
        return 0;
}

Example 4: Function Overload (2)

In earlier sections of this article, you learned that function overloading can be performed based on differences in parameter type. Here, we have overloaded the “addition()” function based on the parameter’s data type. In the first version of the addition function, we will add two integer type variables; and in the second version, we will add two float type variables.

#include <iostream>
using namespace std;

class DemoAdd
{
public:
        // First definition of addition()
        int addition(int a, int b)
        {
                int result;
                result = a + b;        
                return result;
        }


        // Overloaded function definition
        float addition(float f, float g)
        {
                float result;
                result = f + g;        
                return result;
        }
};


int main()

{        
        DemoAdd d;
        int i1 = 10, i2 = 20, res1;
        float f1 = 10.5, f2 = 20.7, res2;
       
        res1 = d.addition(i1, i2);  // addition(int a, int b) will be called
        res2 = d.addition(f1, f2);  // addition(float f, flat g) will be called      

        cout << "Result = " << res1 << endl;
        cout << "Result = " << res2 << endl;
       
        return 0;

}

Example 5: Function Overload (3)

In this example, the “addition()” function is overloaded based on differences in the sequence of the parameter list. This is another way to overload a function in C++.

#include <iostream>
using namespace std;

class DemoAdd
{
public:
        // First function definition of addition() function
        float addition(int a, float b)
        {

                float result;
                result = (float)a + b;
                return result;
        }

        // Overloaded function definition of addition() function
        float addition(float a, int b)
        {

                float result;
                result = a + (float)b;    
                return result;
        }
};


int main()
{        
        DemoAdd d;
        int i1 = 10;
        float f1 = 10.5, res1, res2;
       
        res1 = d.addition(i1, f1); // addition(int a, float b) will be called
        res2 = d.addition(f1, i1); // addition(float a, int b) will be called
       
        cout << "Result = " << res1 << endl;
        cout << "Result = " << res2 << endl;
       
        return 0;
}

Conclusion

C++ is a general-purpose and flexible programming language that is widely used in various domains. This programming language supports both compile-time and run-time polymorphism. In this article, you learned how to achieve compile-time polymorphism in C++ using the function overloading feature. This is a very helpful feature in C++ that helps programmers to write readable code. It can also be helpful for writing reusable code.

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.