Javatpoint Logo
Javatpoint Logo

C++ Function Overloading

In brief Object-oriented programming is a notion found in C++ (also known as OOPS). We will talk about function overloading among the various attributes found in OOPS. Every programming language offers the ability to reuse previously written code by using functions. The number and type of parameters provided to a function are typically decided by the programmer based on the definition of the function. Function overloading is a further characteristic of programming languages that enable OOPS ideas in relation to functions. It makes reference to the possibility of having many functions with the same name but distinct parameters.

Two or more functions may share the same name but not the same list of arguments when functional overloading occurs. It is a key characteristic of C++. Compile-time polymorphism and function overloading are similar concepts. A close examination reveals that the name stays the same, although the list of arguments, data type, and order all change. Take a look at a C++ function overloading example.

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.





C++ Function Overloading

In the aforementioned example, we can see that two functions are defined as a piece of code. The names of the functions are the same-"addPodium"-but the return type, the list of input arguments, and the data types for those arguments have been altered.

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

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


r1 is : 42
r2 is : 0.6  

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.

Type Conversion:

C++ Function Overloading

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 us

Why is C++ Using Function Overloading?

Over traditional structured programming languages, the OOPS ideas offer a number of benefits. Overloading functions is regarded as compile-time polymorphism. With the help of this OOPS idea, a programmer can create a function with the same name but a distinct execution pattern, enabling the code to be more understandable and reusable.

What are the rules of function overloading in C++?

When overloading a function in C++, there are some guidelines that must be observed. Let's take a closer look at a few of them:

1) The parameters for each function must be in a distinct order.

2) The functions have to be named the same.

3) The parameters for the functions must be distinct.

4) The parameters for the functions must be of various sorts.

What are the types of function overloading in C++?

In C++, there are two types of function overloading. Those are

1) Overloading at compile time occurs when alternative signatures are used to overload the functions. The function's return type, number, and type of parameters are all regarded as the function's signature.

2) Overloading that occurs during runtime refers to the overloading of the functions. Time overloading occurs when a different number of parameters are added to the function during execution.

What are the advantages of function overloading in C++?

Here are a few benefits of function overloading in C++.

1) The programmer can create functions with distinct purposes but the same name by using function overloading.

2) It speeds up the program's execution.

3) The code is clearer and simpler to comprehend.

4) It reduces memory utilization and makes programs reusable.

What are the disadvantages of function overloading in C++?

The following are some drawbacks of C++ function overloading.

1) The primary drawback of function overloading is that it prevents the overloading of functions with various return types.

2) The identical parameters cannot be overloaded in the case of a static function.

What are the differences between function overloading and operator overloading?

Function overloading Operator overloading
It is possible to use various parameters in an overload of a function with the same name. One can overload operators such as +, -, /
A function might have more than one execution with various parameters when it is overloaded. Operation reliance on operands occurs when an operator is overloaded.
The user can call using a variety of methods. In addition to the predetermined meaning, it enables the user to have a more expansive meaning.

What are the differences between function overloading and function overriding?

Function overloading Function overriding
It allows the programmer to have many functions with the same name but distinct arguments. Method overriding occurs when two methods with the same name and parameters are present in different classes, one in the parent and one in the child.
They have the same scope. They have different scopes.
Even without inheritance, function overloading is possible. It only happens when there is inheritance.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA