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






Overload abs() three ways

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

1.Functions differ in number of parametersFunctions differ in number of parameters
2.Overload function: int and longOverload function: int and long
3.Create three functions called prompt( ) that perform this task for data of types int, double, and long
4.Overload function to accept integer or char * argumentOverload function to accept integer or char * argument
5.Overload the min() function.Overload the min() function.
6.Compute area of a rectangle using overloaded functions.Compute area of a rectangle using overloaded functions.
7.Overload function: int and floatOverload function: int and float
8.function overloading between int and string type