+ 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?
3 Réponses
+ 3
Thanks codemonkey for the answer
+ 1
Please correct your example to prevent misunderstanding Simangolwa Lifwatila
cin >> 4.32; // should be cin >> a;
cin >> 4; // should be cin >> b;
+ 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.