Javatpoint Logo
Javatpoint Logo

Function Overloading in C++

A characteristic of object-oriented programming is the ability for many functions to have the same name but distinct arguments. This is known as function overloading. Function overloading is the process of adding additional tasks to a function name. The "Function" name and the parameters should be different when using function overloading. In C++, function overloading is an illustration of a polymorphism feature.

Polymorphism

Polymorphism refers to the existence of several forms. Polymorphism may be simply defined as a message's capacity to be presented in several forms.

An individual who can have many traits at once is a real-world example of polymorphism. A guy is a parent, a spouse, and a worker all at once. As a result, the same person behaves differently depending on the circumstance. Polymorphism is the term for this. One of the key components of object-oriented programming is polymorphism.

For function overloading, the arguments should adhere to one or more of the following requirements:

There should be a separate sort of parameter.

The execution of the above discussion is shown below:

Output:

Sum = 12
Sum = 11.5
...................
Process executed in 1.22 seconds
Press any key to continue.

Explanation:

In the above code as we can see we have two functions with the same name "add" having the same functionality, but the parameter data types are different that is int and double respectively.

The number for each parameter should be different.

The execution of the above discussion is shown below:

Output:

sum = 12
sum = 15
...............
Process executed in 1.22 seconds
Press any key to continue.

Explanation:

In the above code both the functions are same but number of parameters is different having the same data types will work the same.

The order of parameters for each parameter should be distinct.

The execution of the above discussion is shown below:

Output:

sum = 12.5
sum = 11.5
..................
Process executed in 1.11 seconds
Press any key to continue.

Here is a straightforward C++ example to show function overloading.

Output:

This is int 10
This is float 10.1
This is char* ten
..............................
Process executed in 1.11 seconds
Press any key to continue.

What's the process of function overloading?

Perfect match: (Function name and Parameter)

If a partial match is discovered:

  • An int is promoted from the types Char, Unsigned Char, and Short.
  • Float is given a twofold promotion

In the absence of a match, C++ uses the standard conversion to look for one.

OTHER ERROR







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA