#Previous question #Index of C++ Questions #Next question

With the C++ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. For example:

double max( double d1, double d2 )
{
  return ( d1 > d2 ) ? d1 : d2;
}
int max( int i1, int i2 )
{
  return ( i1 > i2 ) ? i1 : i2;
}

The function max is considered an overloaded function. It can be used in code such as the following:

main()
{
  int    i = max( 12, 8 );
  double d = max( 32.9, 17.4 );
  return 0;
}

In the first case, where the maximum value of two variables of type int is being requested, the function max( int, int ) is called. However, in the second case, the arguments are of type double, so the function max( double, double ) is called.

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students