Function Overloading in C++ with Example.

Function Overloading in C++

Function Overloading is a feature of Object Oriented Programming in C++ that allows us to create multiple functions with the same name but different parameters. When the same function name is overloaded with different work in the same scope then it is called function overloading.


Function overloading is based on the concept of polymorphism, which allows a function to take on different forms based on its input or context. 

Example:

//example of function overloading
int fun(){}
int fun(int a, int b){}
float fun(double num){}

Function overloading is an important concept to understand because we use function overloading many times in real-life programming. There are a few important points that we need to keep in mind, let's discuss each of them with examples.

Overloading Function with Different Parameter Types.

Function overloading is used to provide different implementations of a function for different types of inputs. A function can be overloaded based on the number or types of parameters. The return type of a function cannot be used to differentiate between overloaded functions.

C++ Example code:
//Overloading Function with different types of parameters.
#include<iostream>
using namespace std;

//function with integer type parameter
int sum(int a, int b) {
   return a + b;
}
//function with float type parameter
float sum(float a, float b) {
   return a + b;
}

int main(){
   //function call
   cout << "Sum of two integers: " << sum(5, 10) << endl;
   cout << "Sum of two floats: " << sum(3.4f, 4.6f) << endl;
}
Output:
Sum of two integers: 15
Sum of two floats: 8

In the above code, we have used the same function name 'sum()' with the same number of parameters but in one function we have passed float type and in another, we have passed integer type parameters.  

Overloading Function with Different Number of Parameters.

The overloaded functions must have different parameter lists, either in terms of the number of parameters or their types. Function overloading is determined at compile-time based on the number and type of arguments passed to the function.

C++ Example code:
//Overloading Function with different number of parameters.
#include<iostream>
using namespace std;

//function with three integer parameters
int sum(int a, int b, int c) {
   return a + b + c;
}
//function with two integer parameters
int sum(int a, int b) {
   return a + b;
}
//function with two float parameters
float sum(float a, float b) {
   return a + b;
}

int main(){
   //function call
   cout << "Sum of three integers: " << sum(5, 10, 5) << endl;
   cout << "Sum of two integers: " << sum(12, 5) << endl;
   cout << "Sum of two floats: " << sum(3.5f, 4.5f) << endl;
}
Output:
Sum of three integers: 20
Sum of two integers: 17
Sum of two floats: 8

In the above code, we have used the same function name sum() with the same return type but passing a different number of parameters.

Conclusion:
Function overloading improves readability and reduces code duplication by providing a common function name to related functionality. It is also used to provide default values for parameters.

Read More:

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS