OOP - Week 8, Lecture 2: Comparison, Prefix and Postfix Operators
Which Point Comes First?
Two points on a grid: are they the same point? If not, which one comes first? And a character that takes one step in the diagonal direction: we would like to write p++, as for a number. With what we know so far, we write functions with names:
class Point {
public:
bool equals(const Point& other) const;
bool isBefore(const Point& other) const;
void step();
};
if (a.equals(b)) ...
As in Lecture 1, we can give the usual symbols a meaning for Point. This lecture covers the comparison operators and the increment operator ++, which has two forms.
1. Comparison Operators
A comparison operator answers a question with true or false, so its return type is bool. It only looks at the two objects, so it is a const function, with a const reference parameter:
bool operator==(const Point& other) const;
Two points are equal when both coordinates are equal:
bool Point::operator==(const Point& other) const {
return x == other.x && y == other.y;
}
!= is the opposite of ==, so we write it with ==, and the logic exists once:
bool Point::operator!=(const Point& other) const {
return !(*this == other);
}
For < we must decide what βsmallerβ means for points. We use the rule of a dictionary: the point with the smaller x comes first, and when the x values are equal, the smaller y comes first. So (1, 1) is smaller than (3, 4), and (1, 1) is smaller than (1, 5):
bool Point::operator<(const Point& other) const {
return x < other.x || (x == other.x && y < other.y);
}
Two functions are enough. == and < hold the whole rule. The other four comparison operators are written with them, one line each, and none of them looks at x and y again:
| The expression | Is written as |
|---|---|
a == b | the rule: both coordinates are equal |
a < b | the rule: a smaller x, then a smaller y |
a != b | !(a == b) |
a > b | b < a |
a <= b | !(b < a) |
a >= b | !(a < b) |
The code of the three that use <:
bool Point::operator>(const Point& other) const {
return other < *this; // a > b is b < a
}
bool Point::operator<=(const Point& other) const {
return !(other < *this); // not bigger
}
bool Point::operator>=(const Point& other) const {
return !(*this < other); // not smaller
}
If the rule changes, only == and < change, and the other four follow.
A comparison gives a bool, and cout prints it as 1 or 0. Write cout << boolalpha; once, and it prints true and false.
2. The Prefix ++
The operator ++ has two forms, and they are two different functions. In the prefix form, ++p, the point is increased first, and the value of the expression is the new point. Our ++ moves a point by one step in both directions: x and y both increase by 1.
Point& Point::operator++() {
x++;
y++;
return *this;
}
The function changes the object, so it is not const, and it returns the object itself as a reference, like += in Lecture 1. This means ++(++p) works: the inner ++p returns p itself, so the outer one increases the same p again.
3. The Postfix ++
In the postfix form, p++, the value of the expression is the old point, and the point is increased after that. To tell the compiler which form we are writing, the postfix function has an extra parameter of type int:
Point operator++(int);
The int has no name and is never used. It is only a mark: p++ calls the function with the int, and ++p calls the one without it. We write the postfix with the prefix:
Point Point::operator++(int) {
Point old = *this; // a copy of the old value
++(*this); // increase the point
return old; // give back the old value
}
The result is a copy, a Point, and not a reference: the old value is a local object that is destroyed at the closing brace.
| Β | Prefix ++p | Postfix p++ |
|---|---|---|
| The value of the expression | the new point | the old point |
| The function | operator++() | operator++(int) |
| Returns | Point&, the object itself | Point, a copy |
| Cost | none | one copy |
If you write only the prefix function, p++ does not compile: cannot increment value of type 'Point'. The operator -- follows the same two patterns.
Which one should you write? On a line of its own,
p++;and++p;do the same top. Butp++first makes a copy of the old value to give back, and then nobody uses that copy.++pmakes no copy. So write++p;when you do not need the old value. For anintthe compiler removes the copy, but for a class with a lot of data the copy costs time.
4. Putting It Together
This is the Point of Lecture 1, with the comparison operators and the two forms of ++. Click Changes since Lecture 1 to see exactly which lines are new. main compares four points and shows the difference between p++ and ++p.
#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;
}#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);
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;
}
void Point::print() const {
cout << "(" << x << ", " << y << ")" << endl;
}
#include "Point.h"
int main() {
Point a(2, 3);
Point b(1, 4);
Point sum = a + b;
sum.print();
Point diff = sum - a;
diff.print();
Point triple = b * 3;
triple.print();
a += b;
a.print();
a -= Point(1, 1);
a.print();
(a += b) += b;
a.print();
Point back = b - sum;
back.print();
return 0;
}
Click each line of output to see what caused it.