Overload abs() three ways : Function Overloaded « Function « C++
- C++
- Function
- Function Overloaded
Overload abs() three ways

#include <iostream>
using namespace std;
int abs(int n);
double abs(double n);
int main()
{
cout << "Absolute value of -10: " << abs(-10) << endl;
cout << "Absolute value of -10.01: " << abs(-10.01) << endl;
return 0;
}
int abs(int n)
{
cout << "In integer abs()\n";
return n<0 ? -n : n;
}
double abs(double n)
{
cout << "In double abs()\n";
return n<0 ? -n : n;
}
Related examples in the same category