Programmers frequently overload the inserter (or insertion) and extractor (or extraction) operators because they are the main output and input functions for C++ programs. Each function follows a fixed pattern. While the pattern is rigid and unchanging, some elements are flexible and left to the programmer to name. Specifically, programmers may choose the parameter names, and the class for which the operator is overloaded determines the type-name of the second parameter. Everything else in the pattern is fixed and required. It's easier to see the pattern with a simple example.
We finish off this section with complete implementations of operator<< and operator>> for the fraction class:
class fraction { private: int numerator; int denominator; public: . . . friend ostream& operator<<(ostream& out, fraction& f); friend istream& operator>>(istream& in, fraction& f); . . . }; |
ostream& operator<<(ostream& out, fraction& f) { out << f.denominator << "/" << f.numerator; return out; } istream& operator>>(istream& in, fraction& f) { cout << "Please enter the numerator part: "; in >> f.numerator; cout << "Please enter the denominator part: "; in >> f.denominator; return in; } |
(a) | (b) |
fraction left; fraction right; cin >> left; cin >> right; fraction result = left + right; cout << result << endl; |
|
(c) | (d) |
friend
keyword is always a part of the function declaration (i.e., the prototype).
friend
keyword does not appear with the definitioncin
is passed into the first parameter, in, and left (referring to the left-hand operand of + in (c)) is passed to fcout
is passed to the second parameter, out, and result is passed to fThe example above illustrates "Please enter . . ." prompts in the extractor operator, but is doing so appropriate? Sure, so long as the operator>> is only used to read input from the console, the prompts work well and are appropriate. But by the end of the semester, we will learn that an istream
can represent many input sources. For example, it is possible to use istreams
and operator>>
to read input from files. So what happens if the operator>>
is nested inside of some kind of loop that ultimately reads megabytes of data?
Writing output to the console is a relatively slow process. So, even if we don't mind the "clutter" appearing on the console screen for each prompt, the mere fact that a program repeatedly writes a prompt can slow it down. I recommend putting any input prompts in the client code. The client "knows" the data source and whether to prompt or not.