/*
* 문제 1.
*
* 아래 조건을 만족하는 -연산자 오버로딩을 해보고자 한다
*
* 1. 전역함수 기반으로 오버로딩
* 2. 멤버 별 -연산의 결과를 담은 Point 객체 반환
*/
/*
* 문제 2.
*
* 아래 조건을 만족하는 +=, -=연산자 오버로딩을 해보고자 한다
*
* 1. 멤버함수 기반으로 오버로딩
* 2. += 결과로 pos1의 멤버변수 값이 pos2의 멤버 변수 값만큼 멤버 별 증가
* 3. -= 결과로 pos1의 멤버변수 값이 pos2의 멤버 변수 값만큼 멤버 별 감소
* 4. 연산 결과로 값이 증감한 pos1의 객체를 반환하도록, 되도록이면 참조형으로 반환
*/
/*
* 문제 3.
*
* 아래 조건을 만족하는 !=, ==연산자 오버로딩을 해보고자 한다
*
* 1. 전역함수 기반으로 오버로딩
* 2. == 결과로 pos1의 모든 멤버의 값이 같다면 true, 아니면 false
* 3. != 결과로 pos1의 모든 멤버의 값이 같다면 false, 아니면 true
* 4. == 를 먼저 오버로딩 한 다음에, 이를 이용하는 형태로 != 연산자 오버로딩
*/
#include<iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0) : xpos(x), ypos(y)
{ }
void ShowPosition() const
{
cout << '[' << xpos << ", " << ypos << ']' << endl;
}
friend Point operator+(const Point& pos1, const Point& pos2);
friend Point operator-(const Point& pos1, const Point& pos2);
Point& operator+=(const Point& pos1);
Point& operator-=(const Point& pos1);
friend bool operator==(const Point& pos1, const Point& pos2);
friend bool operator!=(const Point& pos1, const Point& pos2);
};
bool operator==(const Point& pos1, const Point& pos2) {
if ((pos1.xpos == pos2.xpos) && (pos1.ypos == pos2.ypos)) {
return true;
}
return false;
}
bool operator!=(const Point& pos1, const Point& pos2) {
if (operator==(pos1, pos2)) {
return false;
}
return true;
}
Point& Point::operator+=(const Point& pos1) {
xpos += pos1.xpos;
ypos += pos1.ypos;
return *this;
}
Point& Point::operator-=(const Point& pos1) {
xpos -= pos1.xpos;
ypos -= pos1.ypos;
return *this;
}
Point operator+(const Point& pos1, const Point& pos2)
{
Point pos(pos1.xpos + pos2.xpos, pos1.ypos + pos2.ypos);
return pos;
}
Point operator-(const Point& pos1, const Point& pos2) {
Point pos(pos1.xpos - pos2.xpos, pos1.ypos - pos2.ypos);
return pos;
}