2015. 9. 2. 00:44 Programing/C++
간단한 오버라이딩
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int _x = 0, int _y = 0) :x(_x), y(_y) {}
void Print() const {
cout << x << ", " << y << endl;
}
const Point operator+(const Point &arg) const
{
Point pt;
pt.x = this->x + arg.x;
pt.y = this->y + arg.y;
return pt;
}
const Point& operator++()
{
++x;
++y;
return *this;
}
const Point operator++(int)
{
Point pt(x, y);
++x;
++y;
return pt;
}
};
int main()
{
Point pt1(2, 3);
Point pt2(3, 5);
++pt2;
Point pt3;
pt3 = pt1 + pt2;
pt3.Print();
pt2.Print();
return 0;
}
'Programing > C++' 카테고리의 다른 글
형변환 연산자 오버로딩 (0) | 2015.09.04 |
---|---|
-> , * 연산자 오버로딩 (0) | 2015.09.02 |
구조체로 구현한 Invader (0) | 2015.06.09 |
클래스 template (0) | 2015.04.27 |
함수 Template (0) | 2015.04.27 |