Definition
📌 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..
Syntax
syntax
a + b a - b a * b a / b a % b
a == b a != b a > b a < b
a && b a || b !aExample
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;
}Output
outputSum: 13
Remainder: 1
Greater: 1
Important Points
- Operator behavior for built-in types matches C exactly.
- C++ lets classes overload operators (e.g. define what
+means for aVectorclass). &&and||short-circuit, same as in C.- The ternary operator
condition ? a : bis a compact if/else for expressions.
Common Mistakes
- Assuming
/gives a decimal result when both operands areint— it truncates. - Overloading operators in ways that surprise the reader (e.g. making
+do something unrelated to addition). - Confusing
=and==inside conditions.
Interview Tip
🎯 Before your interview
"What is operator overloading?" is a very common C++ interview question — be ready with a one-line definition and a small example.
