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

  • Thread starter Thread starter cikon
  • Start date Start date
  • Tags Tags
    Data Input Type
AI Thread Summary
The discussion revolves around function overloading in C++ and the issue of determining the data type of a variable based on user input. The original poster presents a code snippet where they attempt to call an overloaded function `example()` with a variable `q`, which is not declared. Respondents clarify that `q` must be declared with a specific data type before it can be used. They emphasize that the type of `q` cannot be dynamically determined at runtime; it must be defined beforehand. To handle varying user inputs, the code should first analyze the input type and then call the appropriate overloaded function based on the determined type. This approach ensures that the correct version of `example()` is invoked according to the user's input.
cikon
Messages
2
Reaction score
0
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 ?
 
Technology news on Phys.org
cikon said:
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.
 
im sorry for not making it clear. act i want to 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.
 
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.
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top