Operator Overloading (+, <<, ==)
C++ allows you to teach standard operators like `+`, `==`, and `<<` how to operate on your custom classes.
What is it?
Operator overloading is a compile-time feature that provides custom implementations for C++ operators applied to user-defined types.
Why is it used?
Enables natural mathematical and stream syntax, e.g. `std::cout << myStudent` or `Vector3 a + Vector3 b`.
Detailed Explanation
Rules of Operator Overloading:
• Cannot invent new operators (must overload existing operators like `+`, `-`, `==`, `[]`, `<<`).
• Cannot alter operator precedence or associativity.
• Stream insertion `<<` must be overloaded as a non-member friend function because the left-hand operand is `std::ostream`, not your class.
Syntax Structure
Syntax Blueprint
bool operator==(const ClassName &other) const;
friend std::ostream& operator<<(std::ostream &os, const ClassName &obj);
Code Example
CPP Code
#include <iostream>
#include <string>
class CourseFee {
private:
int amount;
std::string currency;
public:
CourseFee(int amt = 0, std::string curr = "INR")
: amount(amt), currency(curr) {}
// Overload + operator: Combines two fees
CourseFee operator+(const CourseFee &other) const {
return CourseFee(this->amount + other.amount, this->currency);
}
// Overload == operator: Tests equality
bool operator==(const CourseFee &other) const {
return this->amount == other.amount && this->currency == other.currency;
}
// Overload << operator for std::cout streaming
friend std::ostream& operator<<(std::ostream &os, const CourseFee &fee) {
os << "₹" << fee.amount << " (" << fee.currency << ")";
return os;
}
};
int main() {
CourseFee python_fee(2500);
CourseFee data_analytics_fee(2500);
// Using overloaded + operator
CourseFee total = python_fee + data_analytics_fee;
// Using overloaded << operator
std::cout << "Python Fee: " << python_fee << "\n";
std::cout << "Total Combo: " << total << "\n";
// Using overloaded == operator
if (python_fee == data_analytics_fee) {
std::cout << "Both individual courses have equal pricing.\n";
}
return 0;
}
Explanation of the Code
• `operator+` returns a new `CourseFee` summing both amounts.
• `friend std::ostream& operator<<` enables printing objects directly via `std::cout << fee`.
• `operator==` provides clean equality comparison.
Expected Output
Python Fee: ₹2500 (INR)
Total Combo: ₹5000 (INR)
Both individual courses have equal pricing.
Real-World Application
Used across 3D math libraries (matrix and vector addition) and BigInteger classes.
Common Mistakes to Avoid
• Not returning `std::ostream&` from `operator<<` (breaks stream chaining `std::cout << a << b`).
• Overloading operators counter-intuitively (e.g. overloading `+` to perform subtraction!).
Quick Practice Challenge
Overload the `!=` operator for the `CourseFee` class.
Key Takeaways
• Operator overloading provides natural syntax for custom objects.
• Stream `<<` and `>>` operators must be implemented as friend functions.
• Reuse `operator==` when implementing `operator!=`.