Перевантаження операторів інкремента та декремента (++)
#include <iostream>
using namespace std;
class Point
{
public:
Point operator++(int); // постдекрементний оператор i++
Point& operator++(); // преінкрементний оператор ++i
Point operator--(int);
Point& operator--();
int x, y;
};
Point& Point::operator++()
{
x++;
y++;
return *this;
}
Point Point::operator++(int)
{
Point temp = *this;
++*this;
return temp;
}
Point& Point::operator--()
{
x--;
y--;
return *this;
}
Point Point::operator--(int)
{
Point temp = *this;
--*this;
return temp;
}
int main()
{
Point a;
a.x = 2;
a.y = 3;
a++;
cout << a.x << endl;
cout << a.y << endl << endl;
++a;
cout << a.x << endl;
cout << a.y << endl << endl;
system("pause");
return 0;
}
3
4
4
5