FUNCTION OVERLOADING | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

FUNCTION OVERLOADING

#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; cin >> a; float b; cin >> b; printNumber(a); printNumber(b); } I tried working a little bit with the code and I discovered that once one inputs a floating point number where there is supposed to be an int, it rounds off the number to its correct syntax i.e int a; cin >> 4.32; //cout is 4 float b; cin >> 4; // cout is 0.4 Anyone who knows why it doesn't give an error but rather just returns the correct value?

26th Jul 2020, 4:52 AM
Simangolwa Lifwatila
Simangolwa Lifwatila - avatar
3 Answers
+ 3
Thanks codemonkey for the answer
26th Jul 2020, 7:08 PM
Simangolwa Lifwatila
Simangolwa Lifwatila - avatar
+ 1
Please correct your example to prevent misunderstanding Simangolwa Lifwatila cin >> 4.32; // should be cin >> a; cin >> 4; // should be cin >> b;
26th Jul 2020, 5:39 AM
Ipang
+ 1
Ipang I was trying to illustrate the question in a way that will be understood thats why my syntax was like that. But thanks for that.
26th Jul 2020, 7:05 PM
Simangolwa Lifwatila
Simangolwa Lifwatila - avatar