[C++] Question regarding input data type. Helppp :|


by cikon
Tags: cin, class, overloading function, unknown data input
cikon
cikon is offline
#1
Dec18-12, 02:27 AM
P: 2
Helo,

im doing overloading function.

example
class a
{
..... example(int a)
...... example(double b)
};

int main()
{
a. b;
cout << "Enter value";
cin>>q;
b.example(q)

}

what is q's data type ?
Phys.Org News Partner Science news on Phys.org
Simplicity is key to co-operative robots
Chemical vapor deposition used to grow atomic layer materials on top of each other
Earliest ancestor of land herbivores discovered
Mark44
Mark44 is online now
#2
Dec18-12, 08:47 AM
Mentor
P: 20,948
Quote Quote by cikon View Post
Helo,

im doing overloading function.

example
class a
{
..... example(int a)
...... example(double b)
};

int main()
{
a. b;
cout << "Enter value";
cin>>q;
b.example(q)

}

what is q's data type ?
You tell us - it's your code, which by the way won't compile, since q is not declared.
cikon
cikon is offline
#3
Dec18-12, 09:45 AM
P: 2
im sorry for not making it clear. act i wanna ask, what is the data type of unknown parameter. like q. it can be either int or double. and we do not know what is the type will be inserted by the user.

Borek
Borek is offline
#4
Dec18-12, 10:50 AM
Admin
Borek's Avatar
P: 22,665

[C++] Question regarding input data type. Helppp :|


No, it is not unknown - you have to declare it before using it in main(). Once it is declared and example(q) is called compiler already knows q type and calls the correct overload.

Edit: I think I know what you mean, you have no idea what the user will input beforehand. You have to write code that will analyze the input before storing the information in the variable. q type is not decided during runtime depending on what the user enters, it must be declared earlier. If you need separate example() functions for different input types, you need to call them AFTER checking what was the user input type.


Register to reply

Related Discussions
Calculate input power from the data Advanced Physics Homework 7
Is data input clocked? Engineering, Comp Sci, & Technology Homework 19
how to test input type before storing C++ Programming & Computer Science 7
struct data input problem.. Programming & Computer Science 6
Matlab- input arguments of type 'double' ???? Math & Science Software 2