Curriculum / C++ / Lesson 04

Operators in C++

C++ has the same core operator families as C — arithmetic, relational, logical, and assignment — plus a powerful extra feature: operator overloading, which lets you redefine what operators like + do for your own classes..

4 of 10
beginner 📖 5 min read 🎯 1 question

Operators

C++ has the same core operator families as C — arithmetic, relational, logical, and assignment — plus a powerful extra feature: operator overloading, which lets you redefine what operators like + do for your own classes.

💡 Key insight Operator overloading is unique to C++ among the four languages here — it's why you can write custom_object1 + custom_object2.

Syntax

syntax
a + b   a - b   a * b   a / b   a % b
a == b  a != b  a > b   a < b
a && b  a || b  !a

Example

Comparison operators like a > b evaluate to a bool in C++.

cpp
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    cout << "Sum: " << (a + b) << endl;
    cout << "Remainder: " << (a % b) << endl;
    cout << "Greater: " << (a > b) << endl;
    return 0;
}
outputSum: 13 Remainder: 1 Greater: 1

Try it yourself

Copy this example into the C++ compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What C++ feature unique to this course's languages lets you redefine what + does for a custom class?

Back to curriculum