Lesson 11 | Overloading I/O operators |
Objective | Reference to stream needed when overloading I/O operators. |
Overloading I/O Operators
Explain why using a reference to a stream is needed when overloading the input/output operators.
In keeping with the spirit of OOP, it is important to overload
<<
and
>>
to output and input
user-defined types as well as native types. The operators
<<
and
>>
have two arguments:
- an
ostream&
istream&
respectively
Overloading input-output Operators in C++
C++ input Class istream
Input is returned to an object of type
istream
as described in
iostream.h
.
The operator
>>
is overloaded in this class to perform input conversions from standard types.
The operator is left associative and returns a value of type
istream&
.
The standard input
istream
corresponding to
stdin
is
cin
.
The effect of executing a simple input statement, such as
cin >> x >> i;
is to read from standard input (normally the keyboard) a value for
x
and then a value for
i
. White space is ignored.
The class
istream
contains public members such as:
istream& operator>>(int& i);
istream& operator>>(long& i);
istream& operator>>(double& x);
istream& operator>>(char& c);
istream& operator>>(char* s);
istream& get(char& c);
istream& get(char* s, int n, char c = '\n');
istream& getline(char* s, int n, char c = '\n');
istream& read(char* s, int n);
The member function
get(char& c)
inputs the character representation to
c
, white-space characters included.
The member function
get(char* s, int n, char c = '\n')
inputs at most
n
characters into the string pointed at by
s
, up to the specified delimiter character
c
or an end-of-file. The member function
getline()
works like
get(char* s, int n, char c = '\n')
except it discards rather than keeps the delimiter character in the designated istream. The member function
read(char* s, int n)
inputs at most
n
characters into the string pointed at by
s
.
Since these are member functions, they can be used as follows:
cin.get(c); //one character
cin.get(s, 40); //length 40 or terminated by '\n'
cin.get(s, 10, '*') //length 10 or terminated by *
cin.getline(s, 40) //same as get but '\n' is discarded
Reference to a Stream
The ADT and the operators must produce an
ostream&
or an
istream&
respectively.
Whenever overloading
<<
or
>>
, you want to use a reference to a stream and return a reference to a stream because you do not want to copy a stream object.
Overloading operator << is similar to overloading operator+ since they are both binary operators, except that the parameter types are different.
Consider the expression cout << cPoint.
Question: If the operator is <<, what are the operands?
Answer: The left operand is the cout object, and the right operand is your Point class object.
cout is actually an object of type ostream. Therefore, the overloaded function will look like this:
friend ostream& operator<< (ostream &out, Point &cPoint);
Implementation of operator<< is fairly straightforward, because C++ already knows how to output doubles using operator<<,
and our members are all doubles, we can simply use operator<< to output the member variables of our Point.
Here is the above Point class with the overloaded operator<<.