A step-by-step guide where we don't just learn what the code does — we understand why every word, symbol, parameter, and line of code is used.
👋 Why I Created This Guide
Recently, a friend asked me to help her understand Operator Overloading in C++ because she was having difficulty understanding it from her training.
When I looked at the code, I realized something important.
The problem wasn't necessarily the Operator Overloading itself.
The problem was that many small C++ concepts were being used together.
For example, a beginner may see:
Time Time::operator+(const Time &t)
and immediately have questions:
- ❓ Why are there two
Times? - ❓ What does
Time::mean? - ❓ Why is the function called
operator+? - ❓ What does
constmean? - ❓ What does
&mean? - ❓ What is
t? - ❓ Where did the first object go?
- ❓ Why do we need
temp? - ❓ Why are we returning
Time? - ❓ What is
this? - ❓ Why does postfix
++haveint? - ❓ Why are
ostreamandistreamused? - ❓ Why are
<<and>>used?
If these small questions aren't answered, it becomes very easy to memorize the syntax without understanding the concept.
So in this article, I want to explain Operator Overloading the way I would explain it to a friend sitting beside me.
The goal is not:
❌ "Memorize this code."
The goal is:
✅ "Look at the code and understand why every part is there."
📚 What We Will Learn
By the end of this article, you should understand:
- Classes and objects
- Operators and operands
- Unary vs Binary operators
- Operator Overloading
- Member operator functions
operator+()operator-()operator=()- Prefix
++ - Postfix
++ - Prefix
-- - Postfix
-- - Why postfix operators use a dummy
int this*thisconst- References using
& - Return types such as
TimeandTime& ostreamistreamoperator<<operator>>- Time addition
- Time subtraction
- Borrowing
- Time normalization
- How C++ can be thought of as translating operator expressions into function calls
🧱 Part 1 — Before Operator Overloading
Before learning Operator Overloading, let's understand the object we are going to use.
We will create a class called Time.
1. What Is a Class?
Consider this:
class Time
{
int hours;
int minutes;
};
If you've already studied classes, this may look simple.
But let's slow down.
What does class mean?
class is a C++ keyword used to create a class.
A simple way to understand a class is to think of it as a blueprint.
Imagine an architect creates a blueprint for a house.
The blueprint describes:
- rooms
- doors
- windows
- structure
But the blueprint itself is not the actual house.
Similarly, our Time class is a blueprint.
It tells us:
"A
Timeobject will contain information related to hours and minutes."
Visualize it like this:
Time
┌───────────────┐
│ BLUEPRINT │
│ │
│ hours │
│ minutes │
└───────────────┘
2. What Are hours and minutes?
Inside the class we have:
int hours;
int minutes;
What is int?
int is a C++ data type used for storing whole numbers.
For example:
int age = 21;
Here:
-
int→ data type -
age→ variable name -
21→ value
Similarly:
int hours;
int minutes;
means our Time object will contain two integer variables:
hours
minutes
For example:
hours = 2
minutes = 40
represents:
2 hours 40 minutes
3. What Is an Object?
We have created our blueprint:
class Time
{
int hours;
int minutes;
};
But we still haven't created an actual Time object.
To create one:
Time t;
Let's read this from left to right:
Time t
↓ ↓
type name
Time is the class type we created.
t is the name of the object.
So:
Time t;
simply means:
Create an object named
twhose type isTime.
You can visualize it like this:
Time class
┌──────────────┐
│ blueprint │
│ │
│ hours │
│ minutes │
└──────┬───────┘
│
create object
↓
┌──────────────┐
│ t │
├──────────────┤
│ hours │
│ minutes │
└──────────────┘
4. Class vs Object
This distinction is extremely important.
🟦 Class
A class is the blueprint or definition.
class Time
{
int hours;
int minutes;
};
🟩 Object
An object is an actual object created from that class.
Time t;
We can create many objects from the same class:
Time t1;
Time t2;
Time t3;
Visual:
Time class
/ | \
↓ ↓ ↓
┌─────┐ ┌─────┐ ┌─────┐
│ t1 │ │ t2 │ │ t3 │
└─────┘ └─────┘ └─────┘
All three objects are created from the same class.
They have the same structure, but their values can be different.
For example:
t1 → 2 hours 40 minutes
t2 → 1 hour 30 minutes
t3 → 5 hours 10 minutes
5. Objects Can Also Use Functions
A class can contain variables as well as functions.
For example:
class Time
{
public:
int hours;
int minutes;
void display()
{
cout << hours << " hours "
<< minutes << " minutes";
}
};
Now:
Time t;
creates an object called t.
That object can call the function:
t.display();
This means:
Call the
display()function for objectt.
Keep this idea in mind.
Later we will see:
t1.operator+(t2);
The same basic idea is involved:
An object can call a member function that belongs to its class.
⚙️ Part 2 — Understanding Operators
Now let's move to operators.
You have already used operators many times.
For example:
int a = 10;
int b = 20;
int c = a + b;
Look at:
a + b
There are three things:
a + b
↑ ↑ ↑
operand operator operand
The + is the operator.
a and b are the operands.
6. What Is an Operand?
An operand is the value or object on which an operator works.
For:
10 + 20
the operator is:
+
and the operands are:
10
20
Visual:
+
/ \
/ \
10 20
↑ ↑
operand operand
7. What Does the + Operator Already Know?
C++ already knows how to use + with built-in data types.
For example:
10 + 20
produces:
30
C++ already knows rules for operations such as:
int + int
float + float
double + double
But now we have created our own type:
Time
And this creates a problem.
❓ Part 3 — The Problem We Need to Solve
Suppose we have:
Time t1;
Time t2;
and:
t1 = 2 hours 40 minutes
t2 = 1 hour 30 minutes
Now suppose we want to write:
Time t3 = t1 + t2;
What should + do?
C++ knows:
10 + 20
But C++ does not automatically know what this means:
t1 + t2
when t1 and t2 are Time objects.
We need to tell C++ what + should mean for our Time class.
And that is where:
⭐ Operator Overloading
comes in.
8. What Is Operator Overloading?
A simple definition is:
Operator Overloading means giving an existing C++ operator a special meaning when it is used with objects of our own class.
Notice an important point:
We are not creating a new operator.
The operator already exists.
For example:
+
-
=
++
--
<<
>>
We are simply teaching C++:
"When this operator is used with my
Timeobjects, perform this operation."
For example:
t1 + t2
can be made to mean:
Add hours
+
Add minutes
+
Convert extra minutes into hours
9. Why Is Operator Overloading Useful?
Without Operator Overloading, we could create a normal function:
Time addTime(Time t1, Time t2);
and then:
Time t3 = addTime(t1, t2);
That works.
But with Operator Overloading, we can write:
Time t3 = t1 + t2;
This looks much more natural.
We are making our custom objects behave more like the built-in types we already know.
🔢 Part 4 — Unary vs Binary Operators
Before writing the operator functions, we need to understand one simple question:
How many operands are involved?
Binary Operator
A binary operator works with two operands.
Example:
t1 + t2;
Visual:
+
/ \
t1 t2
There are two operands.
Therefore:
2 operands → Binary operator
Other examples:
t1 - t2;
t1 = t2;
Unary Operator
A unary operator works with one operand.
Example:
++t1;
Visual:
++
|
t1
There is only one operand.
Therefore:
1 operand → Unary operator
Examples:
++t1;
--t1;
t1++;
t1--;
🧠 Easy Rule
Remember this:
┌─────────────────────────┐
│ 2 operands → BINARY │
│ 1 operand → UNARY │
└─────────────────────────┘
That's enough to classify the operators we are using.
➕ Part 5 — operator+()
Now let's finally overload +.
We want this:
Time t3 = t1 + t2;
to work.
So we define:
Time Time::operator+(const Time &t)
{
Time temp;
temp.hours = hours + t.hours;
temp.minutes = minutes + t.minutes;
temp.hours = temp.hours + temp.minutes / 60;
temp.minutes = temp.minutes % 60;
return temp;
}
Don't try to memorize this.
We are going to take it apart piece by piece.
10. Understanding the Function Header
Look at:
Time Time::operator+(const Time &t)
Break it into parts:
Time Time:: operator+ (const Time &t)
↑ ↑ ↑ ↑
return belongs operator parameter
type to Time being used object
Now let's understand every part.
11. The First Time
Time Time::operator+
The first Time is the return type.
What is a return type?
A function can produce a result.
For example:
int add(int a, int b)
{
return a + b;
}
The result is an integer.
Therefore:
int
is the return type.
Our operator function returns:
Time
Why?
Because:
Time + Time
produces another Time.
For example:
2 hours 40 minutes
+ 1 hour 30 minutes
-------------------
4 hours 10 minutes
The result is still a Time.
Therefore:
Time
is the return type.
12. What Does Time:: Mean?
The symbol:
::
is called the scope resolution operator.
It tells C++ which class something belongs to.
So:
Time::operator+
means:
The
operator+function belonging to theTimeclass.
Think:
Time
↓
class
↓
operator+
↓
belongs to Time
13. What Does operator+ Mean?
This part:
operator+
means:
We are defining the behavior of the
+operator.
Similarly:
operator-
defines -.
operator=
defines =.
operator++
defines ++.
And so on.
14. Understanding (const Time &t)
Now look at:
(const Time &t)
This is the parameter list.
A parameter is information that a function receives.
For example:
int add(int a, int b)
has:
a
b
as parameters.
Our function receives:
const Time &t
Let's understand each piece.
15. What Does Time Mean Here?
Look at:
Time t
This means:
tis of typeTime.
Remember:
Time t1;
means:
Create a
Timeobject calledt1.
Similarly, inside our function:
Time t
means:
trefers to aTimeobject received by the function.
16. What Does & Mean?
Here:
Time &t
the & means reference.
A reference is another name or alias for an existing object.
Instead of creating a completely separate copy of the object, the parameter refers to an existing object.
For our expression:
t1 + t2;
we can think of the call as:
t1.operator+(t2);
Inside the function:
t1
↓
current object
t2
↓
parameter t
So t refers to the second object.
17. What Does const Mean?
Now we have:
const Time &t
const means:
We promise not to modify the object through this reference.
Why?
Because while calculating:
t1 + t2
we only need to read t2.
We don't want addition to change t2.
So:
const Time &t
can be understood as:
"Give me a reference to another
Timeobject. I only want to read it, not modify it."
⭐ 18. Where Did t1 Go?
This is one of the most important ideas in understanding member operator functions.
Suppose we write:
t1 + t2;
Because operator+ is a member function of Time, we can think of it as:
t1.operator+(t2);
Now everything becomes easier to understand.
t1.operator+(t2)
↑ ↑
| |
current parameter
object t
So:
t1 → current object
t2 → parameter t
Inside the function:
hours
means the hours belonging to the current object.
So we can mentally think:
hours
as:
t1.hours
And:
t.hours
means:
t2.hours
This mental model is extremely important.
🧮 19. Let's Actually Add Two Times
Suppose:
t1 = 2 hours 40 minutes
t2 = 1 hour 30 minutes
We want:
2 h 40 m
+ 1 h 30 m
-----------
4 h 10 m
First we add the hours.
temp.hours = hours + t.hours;
The current object's hours:
t1.hours = 2
The parameter object's hours:
t2.hours = 1
Therefore:
2 + 1 = 3
So:
temp.hours = 3
20. Adding the Minutes
Next:
temp.minutes = minutes + t.minutes;
We have:
t1.minutes = 40
t2.minutes = 30
Therefore:
40 + 30 = 70
Now:
temp = 3 hours 70 minutes
But this isn't the normal way we represent time.
We normally want minutes to be:
0 to 59
So we need to convert the extra minutes.
🔄 21. What Is Normalization?
We can call this process normalization.
Normalization simply means:
Convert a value into its proper or standard form.
For example:
3 hours 70 minutes
can be converted to:
4 hours 10 minutes
because:
70 minutes = 1 hour 10 minutes
22. Why Do We Use / 60?
The code says:
temp.hours = temp.hours + temp.minutes / 60;
Suppose:
temp.minutes = 70
Then:
70 / 60 = 1
Because these are integers, this is integer division.
We are interested in how many complete hours are contained inside 70 minutes.
The answer is:
1 complete hour
So:
3 hours + 1 hour
=
4 hours
23. Why Do We Use % 60?
Next:
temp.minutes = temp.minutes % 60;
The % operator gives the remainder after division.
For example:
70 % 60 = 10
So:
70 minutes
contains:
1 complete hour
+
10 minutes remaining
Therefore:
4 hours 10 minutes
is our final answer.
24. Why Do We Create temp?
Look at:
Time temp;
Why are we creating another object?
Because we need a separate object to store the result.
We don't want:
t1 = 2:40
t2 = 1:30
to be changed when we calculate:
t1 + t2
Instead:
t1 t2
│ │
2:40 1:30
\ /
\ /
\ /
operator+
|
↓
temp
|
4:10
So:
Time temp;
means:
Create a temporary
Timeobject that we can use to build the result.
25. What Does return temp Mean?
At the end:
return temp;
means:
Give the calculated
Timeobject back to the code that called the function.
Remember:
Time t3 = t1 + t2;
The flow is:
t1 + t2
↓
operator+()
↓
create temp
↓
calculate 4:10
↓
return temp
↓
t3 receives the result
So:
t3 = 4 hours 10 minutes
🔍 26. Complete operator+() Flow
The expression:
Time t3 = t1 + t2;
can be mentally understood as:
Time t3 = t1.operator+(t2);
Then:
t1
↓
current object
t2
↓
parameter t
operator+
↓
create temp
↓
add hours
↓
add minutes
↓
convert extra minutes
↓
return temp
↓
t3
If this flow is clear, you already understand the core idea of Operator Overloading.
➖ Part 6 — operator-()
Now let's understand subtraction.
Time Time::operator-(const Time &t)
{
Time temp;
temp.hours = hours;
temp.minutes = minutes;
if (temp.minutes < t.minutes)
{
temp.hours--;
temp.minutes = temp.minutes + 60;
}
temp.hours = temp.hours - t.hours;
temp.minutes = temp.minutes - t.minutes;
return temp;
}
Most of this is already familiar.
The main new idea is:
Borrowing
27. Why Is - Binary?
Look at:
t1 - t2;
There are two operands:
t1 - t2
↑ ↑
operand operand
Therefore:
-is a binary operator.
28. Why Do We Need Borrowing?
Suppose:
t1 = 3 hours 20 minutes
t2 = 1 hour 40 minutes
We want:
3 h 20 m
-1 h 40 m
But:
20 - 40
doesn't give us a valid positive minute value.
So we borrow one hour.
Remember:
1 hour = 60 minutes
Therefore:
3 hours 20 minutes
becomes:
2 hours 80 minutes
Now:
80 - 40 = 40 minutes
and:
2 - 1 = 1 hour
Final:
1 hour 40 minutes
29. The Borrowing Code
if (temp.minutes < t.minutes)
{
temp.hours--;
temp.minutes = temp.minutes + 60;
}
Let's read it like a human.
First:
if (temp.minutes < t.minutes)
means:
If the minutes we currently have are smaller than the minutes we want to subtract...
Then:
temp.hours--;
means:
Take one hour away.
For example:
3 hours
↓
2 hours
Then:
temp.minutes = temp.minutes + 60;
means:
Convert that borrowed hour into 60 minutes.
So:
20 minutes + 60 minutes
=
80 minutes
Now the subtraction can continue.
30. Complete Subtraction
After borrowing:
temp.hours = temp.hours - t.hours;
temp.minutes = temp.minutes - t.minutes;
For:
2 h 80 m
-1 h 40 m
we get:
2 - 1 = 1 hour
80 - 40 = 40 minutes
Final result:
1 hour 40 minutes
🟰 Part 7 — Assignment Operator =
Now let's understand:
t1 = t2;
This means:
Copy the data from
t2intot1.
We can overload it:
Time& Time::operator=(const Time &t)
{
if (this != &t)
{
hours = t.hours;
minutes = t.minutes;
}
return *this;
}
31. Why Is = Binary?
Look at:
t1 = t2;
There are two operands:
t1 = t2
↑ ↑
operand operand
Therefore:
=is a binary operator.
🧭 32. What Does this Mean?
Now we encounter:
this
Inside a member function, this is a pointer to the current object.
Suppose:
t1 = t2;
is being handled by:
operator=
Then:
current object → t1
other object → t2
So:
this
↓
t1
In simple words:
thistells us which object is currently calling the member function.
33. What Does *this Mean?
If:
this
is a pointer to the current object, then:
*this
means the actual current object.
Think:
this
↓
address of current object
*this
↓
actual current object
Therefore:
return *this;
means:
Return the current object.
34. Why Do We Check this != &t?
The code contains:
if (this != &t)
This checks for self-assignment.
For example:
t1 = t1;
Here the left and right objects are the same.
We don't need to copy an object onto itself.
So the condition asks:
"Is the current object different from the object being assigned?"
If yes, perform the copy.
35. Why Does operator= Return Time&?
Notice:
Time&
instead of:
Time
The & means the function returns a reference to the existing Time object.
Returning a reference allows assignment expressions to behave naturally and supports expressions such as:
t1 = t2 = t3;
For a beginner, remember:
Time&means we return the existingTimeobject by reference instead of creating another separate object.
⬆️ Part 8 — Prefix ++
Now let's move to unary operators.
Suppose:
t1 = 2 hours 59 minutes
We want:
++t1;
to increase the time by one minute.
We define:
Time& Time::operator++()
{
minutes++;
if (minutes >= 60)
{
minutes = 0;
hours++;
}
return *this;
}
36. Why Is ++ Unary?
Look at:
++t1;
Visual:
++
|
t1
Only one operand exists.
Therefore:
++is a unary operator.
37. What Does minutes++ Mean?
Inside the function:
minutes++;
means:
Increase the current object's minutes by one.
If:
minutes = 40
then:
minutes = 41
If:
minutes = 59
then:
minutes = 60
But normal time representation doesn't keep 60 minutes.
So we need:
if (minutes >= 60)
38. What Happens at 60 Minutes?
If:
minutes = 60
we do:
minutes = 0;
hours++;
So:
2 hours 60 minutes
becomes:
3 hours 0 minutes
Therefore:
2:59
↓
++t1
↓
3:00
39. What Is Prefix?
Prefix means the operator appears before the object:
++t1;
The basic idea is:
CHANGE FIRST
↓
USE NEW VALUE
For example:
Before → 2:59
++t1
After → 3:00
So prefix returns the updated object.
That's why we use:
return *this;
⬆️ Part 9 — Postfix ++
Now look at:
t1++;
It is also unary, but its behavior is different.
The implementation is:
Time Time::operator++(int)
{
Time temp = *this;
minutes++;
if (minutes >= 60)
{
minutes = 0;
hours++;
}
return temp;
}
40. Why Does Postfix Have int?
This is one of the most confusing parts for beginners.
Compare:
operator++()
and:
operator++(int)
The int is a dummy parameter.
It is used to distinguish the two forms.
operator++()
↓
PREFIX
operator++(int)
↓
POSTFIX
The int is not used to perform the increment.
It exists so C++ can distinguish prefix and postfix versions.
41. Why Do We Need temp for Postfix?
Suppose:
t1 = 2:59
When we write:
t1++;
postfix means:
Use the old value first, then change the object.
So first:
Time temp = *this;
Remember:
*this
means the current object.
Therefore:
temp = 2:59
t1 = 2:59
Then we increment the original object:
t1 = 3:00
But postfix needs to return the old value.
So:
return temp;
returns:
2:59
Final situation:
Returned value = 2:59
Current t1 = 3:00
42. Prefix vs Postfix
This is one of the most important things to remember.
Prefix
++t1;
OLD VALUE
↓
CHANGE
↓
NEW VALUE
↓
RETURN NEW VALUE
Postfix
t1++;
OLD VALUE
↓
SAVE OLD VALUE
↓
CHANGE ORIGINAL
↓
RETURN OLD VALUE
🧠 Easy memory trick
Prefix → change first
Postfix → save/use old value first
⬇️ Part 10 — Prefix --
The same basic idea applies to decrement.
Time& Time::operator--()
{
minutes--;
if (minutes < 0)
{
minutes = 59;
hours--;
}
return *this;
}
Suppose:
t1 = 3:00
Then:
--t1;
does:
3:00
↓
minutes becomes -1
↓
borrow from hour
↓
minutes = 59
hours = 2
↓
2:59
43. Why Do We Check minutes < 0?
Normally minutes should stay between:
0 and 59
If:
minutes = 0
and we subtract one:
minutes = -1
That's not a valid minute value.
So we convert:
3:00
into:
2:59
This is essentially the reverse idea of carrying an extra hour during addition.
⬇️ Part 11 — Postfix --
The postfix version is:
Time Time::operator--(int)
{
Time temp = *this;
minutes--;
if (minutes < 0)
{
minutes = 59;
hours--;
}
return temp;
}
Again:
t1--
↓
save old value
↓
decrement original
↓
return old value
Exactly the same pattern as postfix ++.
44. ++ and -- Summary
| Expression | Type | Meaning |
|---|---|---|
++t1 |
Unary | Prefix increment |
t1++ |
Unary | Postfix increment |
--t1 |
Unary | Prefix decrement |
t1-- |
Unary | Postfix decrement |
Remember:
Prefix:
change first → return new value
Postfix:
save old value → change → return old value
🖨️ Part 12 — Output Operator <<
Now let's understand one of the most confusing parts.
We already know:
cout << 10;
prints:
10
But what happens with:
Time t1;
cout << t1;
C++ doesn't automatically know how to print our custom Time object.
So we define what << should do for Time.
ostream& operator<<(ostream &out, const Time &t)
{
out << t.hours << " hours "
<< t.minutes << " minutes";
return out;
}
45. What Does cout << t1 Mean?
Think of:
cout << t1;
as:
<<
/ \
cout t1
There are two operands:
cout
t1
But there is an important difference from our earlier operators.
The left operand is:
cout
not:
Time
That is why stream operators are commonly implemented as non-member functions.
46. What Is a Non-Member Function?
A non-member function is simply a function that does not belong to the class as a member function.
Our earlier function was:
Time Time::operator+(...)
Notice:
Time::
So it belongs to the Time class.
But:
ostream& operator<<(...)
doesn't have:
Time::
before it.
So it is a non-member function.
Why?
Because:
cout << t1;
has:
left operand → cout
right operand → t1
The left operand isn't a Time object.
47. What Is ostream?
ostream means:
Output Stream
cout is an output stream.
Therefore:
ostream &out
means:
outis a reference to an output stream.
Inside the function:
out << t.hours;
means:
Send the hours to the output stream.
48. Why Is const Time &t Used for Output?
We have:
const Time &t
because printing the object should only read its values.
We don't want:
cout << t1;
to change:
t1.hours
t1.minutes
So:
OUTPUT
↓
read object
↓
don't modify it
↓
const Time&
49. Why Do We Return out?
At the end:
return out;
This allows output chaining.
For example:
cout << t1 << t2;
After printing t1, the stream is returned, allowing the next operation:
<< t2
That's why stream output operators commonly return:
ostream&
⌨️ Part 13 — Input Operator >>
Now let's look at:
cin >> t1;
We want the user to enter a time and store it inside the Time object.
So we overload >>:
istream& operator>>(istream &in, Time &t)
{
cout << "Enter hours: ";
in >> t.hours;
cout << "Enter minutes: ";
in >> t.minutes;
t.hours = t.hours + t.minutes / 60;
t.minutes = t.minutes % 60;
return in;
}
50. What Is istream?
istream means:
Input Stream
cin is an input stream.
Therefore:
istream &in
means:
inis a reference to an input stream.
51. Why Is Time &t Used for Input?
Notice:
Time &t
not:
const Time &t
Why?
Because input needs to modify the object.
Suppose:
cin >> t1;
The user enters:
2
90
The function needs to store these values inside t1.
Therefore:
cin
↓
input values
↓
modify Time object
So the Time object cannot be const.
52. Why Does Input Normalize the Time?
Suppose the user enters:
hours = 2
minutes = 90
The code:
t.hours = t.hours + t.minutes / 60;
t.minutes = t.minutes % 60;
does:
90 / 60 = 1
Therefore:
2 + 1 = 3 hours
And:
90 % 60 = 30
So:
2 hours 90 minutes
becomes:
3 hours 30 minutes
🔄 Part 14 — << vs >>
A very easy way to remember them:
Output
cout << t1;
Think:
Time object
↓
goes OUT
↓
cout
Therefore:
<<→ OUTPUT
Input
cin >> t1;
Think:
cin
↓
data goes INTO
↓
Time object
Therefore:
>>→ INPUT
🗺️ Part 15 — Complete Operator Map
Now let's put everything together.
OPERATOR OVERLOADING
|
┌─────────────────┼─────────────────┐
↓ ↓ ↓
BINARY UNARY STREAM
| | |
+ - = ++ -- << >>
| | |
2 operands 1 operand input/output
📋 Quick Reference Table
| Operator | Example | Type | Function |
|---|---|---|---|
+ |
t1 + t2 |
Binary | operator+() |
- |
t1 - t2 |
Binary | operator-() |
= |
t1 = t2 |
Binary | operator=() |
++ |
++t1 |
Unary | operator++() |
++ |
t1++ |
Unary | operator++(int) |
-- |
--t1 |
Unary | operator--() |
-- |
t1-- |
Unary | operator--(int) |
<< |
cout << t1 |
Stream | operator<<() |
>> |
cin >> t1 |
Stream | operator>>() |
⭐ Part 16 — The Most Important Mental Model
If you remember only one thing from this entire article, remember this.
When you see:
t1 + t2;
think:
t1.operator+(t2);
When you see:
t1 - t2;
think:
t1.operator-(t2);
When you see:
t1 = t2;
think:
t1.operator=(t2);
When you see:
++t1;
think:
t1.operator++();
When you see:
t1++;
think:
t1.operator++(int);
For output:
cout << t1;
conceptually:
operator<<(cout, t1);
For input:
cin >> t1;
conceptually:
operator>>(cin, t1);
🧠 One Big Picture
Think of Operator Overloading like this:
t1 + t2
|
↓
"What should + do
for Time objects?"
|
↓
operator+()
|
↓
calculate
|
↓
return Time
And for unary operators:
++t1
|
↓
operator++()
|
↓
change t1
|
↓
return result
And for streams:
cout << t1
|
↓
operator<<()
|
↓
read Time object
|
↓
display
❓ Part 17 — Common Beginner Questions
❓ Why can't I simply use t1 + t2?
Because C++ doesn't automatically know what + should mean for two objects of your custom Time class.
We define that behavior through Operator Overloading.
❓ Why is + binary?
Because:
t1 + t2
contains two operands.
❓ Why is ++ unary?
Because:
++t1
contains one operand.
❓ Why does postfix ++ have int?
Because C++ needs a way to distinguish:
operator++()
from:
operator++(int)
The first represents prefix.
The second represents postfix.
❓ What is this?
this is a pointer to the current object.
❓ What is *this?
*this represents the actual current object.
❓ Why is const used?
When we don't want a function to modify an object through a parameter.
For example:
const Time &t
means:
We can read the object, but we cannot modify it through
t.
❓ Why is & used?
It creates a reference.
A reference allows us to work with an existing object instead of creating another separate object.
❓ Why do we need temp?
For + and -, temp is used to build and store the result.
For postfix ++ and --, temp is used to save the old value before changing the original object.
❓ Why is output const?
Because output only needs to read the object.
❓ Why isn't input const?
Because input needs to change the object and store the entered values inside it.
🧪 Part 18 — Practice Questions
Before looking at the answers, try these yourself.
Question 1
Is this unary or binary?
t1 + t2;
Question 2
Is this unary or binary?
++t1;
Question 3
Which function handles this?
t1 + t2;
Question 4
Which function handles this?
t1++;
Question 5
Why does postfix ++ have int?
Question 6
What does this represent?
Question 7
What does *this represent?
Question 8
Why do we use % 60?
Question 9
Which operator handles:
cout << t1;
Question 10
Which operator handles:
cin >> t1;
✅ Answers
1. Binary
t1 + t2;
has two operands.
Therefore:
Binary
2. Unary
++t1;
has one operand.
Therefore:
Unary
3.
operator+()
4.
operator++(int)
5.
The int is a dummy parameter used to distinguish postfix ++ from prefix ++.
6.
this is a pointer to the current object.
7.
*this represents the actual current object.
8.
To keep minutes within the normal range of:
0–59
For example:
70 % 60 = 10
9.
operator<<()
10.
operator>>()
📝 Part 19 — Final Cheat Sheet
╔══════════════════════════════════════════════╗
║ C++ OPERATOR OVERLOADING ║
╚══════════════════════════════════════════════╝
BINARY
──────────────────────────────────────────────
t1 + t2 → operator+()
t1 - t2 → operator-()
t1 = t2 → operator=()
UNARY
──────────────────────────────────────────────
++t1 → operator++()
t1++ → operator++(int)
--t1 → operator--()
t1-- → operator--(int)
STREAM
──────────────────────────────────────────────
cout << t1 → operator<<()
cin >> t1 → operator>>()
IMPORTANT
──────────────────────────────────────────────
2 operands → Binary
1 operand → Unary
++t → Prefix → change first
t++ → Postfix → save old value first
--t → Prefix → change first
t-- → Postfix → save old value first
<< → Output
>> → Input
this → pointer to current object
*this → actual current object
const → don't modify through this reference
& → reference
🎯 Final Takeaway
Operator Overloading can look complicated when we first see:
Time Time::operator+(const Time &t)
But if we break it down:
Time
↓
return type
Time::
↓
belongs to Time class
operator+
↓
defines +
const
↓
don't modify the parameter
Time
↓
parameter type
&
↓
reference
t
↓
parameter name
Suddenly the declaration isn't so mysterious.
Then look at the body:
create result object
↓
add hours
↓
add minutes
↓
convert extra minutes
↓
return result
And the complete flow becomes:
t1 + t2
↓
t1.operator+(t2)
↓
operator+()
↓
calculate
↓
return Time object
That's the real idea behind Operator Overloading.
Don't memorize the syntax. Understand the relationship between the operator, the object, the function, and the result.
Once that mental model becomes clear, Operator Overloading stops looking like a collection of strange symbols and starts becoming a normal C++ concept.
📂 Complete Notes & Practice Material
If you want to go beyond this article, I've also organized the complete learning material in my GitHub repository.
It includes:
- 📖 Detailed beginner-friendly notes
- 🎨 Visual study notes
- 💻 Complete C++ code
- 🔍 Step-by-step concept explanations
- 🧪 Practice questions with answers
- 📌 Quick-reference / cheat sheets
👉 Explore the complete repository:
https://github.com/Poushmita/cpp_doubtClearance
You can use the repository alongside this article to read the concepts, explore the code, revise visually, and practice them yourself.
💭 Why I Like Learning This Way
This guide started with a simple situation: a friend asked me for help understanding Operator Overloading.
While explaining it, I realized that sometimes a concept feels difficult not because the concept itself is extremely complicated, but because too many small things are assumed to be already understood.
A beginner may understand Time but not Time&.
They may understand ++ but not why:
operator++(int)
contains int.
They may understand cout but not why:
ostream& operator<<(ostream &out, const Time &t)
looks different from:
Time Time::operator+(const Time &t)
So I decided to write these notes with one simple rule:
Don't just explain what the code does. Explain why the code is written that way.
If you're learning C++ and Operator Overloading has ever felt confusing, I hope this guide helps you understand it one small piece at a time.
And if you're helping someone else learn it, feel free to use these notes as a teaching resource. ❤️
If you find something that could be explained more clearly, feel free to suggest an improvement.
Top comments (0)