C++ ++ and -- Operator Overloading
C++ Overloaded Operators and Overloaded Functions
The increment operator (++) and the decrement operator (--) are two important unary operators in C++.
The following example demonstrates how to overload the increment operator (++), including both prefix and postfix usage. Similarly, you can also try overloading the decrement operator (--).
Example
#include
<
iostream
using
namespace
std;
class
Time
{
private
:
int
hours
;
// 0 to 23
int
minutes
;
// 0 to 59
public
:
// required constructor
Time
(
)
{
hours
=
0
;
minutes
=
0
;
}
Time
(
int
h
,
int
m
)
{
hours
=
h
;
minutes
=
m
;
}
// method to display time
void
displayTime
(
)
{
cout
<<
"
H:
"
<<
hours
<<
"
M:
"
<<
minutes
<<
endl
;
}
// overload prefix increment operator (++)
Time
operator
++
(
)
{
++
minutes
;
// add 1 to the object
if
(
minutes
=
60
)
{
++
hours
;
minutes
-=
60
;
}
return
Time
(
hours
,
minutes
)
;
}
// overload postfix increment operator (++)
Time
operator
++
(
int
)
{
// save the original value
Time
T
(
hours
,
minutes
)
;
// add 1 to the object
++
minutes
;
if
(
minutes
=
60
)
{
++
hours
;
minutes
-=
60
;
}
// return the old original value
return
T
;
}
}
;
int
main
(
)
{
Time
T1
(
11
,
59
)
,
T2
(
10
,
40
)
; ++
T1
;
// T1 plus 1
T1
.
displayTime
(
)
;
// display T1
++
T1
;
// T1 plus 1 again
T1
.
displayTime
(
)
;
// display T1
T2
++;
// T2 plus 1
T2
.
displayTime
(
)
;
// display T2
T2
++;
// T2 plus 1 again
T2
.
displayTime
(
)
;
// display T2
return
0
;
}
When the code above is compiled and executed, it produces the following result:
H: 12 M:0
H: 12 M:1
H: 10 M:41
H: 10 M:42
Note that int inside the parentheses is to tell the compiler that this is a postfix form, not to indicate an integer.
The prefix form overload is called operator++(), and the postfix form overload is called operator++(int).
Example (++ Overload)
#include
<
iostream
using
namespace
std;
class
Check
{
private
:
int
i
;
public
:
Check
(
)
:
i
(
0
)
{
}
Check
operator
++
(
)
{
Check
temp
;
temp
.
i
= ++
i
;
return
temp
;
}
// insert int in parentheses to indicate postfix
Check
operator
++
(
int
)
{
Check
temp
;
temp
.
i
=
i
++;
return
temp
;
}
void
Display
(
)
{
cout
<<
"
i =
"
<<
i
<<
endl
;
}
}
;
int
main
(
)
{
Check
obj
,
obj1
;
obj
.
Display
(
)
;
obj1
.
Display
(
)
;
// call the operator function, then assign obj's value to obj1
obj1
= ++
obj
;
obj
.
Display
(
)
;
obj1
.
Display
(
)
;
// assign obj to obj1, then call the operator function
obj1
=
obj
++;
obj
.
Display
(
)
;
obj1
.
Display
(
)
;
return
0
;
}
The execution output is:
i = 0
i = 0
i = 1
i = 1
i = 2
i = 1
Example (-- Overload)
#include
<
iostream
using
namespace
std;
class
Check
{
private
:
int
i
;
public
:
Check
(
)
:
i
(
3
)
{
}
Check
operator
--
(
)
{
Check
temp
;
temp
.
i
= --
i
;
return
temp
;
}
// insert int in parentheses to indicate postfix
Check
operator
--
(
int
)
{
Check
temp
;
temp
.
i
=
i
--;
return
temp
;
}
void
Display
(
)
{
cout
<<
"
i =
"
<<
i
<<
endl
;
}
}
;
int
main
(
)
{
Check
obj
,
obj1
;
obj
.
Display
(
)
;
obj1
.
Display
(
)
;
// call the operator function, then assign obj's value to obj1
obj1
= --
obj
;
obj
.
Display
(
)
;
obj1
.
Display
(
)
;
// assign obj to obj1, then call the operator function
obj1
=
obj
--;
obj
.
Display
(
)
;
obj1
.
Display
(
)
;
return
0
;
}
The execution output is:
i = 3
i = 3
i = 2
i = 2
i = 1
i = 2