728x90
반응형

[Vector 연산자]

// 0120.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다.
//

#include <iostream>

class CVector {
public:
	float x, y;
	CVector(float x, float y) {
		this->x = x;
		this->y = x;
	}

	void output() {
		printf("%.02f %.02f", x, y);
	}

};


int main()
{
	CVector a(2.0f, 1.0f);
	a.output();
}


[연산자 재정의]

#include <iostream>

class CVector {
public:
	float x, y; // 성분값이다. z는 버린다.
	CVector() {}
	CVector(float x, float y) {
		this->x = x;
		this->y = x;
	}

	void output() {
		printf("%.02f %.02f\n", x, y);
	}
	CVector add(CVector v) {
		CVector r;
		r.x = x + v.x;
		r.y = y + v.y;
		return r;
	}
	//함수이름 : operator + 
	CVector operator +(CVector v) {
		CVector r;
		r.x = x + v.x;
		r.y = y + v.y;
		return r;
	}
};


int main()
{
	CVector a(2.0f, 1.0f), b(1.0f, 2.0f);
	a.output();
	CVector c = a.add(b);
	c.output();
	CVector d = a.operator+(b);
	d.output();

	CVector e = a + b; // 컴파일이 되면 d코드가 된다.
	d.output();
}

728x90
반응형

'Education > Edu | .net' 카테고리의 다른 글

# 14.1) [MFC] 시작하기6  (0) 2021.01.22
# 13) [MFC] 다이얼로그 간 데이터 교환, 통신  (0) 2021.01.22
# 12.1) [MFC] 시작하기5  (0) 2021.01.20
# 11.2) [MFC] 시작하기4  (0) 2021.01.19
# 11.1) [MFC] 주식 프로그램  (0) 2021.01.19

+ Recent posts