- What is Function Overloading in C++?
- Function Overloading in C++ Example
- Why is function overloading in C++ is used?
- Rules of Function Overloading in C++
- What is the difference between function overloading and overriding in C++?
- Function overloading and ambiguity
- Types of function overloading in c++
- Causes of function overloading in c++
- Overloading using different types of parameters
- Advantages of Function Overloading in c++
- Disadvantages of Function Overloading in c++
- Difference between Function Overloading and Operator Overloading
- Difference between Function Overloading and Function Overriding
- Function overloading in C++ FAQs
- Conclusion
What is Function Overloading in C++?
Two or more functions can have the same name but different parameters; such functions are called function overloading in c++. The function overloading in the c++ feature is used to improve the readability of the code. It is used so that the programmer does not have to remember various function names. If any class has multiple functions with different parameters having the same name, they are said to be overloaded. If we have to perform a single operation with different numbers or types of arguments, we need to overload the function.
If I say parameter list, it means the data type and sequence of the parameters. For example, the parameters list of a function myfunction (int a, double b) is (int, double), which is different from the function myfunction (double a, int b) parameter list (double, int). Function overloading is a compile-time polymorphism. As we already know what a parameter list is, so let’s see the rules of overloading: we can have the following functions in the same scope.
sum(int a, int b)
sum(int a, int b, int c)
The easiest way to remember this rule is that the parameters should qualify any one or more than one of the following conditions:
- They should have a different type
- They should have a different number
- They should have a different sequence of parameters.
To put it simply,
Function overloading in c++ can have the same name but different parameters
C++ has many features, and one of the most important features is function overloading. It is a code with more than one function with the same name having various types of argument lists. This argument list includes the data type of arguments and the sequence of the arguments.
Function Overloading in C++ Example
Example 1:
#include <iostream>
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
Example 2:
#include <iostream>
using namespace std;
class Addition
{
public:
int sum(int a,int b)
{
return a+b;
}
int sum(int a,int b, int c)
{
return a+b+c;
}
};
int main(void)
{
Addition obj;
cout<<obj.sum(20, 15)<<endl;
cout<<obj.sum(81, 100, 10);
return 0;
}
Output :
35
191
Why is function overloading in C++ is used?
Function overloading is similar to polymorphism that helps us to get different behaviour, with the same name of the function. Function overloading in c++ is used for code reusability and to save memory.
Rules of Function Overloading in C++
Different parameters or three different conditions :
1. These functions have different parameter type
sum(int a, int b)
sum(double a, double b)
2. These functions have a different number of parameters
sum(int a, int b)
sum(int a, int b, int c)
3. These functions have a different sequence of parameters
sum(int a, double b)
sum(double a, int b)
The above three cases are valid cases of overloading. We can have any number of functions, but remember that the parameter list must be different. For example:
int mul(int, int)
double mul(int, int)
As the parameter list is the same, this is not allowed. Even though their return types are different, it’s not valid.
What is the difference between function overloading and overriding in C++?
Function Overloading | Function Overriding | |
Definition | When two or more methods in a class have distinct parameters but the same method name, this is known as function overloading. | One method is in the parent class and the other is in the child class when a function is overridden, but they have the same parameters and method name. |
Function signature | There should be a difference in the number or kind of parameters. | The function signature should not change. |
Behaviour | defines several methods’ behaviours. | changes the way the procedure behaves. |
Scope of Function | They belong to the same category. | They have a distinct range. |
Inheritance | Even without inheritance, it can happen. | Only when a class inherits from another does it happen |
Polymorphism | Compile Time | Run Time |
Technique | code improvement method. | method for replacing code. |
Function overloading and ambiguity
The C++ programming language offers an overloading feature that enables an overload of two or more methods with the same name but distinct parameters in order to create compile-time polymorphism. Function and operator overloading can be used to carry it out. While operator overloading overloads operators to provide user-defined data types with particular meaning, function overloading overloads two or more functions with the same name but distinct parameters.
Using built-in operators on user-defined types is made possible by this feature. Operator overloading makes the code easier to understand by redefining the functionality to meet user needs. Both function overloading and operator overloading will be discussed in detail as well as how they are applied in C++ in this article.
Using the Overloading idea, C++ enables the creation of flexible and understandable code. It enables adding different functionality to the existing code with a minimum number of changes, hence minimising the need for duplicate code. Practically speaking, C++ allows two basic types of overloading.
Function Overloading, a feature of C++, allows us to create functions with the same name but different datatypes or numbers of arguments supplied to them. Developers can define functions with the same name within the same scope thanks to this capability. The functions that have a name also share the same behaviour, enabling compile-time polymorphism. One benefit of function overloading is that it makes the code easier to read.
The use of arguments can be used in any way to create function overloading. Usage of parameters might refer to a particular parameter type, a parameter count, or a parameter sequence. As a result, a function defined as calc (float x, int y) is different from calc (int x, float y), which has separate parameters with a different datatype.
Types of function overloading in c++
There are two types of function overloading in c++-
- Compile time overloading– In compile time overloading, functions are overloaded using different signatures. Signature of a function includes its return type, number of parameters and types of parameters.
- Runtime overloading -In runtime overloading, functions are overloaded using a different number of parameters.
Causes of function overloading in c++
- Type Conversion.
- Function with default arguments.
- Function with a pass-by reference.
1. Type Conversion
Program :
#include<iostream>
using namespace std;
void function(float);
void function(int);
void function(float x)
{
std::cout << "Value of x is : " <<x<< std::endl;
}
void function(int y)
{
std::cout << "Value of y is : " <<y<< std::endl;
}
int main()
{
function(3.4);
function(34);
return 0;
}
This example shows an error “call of overloaded ‘function’ is ambiguous”. According to our prediction, function (3.4) will call the first function, and function (34) will call the second function. But this is not what happens because, in C++, all the floating-point constants are not treated as float; instead, they are treated as double. If we replace the float variable with a double variable, the program will work properly. Thus we call it a type conversion error from float to double.
2. Function with Default Arguments
Program :
#include<iostream>
using namespace std;
void function(int);
void function(int,int);
void function(int x)
{
std::cout << "Value of x is : " <<x<< std::endl;
}
void function(int y,int z=12)
{
std::cout << "Value of y is : " <<y<< std::endl;
std::cout << "Value of z is : " <<z<< std::endl;
}
int main()
{
function(12);
return 0;
}
The above example shows an error saying “call of overloaded ‘fun(int)’ is ambiguous”, this is because function(int y, int z=12) can be called in two ways:
By calling the function with only one argument (and it will automatically take the value of z = 12)
By calling the function with only two arguments.
When you call the function: function(12), we full fill the condition of both function(int) and function(int, int); therefore, the compiler gets into an ambiguity that gives an error.
3. Function with a Pass by Reference
Program :
#include <iostream>
using namespace std;
void function(int);
void function(int &);
void function(int a)
{
std::cout << "Value of a is : " <<a<< std::endl;
}
void function(int &b)
{
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
int x=10;
function(x);
return 0;
}
The above program shows an error saying “call of overloaded ‘fun(int&)’ is ambiguous”. As we see, the first function takes one integer argument, and the second function takes a reference parameter as an argument. In this case, the compiler cannot understand which function is needed by the user as there is no syntactic difference between the fun(int) and fun(int &); therefore, it shoots an error of ambiguity.
Overloading using different types of parameters
#include <iostream>
using namespace std;
void printValue(int A);
void printValue(char A);
void printValue(float A);
int main()
{
printValue(10 );
printValue('@' );
printValue(3.14f );
return 0;
}
void printValue(int A)
{
cout<< endl << "Value of A : "<< A;
}
void printValue(char A)
{
cout<< endl << "Value of A : "<< A;
}
void printValue(float A)
{
cout<< endl << "Value of A : "<< A;
}
Output:
Value of A : 10
Value of A : @
Value of A : 3.14
Function overloading can be implemented based on the many types of arguments that are passed into the function. Both non-member functions and member functions of a class may implement function overloading.
Function overloading by changing the number of arguments
On the basis of the quantity of parameters supplied into the function, we can implement function overloading. Both non-member functions and member functions of a class may implement function overloading.
#include <iostream>
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
In this way of function overloading, we define two functions of the same type but with a different number of parameters with the same names. For example, in the program given below, we have made two add() functions to return the sum of two and three integers.
// first function definition
int add(int a, int b)
{
cout << a+b;
}
// second overloaded function definition
int add(int a, int b, int c)
{
cout << a+b+c;
}
Here add() function is said to be overloaded, as it has two definitions, one that can accept two arguments and another which can accept three arguments. Which add() function will be called, depending on the number of arguments.
int main()
{
add(10, 20); // add() with 2 parameter
add(10, 20, 30); //sum() with 3 parameter
}
Program ( By changing the number of arguments):
#include <iostream>
using namespace std;
int add(int a, int b)
{
cout << a+b <<endl;
return 0;
}
int add(int a, int b, int c)
{
cout << a+b+c <<endl;
return 0;
}
int main()
{
add(20, 40);
add(40, 20, 30);
}
Output:
60
90
In the above example, we overload an add() function by changing the number of arguments. First, we define an add() function with the two parameters, and then we can overload it by again defining an add() function but this time with three parameters.
Function overloading by different types of arguments
In this method, we define two or more functions with the parameters of different data types but with the same number of parameters with the same name. For example, in this program, we have three add() functions; the first one will get two integer arguments, the second one will get two float arguments, and the third one will get two double arguments.
Program :
#include <iostream>
using namespace std;
int add(int x, int y) // first definition
{
cout<< x+y << endl;
return 0;
}
float add(float a, float b)
{
cout << a+b << endl;
return 0;
}
double add(double x, double y)
{
cout << x+y << endl;
return 0;
}
int main()
{
add(20, 40);
add(23.45f, 34.5f);
add(40.24, 20.433);
}
Output :
60
57.95
60.673
In the above example, we define an add() function three times. First, using integers as parameters, secondly, using float as parameters, and third using double as a parameter.
Therefore we override the add() function twice.
Advantages of Function Overloading in c++
- The program is very simple and also easier to understand.
- It saves the memory space, maintains consistency, makes clear interface for methods regardless of the parameter’s type and readability of the program.
- Using the function overloading concept, we can develop more than one function with the same name, but the arguments passed should be of different types.
- Function overloading executes the program faster.
- Function overloading is used for code reusability and to save memory.
To understand function overloading in C++ clearly, you must be well-versed in C++. If you wish to enhance your skills, you can take up the Introduction to C++ free course at Great Learning Academy and learn the basic concepts and fundamentals of C++ to help you build expertise in the subject.
Disadvantages of Function Overloading in c++
- Function declarations that differ by their return type cannot be overloaded with the function overloading process.
- If any static member function is declared, then the same parameters or the same name types cannot be overloaded.
Difference between Function Overloading and Operator Overloading
Function Overloading | Operator Overloading |
Function overloading allows us to call it in multiple ways. | Operator overloading allows operators to have their extending meaning beyond its predefined operational meaning. |
You can overload the function with the same name but with different parameters. | You can overload (define custom behaviour) for operators such as ‘+’, ‘-‘, ‘()’, ‘[]’. |
Function overloading means using a single name and giving more functionality to it. | Operator overloading means adding extra functionality for a certain operator. |
When an operator is overloaded, the operator has different meanings, which depend on the type of its operands. | When a function is overloaded, the same function name has different interpretations depending on its signature, which is the list of argument types in the functions parameter list. |
If you want to learn more about operator overloading visit Operator Overloading in C++.
Difference between Function Overloading and Function Overriding
Function Overloading | Function Overriding | |
Definition | In function overloading, two or more methods in one class have different parameters but the same method name. | In function overriding, two methods have the same parameters, and method name, but one method is in the parent class and the other is in the child class. |
Function Signature | Either type of parameters or the number of parameters should differ. | Function signature should remain the same. |
Behavior | Defines multiple behaviors to a method. | Changes the behavior of the method. |
Scope of Function | They are in the same scope. | They are in different scope. |
Inheritance | It can occur without inheritance. | It occurs only when one class is inherited from another class. |
Polymorphism | Compile Time | Run Time |
Technique | Code refinement technique | Code replacement technique |
No. of Classes | Only one class is required. | Minimum two classes are required. |
Function overloading in C++ FAQs
Function overloading refers to when two or more functions with the same name but distinct parameters exist.
Function overloading is one of the most crucial characteristics of C++, among its many other features. There are many functions with the same name in this code, each with a unique set of argument lists. The data type and ordering of the arguments are also included in this argument list.
The C++ function overloading feature is used to make the code easier to read. It is used to save the programmer from having to memorise different function names. Overloaded functions are those that belong to a class but have more than one instance with the same name but different parameters. The function must be overloaded if a single operation must be performed with various numbers or types of parameters.
Function overloading is referred to as a function of polymorphism in OOP.
Similar to polymorphism, function overloading enables us to obtain different behaviour while maintaining the function’s name. In C++, function overloading is used to reduce memory usage and increase code reuse.
Late binding and dynamic polymorphism are other names for runtime polymorphism. The function call is resolved at runtime in runtime polymorphism. In contrast, with compile time or static polymorphism, the compiler determines which function call to bind to the object after deducing it at runtime.
By using inheritance, existing classes can be extended and reused without being changed, creating hierarchical links between them. A class can be embedded into an object via inheritance. Let’s say you include a class A object of type x in the class specification of type B.
The fundamental benefit of function overloading is that it makes code easier to read and reuse.
Function overloading is used to improve consistency, readability, and memory efficiency.
The program’s execution is accelerated by it.
Additionally, code upkeep becomes simple.
Function overloading gives code flexibility.
The function’s versatility avoids the need for many function names to conduct the same set of functions.
Conclusion
Function overloading in C++ can be used in different ways to enhance code readability. While doing programming in C++, it helps to save compile time and also saves memory space. You also learned how similar operations are performed using the function overloading concept. After mastering topics like function overloading in C++, you can continue to build your expertise with various free certificate courses that offer certifications in different programming languages
You can enhance your function overloading in c++ concepts here: