WHAT IS OPERATOR OVERLOADING(EXPLAINATION)
Absolutely! Let’s make it very beginner-friendly — imagine I’m your teacher and you’re just
learning this for the first time. 😊
🧑🏫 What is Operator Overloading? (Beginner-Friendly Explanation)
Think of operators like +, -, *, == as tools — tools that help us do things like add, compare, or subtract
numbers.
Normally, these tools work only with basic data types like int, float, etc.
But what if you create your own data type (like a class), and want to use + or == with it?
That's where Operator Overloading comes in.
🪀 Real-Life Example: Toy Boxes
Imagine:
You have two toy boxes.
Each toy box has a number of toys.
Now, let’s say you want to add two toy boxes together to find the total number of toys.
C++ doesn’t know how to add toy boxes by default.
But you can teach C++ how to add two toy boxes by using operator overloading.
✅ Example in C++: Add Two ToyBox Objects
#include <iostream>
using namespace std;
// Create a ToyBox class
class ToyBox {
int toys; // Number of toys in the box
public:
// Constructor to set toy count
ToyBox(int t) {
toys = t;
}
// Overload the '+' operator to add two ToyBoxes
ToyBox operator+(ToyBox b) {
return ToyBox(toys + [Link]);
}
// Display total toys
void display() {
cout << "Total toys: " << toys << endl;
}
};
int main() {
ToyBox box1(5); // First toy box with 5 toys
ToyBox box2(7); // Second toy box with 7 toys
ToyBox total = box1 + box2; // Overloaded '+' is used here
[Link](); // Output: Total toys: 12
return 0;
}
🧠 What's Happening?
box1 + box2 ➜ this calls the overloaded + operator.
It adds the toys from both boxes.
A new box is returned with the total.
📝 In Simple Words (for your exam):
Operator Overloading is a feature in C++ that lets us use operators like +, -, ==, etc. with objects of
a class, just like we use them with basic data types.
🧮 Common Operators You Can Overload:
Operato Meaning
r
+ Addition
- Subtraction
== Comparison
++ Increment (prefix/postfix)
<< Output (cout)
>> Input (cin)
🎯 Example: Overloading == Operator
class Student {
int roll;
public:
Student(int r) {
roll = r;
}
bool operator==(Student s2) {
return roll == [Link];
}
};
int main() {
Student s1(101), s2(101);
if (s1 == s2)
cout << "Same student" << endl;
else
cout << "Different student" << endl;
return 0;
}
Output: Same student
🌀 Types of Operator Overloading
1. Unary Operator Overloading → Works on 1 operand (++a, -a)
2. Binary Operator Overloading → Works on 2 operands (a + b, a - b)
Unary Operator Overloading Example:
class Number {
int x;
public:
Number(int a) {
x = a;
void operator++() {
++x;
void display() {
cout << "Value: " << x << endl;
}
};
int main() {
Number n1(5);
++n1; // Calls operator++()
[Link](); // Value: 6
return 0;
🔁 Operator Overloading using Friend Function
Sometimes we need to access private members. So we use friend functions.
class Complex {
int real, imag;
public:
Complex(int r, int i) {
real = r;
imag = i;
friend Complex operator+(Complex c1, Complex c2); //REPLACE THE OVERLOADING MEMBER
FUNCTION WITH FRIEND FUNCTION
};
Complex operator+(Complex c1, Complex c2) { // REDEFINE THE OVERLOADED FRIEND FUNCTION
return Complex([Link] + [Link], [Link] + [Link]);
FOR WRITING AN OPERATOR OVERLOADING FUNCTION WITH FRIEND FUNCTION
1)REPLACE THE OVERLOADING MEMBER FUNCTION WITH FRIEND FUNCTION
2)REDEFINE THE OVERLOADED FRIEND FUNCTION
📌 Difference: Member Function vs Friend Function
Feature Member Function Friend Function
Left Operand Must be object of the class Can be any type
Right Operand Passed as argument Both operands passed
obj1 + obj2 → [Link]+
Syntax obj1 + obj2 → operator+(obj1, obj2)
(obj2)
Access to private
Yes Yes (due to friend)
members
Either operand can be an object or a
Useful when Left operand is an object
different type
🔁 Example Use Case:
Use member function when both operands are objects of the same class and the left
operand must be an object.
Use friend function when:
o You want to allow non-member types on the left.
o You want to support commutative operations like 5 + obj (where 5 is not an
object).
📝 Summary for Exam:
Operator overloading can be done using either member functions or friend functions. Member
functions are called by the left object, while friend functions are external and can access private
data using the friend keyword.
✅ Rules for Operator Overloading in C++
To overload operators properly, C++ gives us certain rules we must follow. Here's a simple list for
you:
🧑🏫 Basic Rules for Operator Overloading
Rule
Rule Description
No.
1️⃣ Only existing operators can be overloaded. You cannot create new symbols like ** or %%.
Rule
Rule Description
No.
2️⃣ At least one operand must be a user-defined type (i.e., class or struct).
3️⃣ You cannot change operator precedence or number of operands.
You cannot change the default meaning of an operator for built-in types (e.g., int + int will
4️⃣
always mean addition).
5️⃣ Some operators cannot be overloaded at all (see list below).
6️⃣ Overloaded operators can be defined as member functions or friend functions.
❌ Operators That CANNOT Be Overloaded at All
Operator Meaning
:: Scope resolution operator
. Member access (dot operator)
.* Pointer-to-member operator
sizeof Size of object or data type
Used with RTTI (Run-Time Type
typeid
Information)
?: Ternary conditional operator
alignof Alignment requirement of a type
co_await Coroutine operator (C++20)
new and delete can be overloaded as special functions, not with
operator overloading syntax.
📊 Table: Operators That Can’t Be Overloaded by Member or Friend Functions
❌ Cannot be Overloaded by Friend
❌ Cannot be Overloaded by Member Function
Function
None (All overloadable operators can be overloaded by =, (), [], -> (These must be member
member functions) functions)
✅ Explanation:
The operators =, (), [], and -> must only be overloaded using member functions.
All other overloadable operators can be written using either member or friend functions.
📝 Summary (Perfect for Exams):
Rules of Operator Overloading:
1. Only existing operators can be overloaded.
2. At least one operand must be a user-defined class.
3. Some operators like ::, ., ?:, and sizeof cannot be overloaded.
4. Operators like =, (), [], and -> must be overloaded using member functions only.
5. All other overloadable operators can use either member or friend functions.
Q/A
✅ Important Questions from "Operator Overloading" Chapter (with Answer Key)
📘 1. Definitions and Terminologies (Write short notes / Define)
No. Question Answer Key (Short)
Giving new meaning to an existing operator to work
1. Define operator overloading.
with class objects.
Operator that works with only one operand, e.g., ++,
2. What is a unary operator?
--, -.
3. What is a binary operator? Operator that works with two operands, e.g., +, -, *.
What is the role of friend keyword in It allows a non-member function to access private
4.
operator overloading? members for overloading.
Define function call operator () Allows objects to be used as if they were functions.
5.
overloading. Must be a member function.
🧠 2. Differences (Very Important for Exams)
No. Question Answer Key (Short)
Difference between member function Member function uses calling object as left operand,
1. and friend function in operator friend takes both operands. Friend function must be
overloading. declared with friend.
Difference between unary and binary Unary: one operand, Binary: two operands. Syntax
2.
operator overloading. and logic differ.
3. Difference between overloading and Overloading: same name, different parameters.
No. Question Answer Key (Short)
overriding. Overriding: redefines base class method.
Difference between compile-time
Compile-time: overloading, Run-time: virtual
4. polymorphism and run-time
functions.
polymorphism.
🧪 3. Analytical / Conceptual Questions
No. Question Answer Key (Short)
Because some operators like ::, ., ?:, etc. are
Why can't all operators be overloaded in
1. fundamental to the language and cannot be
C++?
customized.
Explain the need for operator overloading E.g., adding two ToyBox objects using + makes
2.
with a real-life example. code clean and readable.
Which operators can only be overloaded
3. =, (), [], ->
using member functions?
Can we change the number of operands in No, operator behavior (like number of operands
4.
operator overloading? Why or why not? and precedence) cannot be changed.
💻 4. Coding Questions (Write with output)
No. Question
1. Write a program to overload the + operator using member function for a class Complex.
2. Overload the == operator to compare two objects of a class Student with same marks.
3. Write a program to overload the unary operator - using member function.
4. Use friend function to overload * operator for two Matrix objects.
5. Overload assignment operator = for a class Book.
🔁 5. Dry-Run Questions (Trace the output)
Q1. What will be the output of the following code?
class Box {
int length;
public:
Box(int l) { length = l; }
Box operator+(Box b) {
return Box(length + [Link]);
void show() { cout << length << endl; }
};
int main() {
Box b1(4), b2(6);
Box b3 = b1 + b2;
[Link]();
return 0;
Answer:
Output: 10
Q2. What happens if both operands in an operator overload are not objects?
class Distance {
int meters;
public:
Distance(int m) { meters = m; }
Distance operator+(int m) {
return Distance(meters + m);
};
int main() {
Distance d1(10);
Distance d2 = d1 + 5; // Valid
Distance d3 = 5 + d1; // Error
return 0;
}
Answer:
d1 + 5 is valid (member function)
5 + d1 gives error (left is not an object — needs friend function)
📋 6. Write Short Notes On:
No. Topic
1. Rules of operator overloading
2. Limitations of operator overloading
3. Operator overloading with friend functions
4. Overloading relational operators (==, <, >)
5. Advantages and disadvantages of operator overloading