연산자 오버로딩

operator overloading

연산자 오버로딩은 C++에서 사용자 정의 타입에 대해 기존의 연산자를 재정의하여 사용할 수 있게 하는 기능입니다. 이를 통해 객체에 대해 자연스럽고 직관적인 연산을 수행할 수 있습니다.

기본 문법

ReturnType operator Op(ArgumentType) {
    // 구현
}
  • ReturnType: 연산 결과의 타입

  • Op: 오버로딩할 연산자 (예: +, -, *, ==, << 등)

  • ArgumentType: 연산자에 전달될 인수의 타입

실습 예제

ch07ex15_PointOperator.cpp

#include <iostream>
#include <string>

using namespace std;

class Point
{
	friend Point operator+(int num, const Point& other);
public:
	Point(int x = 0, int y = 0) : x(x), y(y) {}
	~Point() {}

	string getPoint() const
	{
		return "(" + to_string(this->x) + ", " + to_string(this->y) + ")";
	}

	Point operator+(Point& other)
	{
		return Point(this->x + other.x, this->y + other.y);
	}

	Point operator+=(const Point& other)
	{
		this->x += other.x;
		this->y += other.y;
		return *this;
	}

	Point& operator++()
	{
		this->x++;
		this->y++;
		return *this;
	}

	Point operator++(int)
	{
		return Point(this->x++, this->y++);
	}

	Point operator+(int a) const
	{
		return Point(this->x + a, this->y + a);
	}

	bool operator==(const Point& other)
	{
		return (this->x == other.x && this->y == other.y);
	}
	
	friend ostream& operator<<(ostream& os, const Point& p)
	{
	    os << "(" << p.x << ", " << p.y << ")";
	    return os;
	}
	
	friend istream& operator>>(istream& is, Point& p)
	{
	    is >> p.x >> p.y;
	    return is;
	}

private:
	int x;
	int y;
	static int pointCount;
};

Point operator+(int num, const Point& other)
{
	return Point(other.x + num, other.y + num);
}
int Point::pointCount = 0;

int main()
{
	Point p1(1, 2);
	Point p2(2, 3);
	Point p3;

	cout << "p1 == p2: " << (p1 == p2) << endl;

	p3 = p1 + p2;
	cout << "p1: " << p1.getPoint() << endl;
	cout << "p2: " << p2.getPoint() << endl;
	cout << "p3: " << p3.getPoint() << endl;
	cout << "p1 + p3: " << (p1 + p3).getPoint() << endl;

	p1 += p2;
	cout << "p1: " << p1.getPoint() << endl;

	p1 += p2 += p3;
	cout << "p1: " << p1.getPoint() << endl;
	cout << "p2: " << p2.getPoint() << endl;

	++p1;
	cout << "p1: " << p1.getPoint() << endl;
	
	++(++p1);
	cout << "p1: " << p1.getPoint() << endl;

	Point result;  // 생성자 호출
	result = p2++;  // p2.operator++(), result.operator=(p2) 호출
	cout << "result: " << result.getPoint() << endl;
	cout << "p2: " << p2.getPoint() << endl;

	Point p4 = p1 + 10;  // 복사 생성자 호출
	cout << "p4: " << p4.getPoint() << endl;

	Point p5 = 10 + p1;  // 좌측 피연산자가 기본 타입인 경우 메서드 호출 불가
	cout << "p5: " << p5.getPoint() << endl;

	cout << endl << endl;
	Point p6;
	cout << "좌표 x, y: ";
	cin >> p6;
	cout << "p6: " << p6 << endl;
	
	return 0;
}

Q1. 점의 좌표를 저장하는 Point class를 만들고, 두 점의 좌표가 같은지 비교하는 연산자 오버로딩을 구현하시오.

Q2. 두 점 p1, p2의 합을 구하는 + 연산자를 구현하시오.

Q3. 두 점의 합을 대입하는 += 복합연산자를 구현하시오.

Q4. 점 p1의 x, y좌표를 모두 1씩 더하는 ++연산자를 사용하도록 구현하시오. 만약 p1의 좌표가 (0, 0)이고, ++p1을 실행하면 p1의 좌표는 (1, 1)이 된다.

Q5. 위 문제 ++ 후위 연산자를 구현하시오. (후위 연산자는 인수로 int 형을 지정한다.)

Q6. 점의 좌표와 정수를 더하면 x, y좌표에 정수를 더하는 + 연산자를 구현하시오. (단, + 연산자는점의 좌표 + 정수 형태로 사용한다.)

Q7. 위 문제에서 정수 + 점의 좌표가 실행되도록 구현하시오.

Last updated