Function overloading:-
-> overloading refers to the use of the same thing for different purposes. C++ also permitts overloading of function.
-> That means we can use the same function name to create functions that perform a variety of different tasks.
-> This is known as Function polymorphism in oop.
-> 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.
// Sample Cpp program for function overloading.
#include<iostream>
using namespace std;
show()
{
cout<<"show called\n";
}
show(int a)
{
cout<<"show called"<<" "<<a<<endl;
}
show(int a,int b)
{
cout<<"show called"<<" "<<a<<" "<<b;
}
int main()
{
show();
show(22);
show(33,66);
return 0;
}
INPUT/OUTPUT:
show called
show called 22
show called 33 66
// CPP Program of function overloading with different types of arguments.
#include<iostream>
using namespace std;
int volume(int );
double volume(double ,int);
long volume(long,int,int);
int main()
{
cout<<"volume of cube:"<<volume(10);
cout<<"\nvolume of cylinder:"<<volume(7.5,8);
cout<<"\nvolume of Rectangle prism:"<<volume(10,20,8);
{
return(s*s*s); // cube
}
double volume(double r,int h)
{
return(3.14159*r*r*h); // cylinder
}
long volume(long l,int b,int h)
{
return(l*b*h); // Rectangle prism
return 0;
}
INPUT/OUTPUT:
volume of cube:1000
volume of cylinder:1413.72
volume of Rectangle prism:1600
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.
When the compiler shows the ambiguity error, the compiler does not run the program.
Causes of Function Overloading:
1. Type Conversion.
2. Function with default arguments.
3. Function with pass by reference.
1. Type Conversion:
Let's see a simple example.
#include<iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(float j)
{
std::cout << "Value of j is : " <<j<< std::endl;
}
int main()
{
fun(12);
fun(1.2);
return 0;
}
-> 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.
2. Function with Default Arguments:
lets see a simple example
#include<iostream>
using namespace std;
void fun(int);
void fun(int,int);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(int a,int b=9)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(12);
return 0;
}
-> 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).
3. Function with pass by reference:
Let's see a simple example.
#include <iostream>
using namespace std;
void fun(int);
void fun(int &);
int main()
{
int a=10;
fun(a); // error, which f()?
return 0;
}
void fun(int x)
{
std::cout << "Value of x is : " <<x<< std::endl;
}
void fun(int &b)
{
std::cout << "Value of b is : " <<b<< std::endl;
}
-> 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 &).
-> overloading refers to the use of the same thing for different purposes. C++ also permitts overloading of function.
-> That means we can use the same function name to create functions that perform a variety of different tasks.
-> This is known as Function polymorphism in oop.
-> 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.
// Sample Cpp program for function overloading.
#include<iostream>
using namespace std;
show()
{
cout<<"show called\n";
}
show(int a)
{
cout<<"show called"<<" "<<a<<endl;
}
show(int a,int b)
{
cout<<"show called"<<" "<<a<<" "<<b;
}
int main()
{
show();
show(22);
show(33,66);
return 0;
}
INPUT/OUTPUT:
show called
show called 22
show called 33 66
// CPP Program of function overloading with different types of arguments.
#include<iostream>
using namespace std;
int volume(int );
double volume(double ,int);
long volume(long,int,int);
int main()
{
cout<<"volume of cube:"<<volume(10);
cout<<"\nvolume of cylinder:"<<volume(7.5,8);
cout<<"\nvolume of Rectangle prism:"<<volume(10,20,8);
}
int volume(int s )
{
return(s*s*s); // cube
}
double volume(double r,int h)
{
return(3.14159*r*r*h); // cylinder
}
long volume(long l,int b,int h)
{
return(l*b*h); // Rectangle prism
return 0;
}
INPUT/OUTPUT:
volume of cube:1000
volume of cylinder:1413.72
volume of Rectangle prism:1600
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.
When the compiler shows the ambiguity error, the compiler does not run the program.
Causes of Function Overloading:
1. Type Conversion.
2. Function with default arguments.
3. Function with pass by reference.
1. Type Conversion:
Let's see a simple example.
#include<iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(float j)
{
std::cout << "Value of j is : " <<j<< std::endl;
}
int main()
{
fun(12);
fun(1.2);
return 0;
}
-> 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.
2. Function with Default Arguments:
lets see a simple example
#include<iostream>
using namespace std;
void fun(int);
void fun(int,int);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(int a,int b=9)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(12);
return 0;
}
-> 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).
3. Function with pass by reference:
Let's see a simple example.
#include <iostream>
using namespace std;
void fun(int);
void fun(int &);
int main()
{
int a=10;
fun(a); // error, which f()?
return 0;
}
void fun(int x)
{
std::cout << "Value of x is : " <<x<< std::endl;
}
void fun(int &b)
{
std::cout << "Value of b is : " <<b<< std::endl;
}
-> 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 &).