Input and Output
That brings us to I/O of an object. We'll consider output first.
Here we overload the << method so that we can output a Dollar object in the same way as any other. This implementation assumes an existing string conversion method.
ostream & operator <<(ostream &out, Dollar &d) { out << static_cast<string>(d); return out; }
Notice that we return the output stream as a return value. This is very important because it makes "stacking" possible, as shown here:
cout << "The value of pie is " << pie << "." << endl; // Prints: The value of pie is $3.14.
Of course, we need not use a string conversion on Dollar at all. We could choose to access the individual members of Dollar (making a friend declaration necessary) and output in some totally different format:
class Dollar { //... friend ostream & operator <<(ostream &, Dollar &); } ostream & operator <<(ostream &out, Dollar &d) { out << d._amt << " dollars and " << d._cents << " cents"; return out; }
Input is a little more problematic because it will usually involve parsing the input in some way. I would recommend using cin.get to retrieve a character at a time and processing it with a simple state machine approach.