C++ ++ and -- Operator Overloading
C++ Operator Overloading and Overloaded Functions
The increment operator (++) and decrement operator (--) are two important unary operators in the C++ language.
The following example demonstrates how to overload the increment operator (++), including both prefix and postfix usages. Similarly, you can also try to overload 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; // Increment 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);
// Increment 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; // Increment T1
T1.displayTime(); // Display T1
++T1; // Increment T1 again
T1.displayTime(); // Display T1
T2++; // Increment T2
T2.displayTime(); // Display T2
T2++; // Increment T2 again
T2.displayTime(); // Display T2
return 0;
}
When the above code 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 inform the compiler that this is a postfix form, not an integer.
The prefix form overload calls Check operator ++ ()
, and the postfix form overload calls operator ++ (int)
.
Example (++ Overloading)
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
Check operator ++ ()
{
Check temp;
temp.i = ++i;
return temp;
}
// Inserting int in parentheses indicates postfix
Check operator ++ (int)
{
Check temp;
temp.i = i++;
return temp;
}
void Display()
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
Check operator ++ ()
{
Check temp;
temp.i = ++i;
return temp;
}
// Postfix form
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 and 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;
}
Execution output:
i = 0
i = 0
i = 1
i = 1
i = 2
i = 1
Example (-- Overloading)
#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 and 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;
}
Execution output:
i = 3
i = 3
i = 2
i = 2
i = 1
i = 2