lab8_1.cpp

来自「清华大学出版社出版的c++程序设计课本」· C++ 代码 · 共 61 行

CPP
61
字号
#include <iostream.h>

class Point
{
private:
	int _x, _y;
public:
	Point& operator++();
	Point operator++(int);
	
	Point& operator--();
	Point operator--(int);
	
	Point() { _x = _y = 0; }
	
	int x() { return _x; }
	int y() { return _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;
}

void main()
{
	Point A;
	cout << "A的值为:" << A.x() << " , " << A.y() << endl;
	A++;
	cout << "A的值为:" << A.x() << " , " << A.y() << endl;
	++A;
	cout << "A的值为:" << A.x() << " , " << A.y() << endl;
	A--;
	cout << "A的值为:" << A.x() << " , " << A.y() << endl;
	--A;
	cout << "A的值为:" << A.x() << " , " << A.y() << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?