A: Use operator overloading to present a friend right-shift operator, operator>>. It is similar to the output operator, except the parameter doesn't contain a const: "Fred&" instead of "const Fred&".
#include class Fred {
public:
friend std::istream& operator>> (std::istream& i, Fred& fred);
... private:
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 down that operator>> returns the stream. It is so the input operations may be cascaded and/or utilized in a while loop or if statement.