English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C++ Operator Overloading and Overloaded Functions
Increment operator ( ++ ) and decrement operator ( -- ) is C++ Two important unary operators in the language.
The following example demonstrates how to overload the increment operator ( ++ )), including prefix and postfix usage. -- )
#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; // object plus 1 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); // object plus 1 ++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 T1.displayTime(); // Display T1 T2++; // T2 plus 1 T2.displayTime(); // Display T2 T2++; // T2 plus 1 T2.displayTime(); // Display T2 return 0; }
When the above code is compiled and executed, it will produce 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 inform the compiler that this is a postfix form, not an integer.
Prefix form of overload call Check operator ++ (), postfix form of overload call operator ++ (int).
#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 the parentheses to represent 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 the value of obj to obj1 obj1 = ++obj; obj.Display(); obj1.Display(); // Assign obj to obj1, and then call the operator function obj1 = obj++; obj.Display(); obj1.Display(); return 0; }Execution output result is:
i = 0 i = 0 i = 1 i = 1 i = 2 i = 1
#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 the parentheses to represent 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 the value of obj to obj1 obj1 = --obj; obj.Display(); obj1.Display(); // Assign obj to obj1, and then call the operator function obj1 = obj--; obj.Display(); obj1.Display(); return 0; }Execution output result is:
i = 3 i = 3 i = 2 i = 2 i = 1 i = 2