Kartik Sharma
Kartik Sharma

Reputation: 723

Working of program in Function Overloading

I want to ask why this program does not generate a compile time error?

int Add(int x, int y){
    return (x+y);
}

double Add(double x, double y, double z){
    return (x+y);
}

int main()
{
    cout<<Add(5,6);
    cout<<Add(5.5,6.6);
    return 0;
}

Upvotes: 1

Views: 109

Answers (5)

Best Nyambe
Best Nyambe

Reputation: 1

Guys this will generate a compile error 'Add' : no overloaded function takes 2 arguments You need three arguments for the ADD functions- both for int and double- to be utilized.

While I appreciate your answers on this, you must understand that function overload in C++ done like this requires exact number of parameters to be specified in the calling.

Visual Studio won't allow u to compile the above source code. Pass three arguments to the calling of functions in main function.

. . . cout << Add(5,6,7) << endl; cout << Add(5.5,6.6,2) << endl; . . .

Upvotes: 0

GMasucci
GMasucci

Reputation: 2892

The solution comes down to the number of arguments:

For doubles you have

double Add(double x, double y, double z){
    return (x+y);
}

this function requires only/exactly 3 variables before it will be utilised, so when you specify only 2 arguments, there is only one choice available:

int Add(int x, int y){
    return (x+y);
}

Should you want to make it multi-purpose you would have to change it to

double Add(double x, double y, double z = 0.0){
    return (x+y+z);
}

This will allow you to add 2 or 3 doubles together as needed, as 2 double added is the same as 2 doubles added to 0.0.

The argument for z (double z = 0.0) is simply a default value declaration: most compilers work on the basis that, working right to left on the parameter list you can omit parameters which have defaults, therefore I could do the following:

double Add(double x= 0.0, double y=0.0, double z = 0.0)

and each of the following would be valid calls:

Add();
Add(1.0);
Add(1.1,2.2);
Add(1.1,2.2,3.3);
Add(1);
Add(1,2);
Add(1,2,3);

And the values which we do not specify will be in place by utilising the default values we defined earlier (0.0).

Example usage of modified function

#include <iostream>
using namespace std;

double Add(double x, double y, double z = 0.0){
    return (x+y+z);
}
int main() {
    cout << Add(1.1,2.1,3.1) << endl;
    cout << Add(2,3) << endl;
    cout << Add(1.0, 2.12345) << endl;
    return 0;
}

Hope this helps, but let me know if you need more information or detail :)

Upvotes: 0

Danvil
Danvil

Reputation: 23001

Add(5,6); calls int Add(int x, int y). That is clear enough.

Now Add(5.5,6.6) looks for a proper matching function. It finds one with two arguments and one with three. Now it checks if it can use the function with two arguments. And indeed it can convert double to int. So it again uses int Add(int x, int y).

If you would provide an double Add(double x, double y), it would find two functions with two arguments and checks which one "matches best". This would then be double Add(double x, double y).

Upvotes: 4

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

When you call

Add(5.5,6.6);

it gets mapped to function signature

int Add(int x, int y);

so, 5.5 -> 5 and 6.8 -> 6 conversion takes place and you get the answer as 11 (5 + 6).

Upvotes: 0

Because C++ defines an implicit conversion from double to int, which gets applied in the second call.

See C++11[conv.fpint]§1:

A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.

Upvotes: 1

Related Questions