CodeBlanks Logo Icon CodeBlanks

Summary - Operator overloading

Combine arithmetic, comparison and stream operators to write a Vector class.

Summary - Operator overloading

In this group you learned operator overloading. You define a function with the word operator and a symbol; your class then works like built-in types.

You saw arithmetic (+, -), comparison (==, <), stream (<<) and spaceship (<=>) operators. Now you will consolidate what you learned with two tasks: first the addition operator, then equality comparison.

Practice Add operator+ to Vector2

Add operator+ to the Vector2 class in the starting code: its body is return Vector2(x + other.x, y + other.y);. In main compute a + b and print the result.

Starter code

main.cpp
#include <iostream>

class Vector2 {
public:
    int x;
    int y;
    Vector2(int x_, int y_) : x(x_), y(y_) {}
    // Add the addition operator
};

int main() {
    Vector2 a(2, 3);
    Vector2 b(4, 5);
    // Compute a + b and print
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Vector2 {
public:
    int x;
    int y;
    Vector2(int x_, int y_) : x(x_), y(y_) {}
    Vector2 operator+(const Vector2& other) const {
        return Vector2(x + other.x, y + other.y);
    }
};

int main() {
    Vector2 a(2, 3);
    Vector2 b(4, 5);
    Vector2 c = a + b;
    std::cout << c.x << " " << c.y;
    return 0;
}

Expected output

6 8

Practice Add operator== to Score

Add operator== to the Score class in the starting code: its body is return value == other.value;. In main compare a == b; print "Equal" if equal, "Different" otherwise.

Starter code

main.cpp
#include <iostream>

class Score {
public:
    int value;
    Score(int v) : value(v) {}
    // Add the equality operator
};

int main() {
    Score a(7);
    Score b(7);
    // Compare a == b and print the result
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Score {
public:
    int value;
    Score(int v) : value(v) {}
    bool operator==(const Score& other) const {
        return value == other.value;
    }
};

int main() {
    Score a(7);
    Score b(7);
    if (a == b) {
        std::cout << "Equal";
    } else {
        std::cout << "Different";
    }
    return 0;
}

Expected output

Equal

End of section

You completed operator overloading. You can now define operator+, operator-, operator==, operator<< and operator<=>.

You made your classes work as naturally as built-in types. The next group is the intermediate final project; there you will combine OOP, templates, STL and file I/O. Operator overloading will also come in handy in that project.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What operator does operator+ define?

main.cpp
Vector2 operator+(const Vector2& other) const;
  • Subtraction
  • Addition Correct answer
  • Comparison
  • Output

Explanation: operator+ is the addition operator.

2 Complete the operator function name

Question
Vector2 ____(const Vector2& other) const { return Vector2(x + other.x, y + other.y); }
Answer
Vector2 operator+(const Vector2& other) const { return Vector2(x + other.x, y + other.y); }

Explanation: operator+ is the addition operator.

All lessons in Intermediate