Last Updated on April 18, 2023 by Prepbytes
While programming in C++, we often need to define the functions that perform the same operations but on different types of data. This problem can be solved by using the concept of Function Overloading in C++. Let us learn about this interesting topic in detail.
What is Function Overloading in C++?
In C++ you can specify more than one function to the same name and that name can either be a function or an operator this process is known as Function overloading in C++. There are two types of overloading in C++. They are function overloading and operator overloading. Here we will learn about one of the two types i.e function overloading in C++.
Function overloading in C++ is a situation when more than one functions have the same name but with different types of parameters or different numbers of parameters. We can use function overloading in C++ to improve the readability of the code as it will lead to fewer different names of functions.
We can overload the function when we need to perform a single operation but with a different number of parameters and arguments. It is considered to be a part of compile-time polymorphism.
Working of Function Overloading in C++
Following a straightforward premise, function overloading in C++ ensures that the function name stays constant while the list of parameters and their data types depend on the programmer. When a function is overloaded and called in the main program, the function will execute if the set and kind of input actual parameters match the syntax of the formal arguments. The promotion of data types (standard type conversion), such as the conversion of a float into a double, occurs if a precise match cannot be found. In the last scenario, this throws an error.
Why Function Overloading in C++ is used?
Comparing OOPS principles to traditional structured programming languages, there are several benefits. Compile-time polymorphism is understood to include function overloading. The ability to create a function with the same name but a distinct execution behavior is a characteristic of OOPS ideas that makes code more legible and reusable.
Example of Function Overloading in C++
Below is an example of Function Overloading in C++. Here we will see how function overloading is used in a program with proper implementation and output of the same.
Code Implementation of Function Overloading in C++
#include<bits/stdc++.h> using namespace std; void productnum(int A, int B); void productnum(int A, int B, int C); void productnum(int A, int B, int C, int D); int main() { productnum(1,2); productnum(1,2,3); productnum(1,2,3,4); return 0; } void productnum(int A, int B) { cout<< endl << "productnum is : "<< A*B; } void productnum(int A, int B, int C) { cout<< endl << "productnum is : "<< A*B*C; } void productnum(int A, int B, int C, int D) { cout<< endl << "productnum is : "<< A*B*C*D; }
Output
productnum is : 2
productnum is : 6
productnum is : 24
Explanation of the example of function overloading in C++
In the above example we have three functions with the name productnum still they are not giving any error because we have different parameters for them and it is known as function overloading the first function contains two arguments whereas the second function contains three arguments and the third function contains four arguments. As the name suggests the main objective of the function is to multiply all of its parameters and hence the answer is corresponding to it as you can see in the output image.
Rules of Function Overloading in C++
While performing function overloading in C++, we have to follow the rules given below as we cannot perform function overloading on any function.
- The most basic rule is to have the same name for all the functions.
- The set of parameters must be different for all the functions.
- The types of parameters must be different for all the functions.
- The sequence of parameters must be different for all the functions.
Types of Function Overloading in C++
There are mainly two types of function overloading in C++ and they are compile-time overloading and run-time overloading.
- Run-time overloading: This is a type of function overloading in which we overload the function using a different number of parameters.
- Compile time overloading: We can achieve compile time overloading using various methods as we can achieve this by changing the signature of the function. The signature of the function contains many things like the number of parameters, return type, and types of parameters.
Various Methods to Achieve Function Overloading in C++
We can obtain or achieve function overloading in C++ by various methods we will see all of the methods in this section with proper code and output followed by the explanation of the code and the reason for the output.
Function Overloading in C++ by Changing the Number of Arguments
As the name suggests in this we will change the number of arguments in simple words we can achieve function overloading by changing the number of arguments to the function. We will now look at the example to understand the same.
Code Implementation of Function Overloading in C++ by Changing the Number of Arguments
#include<bits/stdc++.h> using namespace std; void sumnum(int A, int B); void sumnum(int A, int B, int C); void sumnum(int A, int B, int C, int D); int main() { sumnum(1,2); sumnum(1,2,3); sumnum(1,2,3,4); return 0; } void sumnum(int A, int B) { cout<< endl << "sumnum is : "<< A+B; } void sumnum(int A, int B, int C) { cout<< endl << "sumnum is : "<< A+B+C; } void sumnum(int A, int B, int C, int D) { cout<< endl << "sumnum is : "<< A+B+C+D; }
Output
sumnum is : 3
sumnum is : 6
sumnum is : 10
Explanation of the example of function overloading by changing the number of arguments
In the above, the example we have three functions with the name sumnum all of the functions have different numbers of arguments one has two arguments one has three arguments and one has four arguments. Their aim is to give the summation of all the arguments present in the declaration of the function. Hence we are getting the corresponding argument.
Function Overloading in C++ by using Different Types of Parameter
Function overloading in c++ can be achieved by changing the types of parameters passed to the function that refers to we can change the data type for the parameter keeping the number of parameters for some or all the functions of the functions which are overloading.
Code Implementation:
#include<bits/stdc++.h> using namespace std; void showValue(int A); void showValue(char A); void showValue(float A); int main() { showValue(22); showValue('!'); showValue(8.54f); return 0; } void showValue(int A) { cout<< endl << "Value of A : "<< A; } void showValue(char A) { cout<< endl << "Value of A : "<< A; } void showValue(float A) { cout<< endl << "Value of A : "<< A; }
Output
Value of A : 22
Value of A : !
Value of A : 8.54
Explanation of function overloading in c++ by using the different types of parameter
In the above example, we have the same function named showValue which print the value of the corresponding argument and we have the same name for all the function just the data type of the argument is varied as you can see in the code, and hence the result is according to tot the argument we have provided.
Function Overloading in C++ by Changing the Argument of the Function
This is one of the important methods to achieve function overloading in c++ as with the help of this we can easily achieve function overloading by just changing the argument of the function like shown in the following example:
Code Implementation:
#include<bits/stdc++.h> using namespace std; int multiply(int x, int y) // first definition { cout<< x*y << endl; return 0; } float multiply(float a, float b) { cout << a*b << endl; return 0; } double multiply(double x, double y) { cout << x*y << endl; return 0; } int main() { multiply(28, 74); multiply(22.22f, 48.2f); multiply(75.268, 48.48); }
Output
2072
1071
3648.99
Explanation of the above code
In the above example, we have a case of function overloading with the function name multiply whose function is to multiply all the parameters given to the function and return the answer according to the return type of the function. So in the first function, the return type is int, in the second function the return type is float and in the last function the return type is double and the corresponding result is shown in the output image.
Advantages of Function Overloading in C++
There are many advantages of function overloading in c++ some of which are mentioned below:
- It is easier to understand and is very simple to write.
- By having the same name for many functions we do not need to create or manage various functions so it saves a lot of memory space.
- It will execute the program faster.
- The readability of the code will be improved by this.
- It will enhance the code reusability of the code.
Disadvantages of Function Overloading in C++
Function Overloading in C++ has the following disadvantages.
- It cannot work properly in the case of static member functions as in them some of the name types may not be overloaded correctly.
- Some of the function declarations cannot be overloaded if they only differ in their return type.
Conclusion
In the above article, we have studied the function overloading feature present in the object-oriented programming of C++. Along with this, we have studied types of function overloading.in c++, followed by the rules for function overloading in c++, the different cases to achieve function overloading c++, and at the last concluded with the advantages and disadvantages of function overloading in c++.
Frequently Asked Questions
1. What are the different types of overloading in C++?
There are mainly two types of overloading available in C++ and they are function overloading in C++ and operator overloading in C++.
2. What do you mean by function overriding in C++?
When the parent and the child contain a function with the same name and parameters it is known as function overriding.
3. Is there any minimum number of functions required for function overloading in C++?
Yes, there must be at least two functions of the same name present to obtain function overloading in c++.