What is Operator Overloading in C++?
Learn operator overloading in C++: member vs non-member functions, overloading + == and <<, what can't be overloaded, and clean code examples.
Expected Interview Answer
Operator overloading in C++ lets you redefine the behavior of built-in operators such as +, ==, or << for your own user-defined types, so objects of a class can be used with natural, expressive syntax.
You implement an operator by defining a function named operator followed by the symbol, either as a member function or a free (non-member) function, often a friend when it needs private access. Binary operators like + are commonly non-members for symmetry, compound assignments like += are usually members, and stream operators << and >> must be non-members taking the stream by reference. Overloading only changes behavior for user types; it cannot invent new operators or change operator precedence and arity.
- Makes user-defined types read like built-in types
- Improves readability of mathematical and container code
- Enables natural syntax with streams, comparisons, and arithmetic
- Supports value semantics for custom classes
- Integrates custom types with standard library algorithms
AI Mentor Explanation
A universal scoreboard '+' button normally adds runs, but you reprogram it so pressing it on a partnership object correctly merges two batters' individual tallies into a combined stand. The familiar symbol now knows how to add your custom thing, just as an overloaded operator teaches + to work on a class.
Step-by-Step Explanation
Step 1
Pick the operator and form
Decide whether to implement it as a member function or a non-member (often friend) function based on symmetry and access needs.
Step 2
Name the function
Define a function called operator followed by the symbol, e.g. operator+ or operator==.
Step 3
Define the semantics
Write the body so it produces a correct, intuitive result matching the built-in operator's meaning.
Step 4
Return the right type
Return a new value for arithmetic, a reference for stream and compound-assignment operators.
Step 5
Keep it consistent
Ensure related operators agree, such as == with != and < defining a total order.
What Interviewer Expects
- Correct syntax using the operator keyword
- Knowing when to use member vs non-member (friend) functions
- Why << and >> must be non-members taking the stream by reference
- Understanding what cannot be overloaded
- Returning appropriate types and preserving intuitive semantics
Common Mistakes
- Trying to invent new operators or change precedence and arity
- Implementing << as a member function instead of a non-member
- Returning by value where a reference is expected (or vice versa)
- Overloading operators with surprising, non-intuitive behavior
- Forgetting to keep == and != or < consistent with each other
Best Answer (HR Friendly)
“Operator overloading lets you teach familiar symbols like + or == to work on your own custom objects, so your code reads naturally instead of calling clunky named functions. For example, adding two money values with a simple + instead of an addMoney function.”
Code Example
#include <iostream>
class Complex {
double re, im;
public:
Complex(double r = 0, double i = 0) : re(r), im(i) {}
// Non-member-friendly via member here: returns a new value
Complex operator+(const Complex& rhs) const {
return Complex(re + rhs.re, im + rhs.im);
}
bool operator==(const Complex& rhs) const {
return re == rhs.re && im == rhs.im;
}
// Stream operator must be a non-member; friend grants access
friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
os << c.re << " + " << c.im << "i";
return os;
}
};
int main() {
Complex a(1, 2), b(3, 4);
Complex sum = a + b; // uses operator+
std::cout << sum << '\n'; // uses operator<<
std::cout << (a == b) << '\n';
}Follow-up Questions
- When should an operator be a member versus a non-member function?
- Why must operator<< and operator>> be non-member functions?
- Which operators cannot be overloaded in C++?
- How do you overload the assignment operator correctly?
- What is the rule of three/five and how does it relate to operators?
MCQ Practice
1. Which keyword is used to define an overloaded operator?
You name the function operator followed by the symbol, e.g. operator+.
2. Why is operator<< usually implemented as a non-member function?
The left operand of << is the ostream, so it cannot be a member of your class; a non-member (often friend) is required.
3. Which of these operators cannot be overloaded in C++?
The scope resolution operator :: (along with ., .*, ?: and sizeof) cannot be overloaded.
Flash Cards
What is operator overloading? — Redefining built-in operators like + or == to work on user-defined types using functions named operator<symbol>.
Why must operator<< be a non-member? — Its left operand is the stream, not your class, so it cannot be a member function; a friend gives it private access.
Name operators that cannot be overloaded. — ::, ., .*, ?: and sizeof cannot be overloaded.
What can operator overloading NOT change? — It cannot invent new operators, change precedence, or change an operator's arity.