0Pricing
C++ Academy · Lesson

Arithmetic and Comparison Operators

Overload +, -, *, /, ==, < and friends following best practices.

Arithmetic and Comparison Operators is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Overload Operators?

Operator overloading lets your types behave like built-in types — v1 + v2 reads better than v1.add(v2). Use it for value types like vectors, complex numbers, money.

Defining operator+

You can overload + as a member or a free function. Free functions are more symmetric.

struct Vec2 {
    double x, y;
    Vec2 operator+(const Vec2& other) const {
        return {x + other.x, y + other.y};
    }
};

Free Function Form

A free function gives both operands the same status — useful when conversion may apply to either side.

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

Compound Assignment Operators

Implement += first; then build + on top of it. This is the canonical idiom.

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

Vec2 operator+(Vec2 lhs, const Vec2& rhs) {
    lhs += rhs;
    return lhs;
}

Unary Operators

Some operators are unary: -x, +x, !x, ~x, ++x, --x.

Vec2 operator-() const { return {-x, -y}; }

Prefix vs Postfix Increment

Prefix takes no parameter; postfix takes a dummy int to disambiguate.

Counter& operator++();        // prefix
Counter  operator++(int);     // postfix

Comparison Operators

Comparison operators return bool. Free functions work best for consistency.

bool operator==(const Vec2& a, const Vec2& b) {
    return a.x == b.x && a.y == b.y;
}
bool operator!=(const Vec2& a, const Vec2& b) { return !(a == b); }

Less Than for Ordering

If your type needs to be sorted or used as a map key, define operator<. Other relational operators follow from it.

Symmetric Mixed-Type Operators

To support 3 + vec as well as vec + 3, define both — or use a free function that converts.

Avoid Surprises

Overloaded operators should behave intuitively. Do not redefine + to mean subtraction. Stick to the conventional algebra of the operator.

Operators You Cannot Overload

Some operators are off-limits:

  • ?: — ternary
  • . — member access (but -> is overloadable)
  • :: — scope resolution
  • sizeof, alignof, typeid

Quick Check

What is the canonical implementation pattern for operator+ on a user type?

Recap

Operator overloading makes user types feel native. Implement compound assignment first, then plain arithmetic. Comparison operators are best as free functions. Stick to conventional semantics — surprise is a bug.

Frequently asked questions

Is the “Arithmetic and Comparison Operators” lesson free?

Yes — the full text of “Arithmetic and Comparison Operators” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arithmetic and Comparison Operators”?

Overload +, -, *, /, ==, < and friends following best practices. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arithmetic and Comparison Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Comparison Operators
  2. Stream Insertion and Extraction Operators
  3. Subscript and Function Call Operators
  4. Three-Way Comparison Operator C++20
← Back to C++ Academy