C++ FAQ Celebrating Twenty-One Years of the C++ FAQ!!!
(Click here for a personal note from Marshall Cline.)
Section 15:
[15.10] How can I provide input for my class Fred?

Use operator overloading to provide a friend right-shift operator, operator>>. This is similar to the output operator, except the parameter doesn't have a const: "Fred&" rather than "Fred const&".

#include <iostream>

class Fred {
  friend std::istream& operator>> (std::istream& i, Fred& fred);
  int i_;    // Just for illustration

std::istream& operator>> (std::istream& i, Fred& fred)
  return i >> fred.i_;

int main()
  Fred f;
  std::cout << "Enter a Fred object: ";
  std::cin >> f;
Note that operator>> returns the stream. This is so the input operations can be cascaded and/or used in a while loop or if statement.