C ++ Increment decrement operator overloading
C ++ function overloading and operator overloading
Increment operator (++) and decrement operators (-) is a C ++ language in two important unary operators.
The following example shows how to reload the increment operator (++), including two uses prefixes and suffixes. Similarly, you can also try overloaded decrement operator (-).
#include <iostream> using namespace std; class Time { private: int hours; // 0 到 23 int minutes; // 0 到 59 public: // 所需的构造函数 Time(){ hours = 0; minutes = 0; } Time(int h, int m){ hours = h; minutes = m; } // 显示时间的方法 void displayTime() { cout << "H: " << hours << " M:" << minutes <<endl; } // 重载前缀递增运算符( ++ ) Time operator++ () { ++minutes; // 对象加 1 if(minutes >= 60) { ++hours; minutes -= 60; } return Time(hours, minutes); } // 重载后缀递增运算符( ++ ) Time operator++( int ) { // 保存原始值 Time T(hours, minutes); // 对象加 1 ++minutes; if(minutes >= 60) { ++hours; minutes -= 60; } // 返回旧的原始值 return T; } }; int main() { Time T1(11, 59), T2(10,40); ++T1; // T1 加 1 T1.displayTime(); // 显示 T1 ++T1; // T1 再加 1 T1.displayTime(); // 显示 T1 T2++; // T2 加 1 T2.displayTime(); // 显示 T2 T2++; // T2 再加 1 T2.displayTime(); // 显示 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