2070 / C++ / Класи / Перевантаження операторів / Перевантаження операторів інкремента та декремента i++, ++i, i--, --i

 

#include <iostream>
using namespace std;

class Point
{
  public:
    Point operator++(int);
    Point& operator++(); 
    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