+ 3

Could someone explain this to me??

{ int foo(int x , int y) res = (x+y)} { int foo(double x , double y) res = (x*y) } int main(){ cout<<foo(3.8,8.0) }

23rd Apr 2017, 9:38 PM
Bushra Arif
Bushra Arif - avatar
9 ответов
+ 12
What is your doubt? PS. The code has errors.
23rd Apr 2017, 9:44 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 12
the compiler decides what function to use depending in the type of the arguments. In the example, the arguments are of type double, this means the second foo function is used. The result will be the multiplication of the arguments.
23rd Apr 2017, 10:04 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 11
Do you mean: how to make it work?
23rd Apr 2017, 9:56 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 11
Could you, please, post the code in the Code Playground so we can see it better?
23rd Apr 2017, 10:11 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
how to do it? i just can't understand it & yeah it have! but i think you could still do it?
23rd Apr 2017, 9:55 PM
Bushra Arif
Bushra Arif - avatar
+ 1
process behind the output
23rd Apr 2017, 10:00 PM
Bushra Arif
Bushra Arif - avatar
+ 1
its done thank you :)
23rd Apr 2017, 10:10 PM
Bushra Arif
Bushra Arif - avatar
0
its function overloading . but there is an error in your code... it goes like.. int foo(int x, int y) { sum= x+y; } double foo(double x, double y) { result=x*y; } int main() { cout<<foo(3.8,3.0); }
23rd Apr 2017, 9:57 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
look the working example of function overloading #include <iostream> using namespace std; void foo(int a, int b) { int sum=a+b; cout<<"Sum :"<<sum; } void foo(double a, double b) { double res=a*b; cout<<"Product is "<<res; } int main() { foo(3.8,8.0); return 0; }
23rd Apr 2017, 10:22 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar