C++ Overloading ++ and -- Operators

C++ Operator Overloading and Function Overloading

The increment operator (++) and the 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 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 the prefix increment operator (++) Time operator++ () { ++minutes; // Add 1 to the object if(minutes >= 60) { ++hours; minutes -= 60; } return Time(hours, minutes); } // Overload the 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 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 above code is compiled and executed, it produces the following results:

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 represent an integer.

The prefix form overload calls Check operator ++ (), and the postfix form overload calls 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 the value of obj 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 result 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 the value of obj 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 result is:

i = 3
i = 3
i = 2
i = 2
i = 1
i = 2

C++ Operator Overloading and Function Overloading

other extensions