C++ Academy · Lesson

Arithmetic and Comparison Operators

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

Lesson 1 of 413 steps

Arithmetic and Comparison Operators is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Arithmetic and Comparison Operators” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Arithmetic and Comparison Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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