// overload2.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates some of the problems that can be caused as a result of // careless use of function overloading. The program is similar to the previous one // "overload1.cpp", but all the "divide" functions do have default parameters. See // the header file "overload2.h" for that. // Notice that the compilation could be successful even if there is // a potential for errors. Compilation errors will only occur when an ambiguous // call is made. // Key Points: 1) Function overloading & compiler confusion. #include "overload2.h" MathOperations::MathOperations( void ) { ires = 0; dres1 = dres2 = 0; } int MathOperations::divide ( int i1, int i2 ) { // divide the 1st passed parameter by the second one, and assign the result to // the private data ires cout << "\n Executing an integer type division. " << endl; ires = int(i1/i2); cout << " The result of dividing " < divide(double ) (1736) CC: "overload2.cpp", line 82: choice of MathOperations::divide()s: CC: "overload2.cpp", line 82: double MathOperations::divide(double , double ); CC: "overload2.cpp", line 82: double MathOperations::divide(double , int , double ); CC: "overload2.cpp", line 83: error: ambiguous call: MathOperations* -> divide() (1736) CC: "overload2.cpp", line 83: choice of MathOperations::divide()s: CC: "overload2.cpp", line 83: int MathOperations::divide(int , int ); CC: "overload2.cpp", line 83: double MathOperations::divide(double , double ); CC: "overload2.cpp", line 83: double MathOperations::divide(double , int , double ); CC: "overload2.cpp", line 84: error: ambiguous call: MathOperations* -> divide(int ,double ) (1736) CC: "overload2.cpp", line 84: choice of MathOperations::divide()s: CC: "overload2.cpp", line 84: int MathOperations::divide(int , int ); CC: "overload2.cpp", line 84: double MathOperations::divide(double , double ); CC: "overload2.cpp", line 84: double MathOperations::divide(double , int , double ); On VC++ overload2.cpp(82) : error C2668: 'divide' : ambiguous call to overloaded function overload2.cpp(83) : error C2668: 'divide' : ambiguous call to overloaded function overload2.cpp(84) : error C2666: 'divide' : 3 overloads have similar conversions */ // The result of running the program (ONLY AFTER THE ILLEGAL CALLS ARE COMMENTED OUT): /* Executing a double type division. The result of dividing 34.2 / 3.5 is: 9.77143 Executing an integer type division. The result of dividing 10 / 4 is: 2 Executing an addition then a double type division. The result of dividing 14.5 / 3.2 is: 4.53125 Executing an addition then a double type division. The result of dividing 14 / 3 is: 4.66667 Executing an integer type division. The result of dividing 4 / 4 is: 1 Executing an addition then a double type division. The result of dividing 7.2 / 3.9 is: 1.84615 */