/* C++ Program to return absolute value of variable types
integer and float using function overloading */
#include <iostream.h>
int absolute(int);
float absolute(float);
int main() {
int a = -5;
float b = 5.5;
cout<<"Absolute value of "<<a<<" = "<<absolute(a)<<endl;
cout<<"Absolute value of "<<b<<" = "<<absolute(b);
return 0;
}
int absolute(int var) {
if (var < 0)
var = -var;
return var;
}
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}
Output
Absolute value of -5 = 5
Absolute value of 5.5 = 5.5
In above example, two functions absolute() are overloaded. Both take single argument but one takes integer type argument and other takes floating point type argument. Function absolute() calculates the absolute value of argument passed and returns it.