What happens when you overload a function?

Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is different from the function myfuncn(float a, int b) parameter list (float, int). Function overloading is a compile-time polymorphism.
Now that we know what is parameter list lets see the rules of overloading: we can have following functions in the same scope.

sum(int num1, int num2)
sum(int num1, int num2, int num3)
sum(int num1, double num2)

The easiest way to remember this rule is that the parameters should qualify any one or more of the following conditions, they should have different type, number or sequence of parameters.

For example:
These two functions have different parameter type:

sum(int num1, int num2)
sum(double num1, double num2)

These two have different number of parameters:

sum(int num1, int num2)
sum(int num1, int num2, int num3)

These two have different sequence of parameters:

sum(int num1, double num2)
sum(double num1, int num2)

All of the above three cases are valid case of overloading. We can have any number of functions, just remember that the parameter list should be different. For example:

int sum(int, int)
double sum(int, int)

This is not allowed as the parameter list is same. Even though they have different return types, its not valid.

Function overloading Example

Lets take an example to understand function overloading in C++.

#include 
using namespace std;
class Addition {
public:
    int sum(int num1,int num2) {
        return num1+num2;
    }
    int sum(int num1,int num2, int num3) {
       return num1+num2+num3;
    }
};
int main(void) {
    Addition obj;
    cout<

Output:

35
191

Function overloading Example 2

As I mentioned in the beginning of this guide that functions having different return types and same parameter list cannot be overloaded. However if the functions have different parameter list then they can have same or different return types to be eligible for overloading. In short the return type of a function
does not play any role in function overloading. All that matters is the parameter list of function.

#include 
using namespace std;
class DemoClass {
public:
    int demoFunction(int i) {
        return i;
    }
    double demoFunction(double d) {
        return d;
    }
};
int main(void) {
    DemoClass obj;
    cout<

Output:

100
5006.52

Advantages of Function overloading

The main advantage of function overloading is to the improve the code readability and allows code reusability. In the example 1, we have seen how we were able to have more than one function for the same task(addition) with different parameters, this allowed us to add two integer numbers as well as three integer numbers, if we wanted we could have some more functions with same name and four or five arguments.
Imagine if we didn’t have function overloading, we either have the limitation to add only two integers or we had to write different name functions for the same task addition, this would reduce the code readability and reusability.

C++ Overloading (Function and Operator)

If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading. In C++, we can overload:

  • methods,
  • constructors, and
  • indexed properties

It is because these members have parameters only.

Types of overloading in C++ are:

  • Function overloading
  • Operator overloading
What happens when you overload a function?

C++ Function Overloading

Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.

The advantage of Function overloading is that it increases the readability of the program because you don't need to use different names for the same action.

C++ Function Overloading Example

Let's see the simple example of function overloading where we are changing number of arguments of add() method.

// program of function overloading when number of arguments vary.

Output:

Let's see the simple example when the type of the arguments vary.

// Program of function overloading with different types of arguments.

Output:

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.

When the compiler shows the ambiguity error, the compiler does not run the program.

Causes of Function Overloading:

  • Type Conversion.
  • Function with default arguments.
  • Function with pass by reference.
What happens when you overload a function?
  • Type Conversion:

Let's see a simple example.

The above example shows an error "call of overloaded 'fun(double)' is ambiguous". The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.

  • Function with Default Arguments

Let's see a simple example.

The above example shows an error "call of overloaded 'fun(int)' is ambiguous". The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).

  • Function with pass by reference

Let's see a simple example.

The above example shows an error "call of overloaded 'fun(int&)' is ambiguous". The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &).

C++ Operators Overloading

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

The advantage of Operators overloading is to perform different operations on the same operand.

Operator that cannot be overloaded are as follows:

  • Scope operator (::)
  • Sizeof
  • member selector(.)
  • member pointer selector(*)
  • ternary operator(?:)

Syntax of Operator Overloading

Where the return type is the type of value returned by the function.

class_name is the name of the class.

operator op is an operator function where op is the operator being overloaded, and the operator is the keyword.

Rules for Operator Overloading

  • Existing operators can only be overloaded, but the new operators cannot be overloaded.
  • The overloaded operator contains atleast one operand of the user-defined data type.
  • We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.
  • When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
  • When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.

C++ Operators Overloading Example

Let's see the simple example of operator overloading in C++. In this example, void operator ++ () operator function is defined (inside Test class).

// program to overload the unary operator ++.

Output:

Let's see a simple example of overloading the binary operators.

// program to overload the binary operators.

Output:

The result of the addition of two objects is : 9 

What is function overloading explain with an example?

(1) The use of same function name to create functions that perform a variety of different tasks is called as. function overloading. (2) Overloading refers to the use of same thing for different purposes. Function overloading or function. polymorphism, is an example of compile time polymorphism.

What are the rules for function overloading?

Rules of Function Overloading in C++ The functions must have the same name. The functions must have different types of parameters. The functions must have a different set of parameters. The functions must have a different sequence of parameters.

Is function overloading a good thing?

The main, and only advantage of (open) overloading in a well principled language like Felix is that it makes it easier to remember library function names, both for the program writer and for the code reviewer. The primary disadvantage of overloading is the complex algorithm required to implement it.

When function is overloaded which part of the function are the same?

Function overloading is a feature that allows us to have same function more than once in a program. Overloaded functions have same name but their signature must be different.