Function overloading in C++
Function overloading based on different parameters:
Based on different number of parameters
In this method, we define two or more functions with the same name and have a different number of parameters.
Code
#include <iostream>
using namespace std;
class multiplication
{
public:
int mul(int x,int y) //1
{
cout<<" function 1 is called ";
return x*y;
}
int mul(int x,int y, int z) //2
{
cout<<" function 2 is called ";
return x*y*z;
}
};
int main()
{
multiplication M; //class object decleration
cout<<M.mul(2, 14, 6)<<endl;
cout<<M.mul(10, 12)<<endl;
cout<<M.mul(5, 10, 12);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
function 2 is called 168
function 1 is called 120
function 2 is called 600
In the above code, we define a mul() function two times. First, mul() have two parameters, second mul() function has three parameters. The mul() function is called based on the number of parameters.
You can try and compile with the help of online c++ compiler.
Based on different types of parameters
In this method, we define two or more functions with the same name, and they have different return types or different types of parameters.
Code
#include <iostream>
using namespace std;
class multiplication
{
public:
int mul(int x, int y) //1
{
cout<<" function 1 is called ";
return x*y;
}
double mul(double x,double y) //2
{
cout<<" function 2 is called ";
return x*y;
}
};
int main()
{
multiplication M; //class object decleration
cout<<M.mul(8.2, 9.5)<<endl;
cout<<M.mul(10, 12)<<endl;
cout<<M.mul(5.6, 10.5);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
function 2 is called 77.9
function 1 is called 120
function 2 is called 58.8
In the above code, we define a mul() function two times. First, mul() first uses integers as parameters, while the second mul() function uses double as parameters. The mul() function is called based on the types of parameters.
Based on different sequences of parameters
In this method, we define two or more functions with the same name and have different sequences of parameters.
Code
#include <iostream>
using namespace std;
class multiplication
{
public:
double mul(int x, double y) //1
{
cout<<" function 1 is called ";
return x*y;
}
double mul(double x, int y) //2
{
cout<<" function 2 is called ";
return x*y;
}
double mul(double x, double y) //3
{
cout<<" function 3 is called ";
return x*y;
}
};
int main()
{
multiplication M; //class object decleration
cout<<M.mul(13, 12.3)<<endl;
cout<<M.mul(9.6, 4.3)<<endl;
cout<<M.mul(5.6, 8);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
function 1 is called 159.9
function 3 is called 41.28
function 2 is called 44.8
In the above code, we define a mul() function three times, but each mul() functions have a different sequence of parameters. The mul() function is called based on the sequence of parameters.
Causes of function overloading in c++
- Type Conversion.
- Function with default arguments
- Function with a pass-by-reference.
Type conversion
Code
#include<iostream>
using namespace std;
void print(int);
void print(float);
void print(int x)
{
cout<<"x="<<x<<endl;
}
void print(float y)
{
cout<<"y="<<y<<endl;
}
int main()
{
print(92);
print(10.2);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
In the above code, we get an error “call of overloaded ‘print(double)’ is ambiguous.” According to the above, learning print(10.2) will call the second function for its execution, but that is not going to happen. This is because the compile will take 10.2 as a double data type, and we didn’t write any function that overloads the double data type.
Function with default arguments
Code
#include<iostream>
using namespace std;
void print(int);
void print(int,int);
void print(int x)
{
cout<<"x="<<x<<endl;
}
void print(int x, int y=15)
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
int main()
{
print(12);
print(14);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
In the above code, we get an error “call of overloaded ‘print(int)’ is ambiguous.” This is because the compiler cannot distinguish between the first and second functions. After all, both the function is written in a way that they are going to accept one integer.
Function with a pass-by-reference
Code
#include<iostream>
using namespace std;
void print(int);
void print(int&);
void print(int x)
{
cout<<"x="<<x<<endl;
}
void print(int &y)
{
cout<<"y="<<y<<endl;
}
int main()
{
int x=42;
print(12);
print(x);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
In the above code, we get an error “call of overloaded ‘print(int&)’ is ambiguous.” As clear from the function code, one function will take an integer as input while the other takes a reference as a parameter. The compiler gets confused and unable to call any one of them for you instead of no error in the program.
Check out this article - Compile Time Polymorphism
Frequently asked questions
-
What is function overloading?
Ans: Function overloading is a feature of C++, where two or more functions can have the same name but different parameters.
-
Does C support function overloading like C++?
Ans: C does not support function overloading like C++.
-
What is the use of function overloading?
Ans: The function overloading is used to improve the readability of the code.
Key Takeaways
That's all about function overloading. This blog is over, but the thirst for learning is not over yet.
Check out more related articles:
Continue your journey of acquiring knowledge with our practice platform Coding Ninjas Studio to learn, attempt, read interview experiences, and much more. Till then, have a nice day and Happy Coding!