Write the definitions for three function named max. Each receives two parameters, of the same type, and returns the larger of the two values. Define one of these functions to apply to type double, another to type int and a third to type char.

2
Write the definitions for three function named max. Each receives two parameters, of the same type, and returns the larger of the two values. Define one of these functions to apply to type double, another to type int and a third to type char.

LANGUAGE: C++

CHALLENGE:

Write the definitions for three function named max. Each receives two parameters, of the same type, and returns the larger of the two values. Define one of these functions to apply to type double, another to type int and a third to type char.

SOLUTION:


int Max(int iInput){
   static int iHighest=0;
   if (iInput>iHighest)
   iHighest=iInput;
  return iHighest;
}

2 comments

  • Matt says:

    This function only has 1 input parameter and is named “Max” not max. Also, this function sets iHighest to 0, so you are always comparing your input to 0 and not to the other variable. This would also not be overloaded, as this is only one function with the given name.

    • Matt says:

      Correct answer would be this:
      int max(int a, int b)
      {
      return ((a > b) ? a : b);
      }

      double max(double a, double b)
      {
      return ((a > b) ? a : b);
      }

      char max(char a, char b)
      {
      return ((a > b) ? a : b);
      }

Add comment