[C++] 스터디 CPPALOM 5주차: 열혈 C++ 프로그래밍 Chap 10-12
(파워 포인트 파일(.pptx)을 Markdown으로 변환하여 업로드하였음) # <br>연산자 오버로딩 * C++의 모든 연산자는 내부적으로 operator+, operator- 등으로 호출된다. * 이는 다음 두 가지 방식으로 오버로딩할 수 있다: * 멤버 함수에 의한 연산자 오버로딩 * 전역 함수에 의한 연산자 오버로딩 ```C++ int main(void) { Point pos1(3, 4); Point pos2(10, 20); Point pos3=pos1.operator+(pos2); pos1.ShowPosition(); pos2.ShowPosition(); pos3.ShowPosition(); Point pos4=pos1+pos2; pos4.ShowPosition(); return 0; } ``` ```C++ 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; } Point operator+(const Point &ref) //operator+��� �̸��� �Լ� { Point pos(xpos+ref.xpos, ypos+ref.ypos); return pos; } }; ``` ```C++ [3, 4] [10, 20] [13, 24] ``` * 전역 함수에 의한 연산자 오버로딩 * 오버로딩된 전역 함수는 Point 객체의 멤버에 접근해야 한다. * 따라서 friend 키워드를 이...