1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| #include <iostream>
using namespace std;
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; } void operator+=(const Point &pos1) //pos1+=pos2; --> pos1=pos1+pos2 { xpos += pos1.xpos; ypos += pos1.ypos; } void operator-=(const Point &pos1) { xpos -= pos1.xpos; ypos -= pos1.ypos; }
Point& operator-() { xpos=-xpos; ypos=-ypos; return *this; } Point& operator++() { xpos++; ypos++; return *this; } //전위 증가 const Point operator++(int) { const Point refobj(xpos, ypos); //복사본 생성 xpos++; ypos++; return refobj; //반환받을 한 라인에서만 사용하고 사라질 복사본 반환 const로 } //후위증가 friend Point operator+(const Point &pos1, const Point &pos2); friend Point operator-(const Point &pos1, const Point &pos2); friend bool operator==(const Point &pos1, const Point &pos2); friend bool operator!=(const Point &pos1, const Point &pos2); friend Point& operator~(Point &pos); friend Point& operator--(Point& ref); friend const Point operator--(Point& ref, int); friend Point operator*(int times, Point& ref); friend Point operator*(Point& ref, int times); friend ostream& operator<<(ostream& os, const Point& pos); friend istream& operator>>(istream& is, Point& pos) ; };
Point operator+(const Point &pos1, const Point &pos2) //기존의 point 값이 변하지 않도록 const키워드를 붙이는것 이 중요하다. { Point pos(pos1.xpos+pos2.xpos, pos1.ypos+pos2.ypos); return pos; } Point operator-(const Point &pos1, const Point &pos2) { Point pos(pos1.xpos-pos2.xpos, pos1.ypos-pos2.ypos); return pos; }
bool operator==(const Point &pos1, const Point &pos2) { if(pos1.xpos == pos2.xpos && pos1.ypos == pos2.ypos) { return true; } else { return false; } } bool operator!=(const Point &pos1, const Point &pos2) { if(pos1.xpos == pos2.xpos && pos1.ypos == pos2.ypos) { return false; } else { return true; } }
Point& operator--(Point& ref) { ref.xpos--; ref.ypos--; return ref; } //전위 증가
const Point operator--(Point& ref, int) { const Point refobj(ref); //복사본 생성 ref.xpos--; ref.ypos--; return refobj; //반환받을 한 라인에서만 사용하고 사라질 복사본 반환 }
Point& operator~(Point &pos) { int temp=pos.xpos; pos.xpos = pos.ypos; pos.ypos = temp; return pos; }
//교환법칙 Point operator*(int times, Point& ref) //int형*point형 { Point pos(ref.xpos*times, ref.ypos*times); return pos; } Point operator*(Point& ref, int times) //point형*int형 { Point pos(ref.xpos*times, ref.ypos*times); return pos; }
//전역함수로 cout<<pos 가능하게 만들기 os가 cout클래 ostream& operator<<(ostream& os, const Point& pos) { os<<'['<<pos.xpos<<','<<pos.ypos<<']'<<endl; return os; } istream& operator>>(istream& is, Point& pos) //값을 집어넣어야 하기때문에 const를 뺴야함 { is>>pos.xpos>>pos.ypos; return is; }
int main() { Point pos1(3,4); Point pos2(10,20);
Point pos3 = pos1+pos2;
pos1.ShowPosition(); pos2.ShowPosition(); pos3.ShowPosition(); pos3=pos2-pos1; pos3.ShowPosition(); pos3+=pos1; pos3.ShowPosition(); pos3-=pos2; pos3.ShowPosition();
Point pos4(1,1); if(pos3==pos4) { cout<<"같습니다"<<endl; } pos4=pos1+pos2;
if(pos3!=pos4) { cout<<"같지 안습니다."<<endl; } pos4=-pos4; pos4.ShowPosition(); pos4=-pos4; pos4.ShowPosition(); pos4=~pos4; pos4.ShowPosition(); cout<< "---------------------------------"<<endl; (++(++pos1)).ShowPosition(); cout<< "전위끝"<<endl; pos1.ShowPosition();
pos1++.ShowPosition(); //후위증가는 Pointer&전달한게 아니기 때문에 pos1.ShowPosition(); Point cpy = pos1*3; cpy.ShowPosition(); cpy = pos1*3; cpy.ShowPosition(); cin>>cpy; cout<<cpy; cpy=pos1; cpy.ShowPosition(); //디폴트 대입연산자 생성&호출 }
|