C++

C++ 연습문제 9.8

wooob 2021. 11. 7. 16:12
728x90

Point 클래스에 전위 감소 연산자와 후위 감소 연산자를 추가하고 이를 테스트해보라.

 

내 코드(main.cpp) : 

#include <iostream>
#include "point.h"
using namespace std;

int main() {
	Point pt1(1, 1);
	Point pt2 = pt1++;
	Point pt3 = ++(++pt1);
	//Point pt4 = (pt1++)++;
	Point pt5 = (++pt1)++;

	pt1.Print();
	pt2.Print();
	pt3.Print();
	//pt4.Print();
	pt5.Print();

	return 0;
}

 

point.h :

#include <iostream>
using namespace std;

class Point {
private:
	int x_, y_;

public:
	Point(int x = 0, int y = 0) : x_(x), y_(y) {}

	friend Point& operator++(Point& pt);
	friend Point operator++(Point& pt, int);
	
	void Print() {
		cout << "(" << x_ << ", " << y_ << ")" << endl;
	}
};

inline Point& operator++(Point& pt) {
	pt.x_++;
	pt.y_++;
	return pt;
}

inline Point operator++(Point& pt, int) {
	Point temp = pt;
	pt.x_++;
	pt.y_++;
	return temp;
}
728x90