OOP - Week 8, Lecture 3: Friend Functions and Stream Operators
Printing and Reading a Point
So far, to show a point we call a function: a.print();, and there is no way to read one from the user. For an int we write cout << x to print and cin >> x to read, and we can chain them: cout << "x = " << x << endl;. We want the same for Point:
Point a(2, 3);
cout << a << endl; // print: we want (2, 3)
cin >> a; // read: two numbers go into x and y of a
We also have a problem from Lecture 1: 3 * b does not compile. The two problems have the same cause. A member operator is always called on the object on its left. In cout << a and in cin >> a, the object on the left is cout or cin, which belongs to the library, not to Point. (cout is an object of the library type ostream, and cin is an object of the type istream.) In 3 * b, the left side is an int. In both cases the left side is not a Point, so the operator cannot be a member of Point.
The solution is a global function with two parameters, one for each side. To read the private x and y, the class declares the function as a friend, as in Week 5.
1. The Operator <<
A global operator function takes both operands as parameters. For cout << a, the left side is the stream and the right side is the point:
ostream& operator<<(ostream& out, const Point& p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}
It is a global function: there is no Point:: before the name, and there is no this. It reads p.x and p.y, which are private, so the class declares it as a friend. As in Week 5, the function also needs a prototype after the class, in the header:
class Point {
friend ostream& operator<<(ostream& out, const Point& p);
};
ostream& operator<<(ostream& out, const Point& p);
Without the line with friend, the function does not compile: 'x' is a private member of 'Point'.
The function returns out, the stream, as a reference. That makes the chain work: cout << a << endl means (cout << a) << endl. The first << returns cout itself, and the second one writes to the same cout. If the return type were void, the second << would have nothing to work on, and the chain would not compile. This is the same idea as return *this for += in Lecture 1.
The parameter p is a const reference, so the point is not copied and not changed. This is why cout << a + b works too: the result of a + b is a temporary Point, and a const reference can hold it.
2. The Operator >>
Reading is the opposite. The stream is istream, and the point is not const, because the function changes it:
istream& operator>>(istream& in, Point& p) {
in >> p.x >> p.y;
return in;
}
It also needs to be a friend, and it returns the stream, so cin >> a >> b works. The function reads two numbers, in this order, into x and y. With it, a program can read a point from the keyboard in one line:
Point c;
cin >> c; // the user types: 7 -2
cout << c << endl; // (7, -2)
The example on this page reads its points with cin in the same way.
3. A Number on the Left: 3 * b
In Lecture 1, b * 3 worked and 3 * b did not. The member operator* always has the object on the left. For 3 * b, we write a global function with the int as the first parameter:
Point operator*(int times, const Point& p) {
return Point(times * p.x, times * p.y);
}
It is a friend too, because it reads p.x and p.y. Now both orders work: b * 3 calls the member, and 3 * b calls the global function.
The same pattern works for any operator with a number on the left, such as +: a global function operator+(int, const Point&) makes 5 + p possible.
A global operator function needs to be a friend only when it reads the private members. If it used only public functions, an ordinary global function would be enough. A friend can read and change the private members, so, as in Week 5, use it only when it is needed.
4. Putting It Together
This is the Point of Lecture 2, with three new global functions: <<, >> and * with an int on the left. Click Changes since Lecture 2 to see exactly which lines are new. main reads three points with cin >> a >> b and cin >> c, prints them with <<, chains several << in one line, and uses 3 * b.
The code viewer cannot type, so it shows the output for the input 2 3 1 4 7 -2. Run the program on your computer and type your own numbers.
#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(int px = 0, int py = 0);
Point operator+(const Point& other) const;
Point operator-(const Point& other) const;
Point operator*(int times) const;
Point& operator+=(const Point& other);
Point& operator-=(const Point& other);
bool operator==(const Point& other) const;
bool operator!=(const Point& other) const;
bool operator<(const Point& other) const;
bool operator>(const Point& other) const;
bool operator<=(const Point& other) const;
bool operator>=(const Point& other) const;
Point& operator++();
Point operator++(int);
void print() const;
friend ostream& operator<<(ostream& out, const Point& p);
friend istream& operator>>(istream& in, Point& p);
friend Point operator*(int times, const Point& p);
};
ostream& operator<<(ostream& out, const Point& p);
istream& operator>>(istream& in, Point& p);
Point operator*(int times, const Point& p);
#endif#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(int px, int py) : x(px), y(py) {
}
Point Point::operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
Point Point::operator-(const Point& other) const {
return Point(x - other.x, y - other.y);
}
Point Point::operator*(int times) const {
return Point(x * times, y * times);
}
Point& Point::operator+=(const Point& other) {
x += other.x;
y += other.y;
return *this;
}
Point& Point::operator-=(const Point& other) {
x -= other.x;
y -= other.y;
return *this;
}
bool Point::operator==(const Point& other) const {
return x == other.x && y == other.y;
}
bool Point::operator!=(const Point& other) const {
return !(*this == other);
}
bool Point::operator<(const Point& other) const {
return x < other.x || (x == other.x && y < other.y);
}
bool Point::operator>(const Point& other) const {
return other < *this;
}
bool Point::operator<=(const Point& other) const {
return !(other < *this);
}
bool Point::operator>=(const Point& other) const {
return !(*this < other);
}
Point& Point::operator++() {
x++;
y++;
return *this;
}
Point Point::operator++(int) {
Point old = *this;
++(*this);
return old;
}
void Point::print() const {
cout << "(" << x << ", " << y << ")" << endl;
}
ostream& operator<<(ostream& out, const Point& p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}
istream& operator>>(istream& in, Point& p) {
in >> p.x >> p.y;
return in;
}
Point operator*(int times, const Point& p) {
return Point(times * p.x, times * p.y);
}#include <iostream>
#include "Point.h"
using namespace std;
int main() {
Point a;
Point b;
cin >> a >> b;
cout << a << endl;
cout << a + b << endl;
cout << "a = " << a << ", b = " << b << endl;
cout << 3 * b << endl;
Point c;
cin >> c;
cout << "c = " << c << endl;
return 0;
}#ifndef POINT_H
#define POINT_H
class Point {
private:
int x;
int y;
public:
Point(int px = 0, int py = 0);
Point operator+(const Point& other) const;
Point operator-(const Point& other) const;
Point operator*(int times) const;
Point& operator+=(const Point& other);
Point& operator-=(const Point& other);
bool operator==(const Point& other) const;
bool operator!=(const Point& other) const;
bool operator<(const Point& other) const;
bool operator>(const Point& other) const;
bool operator<=(const Point& other) const;
bool operator>=(const Point& other) const;
Point& operator++();
Point operator++(int);
void print() const;
};
#endif
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(int px, int py) : x(px), y(py) {
}
Point Point::operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
Point Point::operator-(const Point& other) const {
return Point(x - other.x, y - other.y);
}
Point Point::operator*(int times) const {
return Point(x * times, y * times);
}
Point& Point::operator+=(const Point& other) {
x += other.x;
y += other.y;
return *this;
}
Point& Point::operator-=(const Point& other) {
x -= other.x;
y -= other.y;
return *this;
}
bool Point::operator==(const Point& other) const {
return x == other.x && y == other.y;
}
bool Point::operator!=(const Point& other) const {
return !(*this == other);
}
bool Point::operator<(const Point& other) const {
return x < other.x || (x == other.x && y < other.y);
}
bool Point::operator>(const Point& other) const {
return other < *this;
}
bool Point::operator<=(const Point& other) const {
return !(other < *this);
}
bool Point::operator>=(const Point& other) const {
return !(*this < other);
}
Point& Point::operator++() {
x++;
y++;
return *this;
}
Point Point::operator++(int) {
Point old = *this;
++(*this);
return old;
}
void Point::print() const {
cout << "(" << x << ", " << y << ")" << endl;
}
#include <iostream>
#include "Point.h"
using namespace std;
int main() {
Point a(1, 1);
Point b(3, 4);
Point c(1, 1);
Point d(1, 5);
cout << boolalpha;
cout << (a == c) << endl;
cout << (a != c) << endl;
cout << (a < b) << endl;
cout << (a < d) << endl;
cout << (b <= a) << endl;
cout << (b > a) << endl;
cout << (a >= c) << endl;
Point p(5, 5);
Point q = p++;
q.print();
p.print();
Point r = ++p;
r.print();
p.print();
++(++p);
p.print();
return 0;
}
Click each line of output to see what caused it.