본문 바로가기
C++/열혈 C++

열혈 C++ 연습문제 10-1-3(+=연산자 오버로딩)

by Beijing_KingGod 2018. 4. 8.

#include<iostream>
using namespace std;

class Point{
private:
 int x,y;
public:
 Point(int x,int y):x(x),y(y){};
 void operator+=(const Point& p);
 void ShowPosition();
};

void Point::operator+=(const Point& p)
 // 멤버 변수를 조작함으로 멤버함수 상수화를 해제했다.
 // const 키워드 없음
{
 x+=p.x;
 y+=p.y;
}
void Point::ShowPosition()
{
 cout<<x<<" "<<y<<endl;
}


int main(void)
{
 Point p1(2,1);
 Point p2(2,1);
 p1+=p2;
 p1.ShowPosition();
 return 0;
}

댓글