C++ Overloading (Function)
If we create two or more members having the same name but different in number or type of parameters, it is known as C++ overloading. In C++, we can overload:
- methods,
- constructors and
- indexed properties
Types of overloading in C++ are:
- Function overloading
- Operator overloading
C++ Function Overloading
Function Overloading is defined as the process of having two or more functions with the same name, but different parameters. In function overloading, the function is redefined by using either different types or number of arguments. It is only through these differences a compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the program because you don’t need to use different names for the same action.
Example: changing number of arguments of add() method
C++
// program of function overloading when number of arguments // vary #include <iostream> using namespace std; class Cal { public : static int add( int a, int b) { return a + b; } static int add( int a, int b, int c) { return a + b + c; } }; int main( void ) { Cal C; // class object declaration. cout << C.add(10, 20) << endl; cout << C.add(12, 20, 23); return 0; } // Code Submitted By Susobhan Akhuli |
30 55
Time complexity: O(1)
Space complexity: O(1)
Example: when the type of the arguments vary.
C++
// Program of function overloading with different types of // arguments. #include <iostream> using namespace std; int mul( int , int ); float mul( float , int ); int mul( int a, int b) { return a * b; } float mul( double x, int y) { return x * y; } int main() { int r1 = mul(6, 7); float r2 = mul(0.2, 3); cout << "r1 is : " << r1 << endl; cout << "r2 is : " << r2 << endl; return 0; } // Code Submitted By Susobhan Akhuli |
r1 is : 42 r2 is : 0.6
Time Complexity: O(1)
Space Complexity: O(1)
Function Overloading and Ambiguity
When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading ambiguity.
When the compiler shows the ambiguity error, the compiler does not run the program.
Causes of Ambiguity:
- Type Conversion.
- Function with default arguments.
- Function with pass-by-reference.
Type Conversion:-
C++
#include <iostream> using namespace std; void fun( int ); void fun( float ); void fun( int i) { cout << "Value of i is : " << i << endl; } void fun( float j) { cout << "Value of j is : " << j << endl; } int main() { fun(12); fun(1.2); return 0; } // Code Submitted By Susobhan Akhuli |
The above example shows an error “call of overloaded ‘fun(double)’ is ambiguous“. The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.
Function with Default Arguments:-
C++
#include <iostream> using namespace std; void fun( int ); void fun( int , int ); void fun( int i) { cout << "Value of i is : " << i << endl; } void fun( int a, int b = 9) { cout << "Value of a is : " << a << endl; cout << "Value of b is : " << b << endl; } int main() { fun(12); return 0; } // Code Submitted By Susobhan Akhuli |
The above example shows an error “call of overloaded ‘fun(int)’ is ambiguous“. The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).
Function with Pass By Reference:-
C++
#include <iostream> using namespace std; void fun( int ); void fun( int &); int main() { int a = 10; fun(a); // error, which fun()? return 0; } void fun( int x) { cout << "Value of x is : " << x << endl; } void fun( int & b) { cout << "Value of b is : " << b << endl; } // Code Submitted By Susobhan Akhuli |
The above example shows an error “call of overloaded ‘fun(int&)’ is ambiguous“. The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &).
Functions in C++
A function is a set of statements that takes input, does some specific computation, and produces output. The idea is to put some commonly or repeatedly done tasks together to make a function so that instead of writing the same code again and again for different inputs, we can call this function.
In simple terms, a function is a block of code that runs only when it is called.
Syntax:
Example:
C++
// C++ Program to demonstrate working of a function #include <iostream> using namespace std; // Following function that takes two parameters 'x' and 'y' // as input and returns max of two input numbers int max( int x, int y) { if (x > y) return x; else return y; } // main function that doesn't receive any parameter and // returns integer int main() { int a = 10, b = 20; // Calling above function to find max of 'a' and 'b' int m = max(a, b); cout << "m is " << m; return 0; } |
m is 20
Time complexity: O(1)
Space complexity: O(1)