#include <iostream> using namespace std; class Time { private: int hours; // 0 to 23 int minutes; // 0 to 59 public: // required constructors 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; } Time& operator++() { ++minutes; if (minutes>=60) { ++hours; minutes = minutes - 60; } return *this; } Time& Time::operator++(int){ Time temp(hours, minutes); minutes++; if (minutes >= 60) { hours++; minutes = minutes - 60; } return *this; } }; int main() { Time T1(11, 59), T2(10, 40); T2 = ++(++T1); // T1.displayTime(); // display 12,01 ok T2.displayTime(); // display 12,01 ok T2 = (T1++)++; T1.displayTime(); // display 12,03 οκ T2.displayTime(); // display 12,03 why?? i think it must be 12,01 return 0; }
Time operator++(int)
T2 = ++(++T1);
temp
Time operator++(int) { Time temp(hours, minutes); minutes++; if (minutes >= 60) { hours++; minutes = minutes - 60; } // Return unchanged object here! return temp; }
Time operator++(int) { Time temp = *this; *++this; return temp; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)