C/C++ Arena

Step 1 of 5

operator+

You can define what operators mean for your types. An operator is just a function with a special name:

struct Vec2 { double x, y; };

Vec2 operator+(Vec2 a, Vec2 b) {
    return {a.x + b.x, a.y + b.y};
}

Vec2 c = Vec2{1, 2} + Vec2{3, 4};   // {4, 6}

Your turn: complete operator- and operator* (vector times a number).

Next: operator== and !=