C++ — Operators
Arithmetic operators
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Add: " << a + b << endl; // 13
cout << "Sub: " << a - b << endl; // 7
cout << "Mul: " << a * b << endl; // 30
cout << "Div: " << a / b << endl; // 3 (integer)
cout << "Mod: " << a % b << endl; // 1
// Floating-point division
cout << "10.0 / 3 = " << 10.0 / 3 << endl; // 3.33333
return 0;
}
Increment and decrement
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 5;
cout << "a++ = " << a++ << ", a = " << a << endl; // 5, 6
cout << "++b = " << ++b << endl; // 6
return 0;
}
Relational operators
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
cout << "Equal: " << (a == b) << endl; // 0
cout << "Not equal: " << (a != b) << endl; // 1
cout << "Greater: " << (a > b) << endl; // 0
cout << "Less: " << (a < b) << endl; // 1
return 0;
}
Logical operators
#include <iostream>
using namespace std;
int main() {
bool a = true, b = false;
cout << "AND: " << (a && b) << endl; // 0
cout << "OR: " << (a || b) << endl; // 1
cout << "NOT: " << (!a) << endl; // 0
// Short-circuit evaluation
int x = 0;
if (x != 0 && 10 / x > 2) {
cout << "Never reached" << endl;
}
return 0;
}
Bitwise operators
#include <iostream>
using namespace std;
int main() {
unsigned int a = 0b1010; // 10
unsigned int b = 0b1100; // 12
cout << "AND: " << (a & b) << endl; // 8
cout << "OR: " << (a | b) << endl; // 14
cout << "XOR: " << (a ^ b) << endl; // 6
cout << "NOT: " << ~a << endl; // Large number
cout << "Left: " << (a << 1) << endl; // 20
cout << "Right: " << (a >> 1) << endl; // 5
return 0;
}
Assignment operators
#include <iostream>
using namespace std;
int main() {
int x = 10;
x += 5; // 15
x -= 3; // 12
x *= 2; // 24
x /= 4; // 6
x %= 4; // 2
x <<= 1; // 4
x >>= 1; // 2
cout << "Final: " << x << endl;
return 0;
}
Ternary and conditional
#include <iostream>
using namespace std;
int main() {
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
cout << status << endl;
// if expression (C++17)
int x = 42;
string label = [] {
return "value";
}();
return 0;
}
sizeof and alignof
#include <iostream>
using namespace std;
int main() {
cout << "char: " << sizeof(char) << endl;
cout << "int: " << sizeof(int) << endl;
cout << "double: " << sizeof(double) << endl;
struct Align {
char a;
int b;
char c;
};
cout << "Struct size: " << sizeof(Align) << endl;
cout << "Alignment: " << alignof(Align) << endl;
return 0;
}
Operator overloading
#include <iostream>
using namespace std;
class Vec2 {
public:
float x, y;
Vec2(float x, float y) : x(x), y(y) {}
Vec2 operator+(const Vec2 &other) const {
return Vec2(x + other.x, y + other.y);
}
bool operator==(const Vec2 &other) const {
return x == other.x && y == other.y;
}
friend ostream &operator<<(ostream &os, const Vec2 &v) {
return os << "(" << v.x << ", " << v.y << ")";
}
};
int main() {
Vec2 a(1, 2), b(3, 4);
Vec2 c = a + b;
cout << a << " + " << b << " = " << c << endl;
cout << "Equal: " << (a == b) << endl;
return 0;
}
Best practices
- Use parentheses when precedence is unclear
- Prefer
==and!=over implicit boolean conversions - Use
static_castinstead of C-style casts - Override operators only when semantically meaningful
- Use
constexprfor compile-time computations
Mini Practice
Write C++ code that:
- Demonstrates all arithmetic operators
- Shows short-circuit evaluation with
&& - Overloads
+and<<for a simple class - Uses
sizeofto check type sizes
Up Next
In the next lesson, you'll learn about Strings — working with std::string in C++.
Related Topics
Frequently Asked Questions about Operators
What is Operators in C++?
Operators is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Operators?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.
Why is Operators important in C++?
Operators is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.