Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 18, 2012 #1
    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 ?
     
  2. jcsd
  3. Dec 18, 2012 #2

    Mark44

    Staff: Mentor

    You tell us - it's your code, which by the way won't compile, since q is not declared.
     
  4. Dec 18, 2012 #3
    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.
     
  5. Dec 18, 2012 #4

    Borek

    User Avatar

    Staff: Mentor

    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: Dec 18, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C++] Question regarding input data type. Helppp :|
  1. C++ question (Replies: 2)

  2. C++ question (Replies: 2)

Loading...