Course Materials

OOP - Week 8, Lecture 1: Arithmetic Operators

A Character on a Grid

A game character stands at the point (2, 3) of a grid. A move is also a point: (1, 4) means one step to the right and four steps up. Where does the character stand after the move? At (3, 7): the x values are added, and the y values are added.

With what we know so far, we write a class Point with a member function:

class Point {
private:
    int x;
    int y;
public:
    Point(int px = 0, int py = 0);
    Point add(const Point& other) const;
};

Point position(2, 3);            // two objects of the class Point
Point move(1, 4);

Point next = position.add(move);

This works, but it does not read like the sum of two numbers. For int and double, we write a + b. C++ lets us do the same for our own classes: we give the operator + a meaning for Point. This is called operator overloading.

1. The Operator + as a Member Function

An operator is a function with a special name: operator followed by the symbol. We write it as a member function:

class Point {
public:
    Point operator+(const Point& other) const;
};

Here a and b are two objects of the class Point:

Point a(2, 3);
Point b(1, 4);

Then the expression a + b is the same as the call a.operator+(b). The object on the left, a, is the one the function is called on, and the object on the right, b, is the parameter:

Point sum = a + b;               // the short way
Point same = a.operator+(b);     // the same call, written as a function

The function creates and returns a new Point. The objects a and b are not changed, which is why the function is const and the parameter is a const reference, as in Week 4:

Point Point::operator+(const Point& other) const {
    return Point(x + other.x, y + other.y);
}

Because the result of a + b is a Point, it can be used again: a + b + c means (a + b) + c.

You can only give a new meaning to an operator for your own class: int + int cannot be changed. You cannot create new operators such as **, and the precedence of the operators stays the same: * still comes before +. Keep the usual meaning: an operator + that subtracts is a trap for the next person who reads the code.

2. The Operators - and *

- follows the same pattern as +:

Point operator-(const Point& other) const;

The right side does not have to be the same type. To multiply a point by a number, the parameter is an int:

Point operator*(int times) const;

Point triple = b * 3;            // b.operator*(3)

The order of the operands matters. The left side is always the object, so b * 3 works, but 3 * b does not compile, because there is no operator* for an int on the left: invalid operands to binary expression ('int' and 'Point'). Lecture 3 shows how to write it.

3. The Operators += and -=

+= is different from +: it changes the object it is called on, and does not create a new one. It returns the object itself, as a reference:

Point& Point::operator+=(const Point& other) {
    x += other.x;
    y += other.y;
    return *this;
}

This is the return *this of Week 5. The function is not const, because it changes the object, and the return type is Point&, so the result is a itself and not a copy. That is why (a += b) += b works: the second += changes the same a. With a return type of Point without the &, the second += would change a copy, and a would keep only the first change.

ย a + ba += b
Changes anoyes
Returnsa new Pointa itself, as a reference
Function is constyesno

-= follows the same pattern. The operators /, *= and /= also follow these patterns.

4. Putting It Together

The class Point is in three files, as in Week 3. It has the constructor, the five operators, and a print function that prints the point, because the operator << comes in Lecture 3. main uses each operator.

Point with arithmetic operatorscode/oop/week-08/point-1
has a note
#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;
}
Terminal
g++ main.cpp Point.cpp -o app
./app
Tip: click a line of output to jump to the code that printed it.Output hidden. Try it yourself first.

Click each line of output to see what caused it.