대입 연산자.

phoenixKim·2022년 5월 9일
0

temp

목록 보기
6/11

소스코드

#include <stdio.h>

#include <iostream>
using namespace std;

class Point
{
private : 
	int x_, y_;
public : 
	Point() = default;
	Point(int _x, int _y) : x_(_x), y_(_y){}
	void Print() const { cout << x_ << " " << y_ << endl; }

	// 후위 연산자.
	Point operator++(int)
	{
		Point p1(*this);
		++x_;
		++y_;
		return p1;

	}
	// 전위 연산자.
	Point& operator++()
	{
		++x_;
		++y_;

		return *this;
	}

	// 대입 3번을 하기 위해서 void가 아닌 참조를 사용해야 함.
	Point& operator=(const Point &other)
	{
		//cout << "대입 연산자." << endl;
		x_ = other.x_;
		y_ = other.y_;
		return *this;
	}
};

int main()
{
	int a = 1;
	int b = 2;
	int c = 5;
	c = a = b;

	Point p1(1, 1);
	Point p2(2, 2);

	Point p3(p1); // 1 , 1 
	Point p4 = p1; // 1 , 1 
	p4 = p2; // 2 , 2
	p4.Print();
	p2 = p1 = p3; 
	p2.Print(); // 1 , 1 나와야 함.
}

profile
🔥🔥🔥

0개의 댓글

관련 채용 정보