+ 2

Function overloading in c++

#include <iostream> using namespace std; void printNumber(int x) { cout << "Prints an integer: " << x << endl; } void printNumber(float x) { cout << "Prints a float: " << x << endl; } int main() { int a = 16; printNumber(a); printNumber(10.0); } In this code, when we write something like printNumber(10.0) , 10.0 will be processed as double ,not float . So there's an error "call of the overloaded function is ambiguous. This is ok. But in the below program, why when we write sum(10.0,n) it runs and there is no problem with 10.0 . Why in this program 10.0 will be processed as double. #include <iostream> using namespace std; void sum(float a, int b) { cout<<"diff="<<a-b<<endl; } void sum(int a,float b) { cout<<"sum="<<a+b<<endl; } int main() { int x=40,n=30; //float y=10.5,m=10.5; sum(10.0,n); sum(x,11.0); return 0; } Can anyone tell the reason please?

4th Mar 2020, 8:51 AM
Armina
Armina - avatar
18 odpowiedzi
+ 3
In the first code there is no overloaded function with double parameter so compiler should use type casting. double may be casted to int or float. Compiler doesn't know what function it should use. In the second code compiler use the most appropriate overloaded function (with less number of type casting): for sum(10.0, n): void sum(float a, int b) only first argument must be casted to float (10.0 -> float) = 1 casting void sum(int a,float b) both of arguments must be casted (10.0->int, n-> float) = 2 casting so compiler will choose void sum(float a, int b) with 1 type casting for sum(x,11.0): void sum(float a, int b) both of arguments must be casted (x->float, 11.0- >int) = 2 casting void sum(int a,float b), only second argument must be casted to float (11.0->float) = 1 casting so compiler will choose void sum(int a,float b) with 1 type casting
4th Mar 2020, 9:13 AM
andriy kan
andriy kan - avatar
+ 2
In c, c++, java, default type for decimal values is double type so if you specify 10.0, it treated as double type, you need to put 10.0f to treat it as float.
4th Mar 2020, 9:03 AM
Jayakrishna 🇮🇳
+ 2
As there is no match for double type, there is implicit type casting taking place.. Since there is one match of int type.. In your first case, they is ambiguity to cast to int or float... Edit: May be i missing something, I try to update my answer later if any missing..
4th Mar 2020, 9:13 AM
Jayakrishna 🇮🇳
+ 2
Armina, 1. Yes, when compiler can't find match it uses type casting. 2. Yes, compiler uses type casting and for not overloaded functions: //.... void func(int a) { cout << a << endl; } int main() { func(10.0); return 0; } will be compiled and run successfully. If function is overloaded then a compiler will choose most appropriate function (function with minimal number of argument type casting). 3. Didn't understand the third question: Compiler always tryes to cast type of parameter of function to type of the declared argument for each overloaded function (that can be used with same number of arguments), if match wasn't declared. If type can be casted (i.e., int to float, int to double,...) then it wil be casted. After trying of all overloaded functions comipler will choose most appropriate one.
4th Mar 2020, 10:01 AM
andriy kan
andriy kan - avatar
+ 2
Armina As @andriy kan as specified in first post.. It casts it try to cast if there is a match in arguments, and if casting is possible without loss of data.. If there is loss in data, compiler give you warnings.. int to double casting is possible since there is no loss of data (in case of function calls, we can consider it as, if numbers of implicit castings < no. Of arguments, then then compiler type cast it in c++.) Hope it helps you.. You're Wel come..
4th Mar 2020, 10:37 AM
Jayakrishna 🇮🇳
+ 2
Armina, Yes, compiler can cast int to double, or double to int, or even double to char! for example, the next code will successfully compile and run, but will output a wrong result: #include <iostream> using namespace std; void func(char a) { double d = a; cout << d << endl; } int main() { double d = 257.5; func(d); return 0; } output will be 1, and not 257 You can try it: https://code.sololearn.com/cWpPQwbZGVgt So, you should be careful with implicit type casting.
4th Mar 2020, 11:06 AM
andriy kan
andriy kan - avatar
+ 2
Armina, char has only 1 byte in size. At first double is converted to integer (using special CPU commands) It will 257. To store 257 you need at least 2 bytes. The binary representation of 257 is next (two least significant bytes): 00000001 00000001 or that bytes in decimal: 1 1 you can get 257 from that bytes by multiplying each byte by 2^(8 * index of byte), where ^ is power sign and summing that numbers up: 257 =( 1 * 2^(8 * 1)) + (1 * 2^(8 * 0)) = 256 + 1 since char has only 1 byte in size, a compiler just take only the least significant byte from that integer which is 1 and stores it to a char variable (it can't fit more than 1 byte to it). for 258 it will be 2 (256 + 2) for 259 it will be 3 (256 + 3) and so on... till 383 (256 + 127) but for 384 it can be -128, if char is signed because 384 = (256 + 128) and 128 is the representation of signed char -128 385 = -127 (256 + 129, 129 = -127) 511 = -1 (256 +255, 255 = -1)
4th Mar 2020, 11:56 AM
andriy kan
andriy kan - avatar
+ 2
Armina Yes. If we try to assign higher type to lower type, so there give error of loss data.. Like double i=12.5; int d=i; (also double to float of your example). But sorry, I confused, this is happence in java.. Java not allow data loss implicitly.. But in C, C++ there takes implicit type casting if there is no ambiguity and possibility of convertion.. So int to float, int to double, float to double.. Like wise possible.. But it simply if we assign double to int, if int is 4bytes and double is 8 byte, then int value is assigned first 4 bytes of double value to int... Hope this helps you in understanding.. You are Wel come...
4th Mar 2020, 12:40 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna andriy kan Thanks for a million. You helped me alot.
4th Mar 2020, 1:28 PM
Armina
Armina - avatar
+ 1
Jayakrishna As i explained in my question, i know the default is double but my question is:why in the second program, 10.0 will be processed as float not double , so there will be no error and the program runs??
4th Mar 2020, 9:08 AM
Armina
Armina - avatar
+ 1
So in the function overloading when compiler find no match for the date type , use type casting to find the appropriate function , am i right? i mean does compiler use type casting only for overloading functions or does it use even for calling the functions which are not overloaded?? And also is there a limit for type casting, for example :to find the appropriate function would the compiler type cast int to double or int to float ??
4th Mar 2020, 9:37 AM
Armina
Armina - avatar
+ 1
andriy kan Thanks alot for your explanation In my third question, i mean than can the complier type cast int to double ?
4th Mar 2020, 10:14 AM
Armina
Armina - avatar
+ 1
Jayakrishna Thanks alot
4th Mar 2020, 10:19 AM
Armina
Armina - avatar
+ 1
Jayakrishna I didn't understand what do you mean by data loss. For example if we have a number with 10 digits (it's double) and compiler wants to type cast it to float , it will be a data loss? In this case does the compiler give us only a warning or an error?
4th Mar 2020, 10:51 AM
Armina
Armina - avatar
+ 1
andriy kan Thanks for a million and sorry for the number of my questions I know char is from -128 to 127 but I didn't understand why the output is 1?
4th Mar 2020, 11:25 AM
Armina
Armina - avatar
+ 1
Armina, You are welcome
4th Mar 2020, 1:55 PM
andriy kan
andriy kan - avatar
+ 1
#include <iostream> #include <string> using namespace std; //complete the function void add(int a, int b) { cout<<a+b<<endl; } //overload it to sum doubles void add(double a, double b) { cout<<a+b; } int main() { //calling add(5,6); add(1.2, 6.5); return 0; } Good Luck
25th Jan 2022, 5:37 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
#include <iostream> #include <string> using namespace std; //complete the function void add(int x, int y) { cout<<x+y<<endl; } //overload it to sum doubles void add(double x, double y) { cout<<x+y<<endl; } int main() { //calling add(5,6); add(1.2, 6.5); return 0; }
11th Sep 2023, 12:18 AM
AMINE MOUTAOUAKKIL