CIS 22 - Data Structures: Operator Overloading - Comments on Example
airtime operator++
()
// prefix version
airtime operator++
(int)
// postfix version
You do not actually supply a value for this dummy argument; the mere inclusion of the int is enough to let the compiler know it's postfix.
The postfix function operator++(int) operates differently from the prefix function operator++(). The postfix version must remember the original value of its object, increment the object, and then return the original value, not the new value. This is accomplished by creating a temporary object, temp, that is initialized to the original value of the object. After the object is incremented, temp, is returned.
2. Overloading the << and >> operators for cout and cin.
The operators
take two arguments, both passed by reference. The first is an object of
isteam
(for >>;
often this is cin)
or of ostream
(for <<;
often this is cout).
The second argument is the object to be displayed, an object of class airtime
in this example. The fact that the stream and airtime object are passed
by reference allows them to be modified by the function. The >>
operator takes input from the stream specified in the first argument and
copies it into the member data of the object specified by the second argument.
The <<
operator copies the data from the object specified by the second argument
and sends it into the stream specified by the first argument.
The operator
<< () and operator
>>() functions must be friends of the airtime
class, because the istream and ostream objects appear on the left side
of the operator. They return, by reference, an object of istream
(for >>) or ostream (for <<).
These return values permit changing so that more than one value can be
input or output in a single statement.
Michael Gorenburg - Spring '98