Overloading
๊ฐ์ ํจ์์ด๋ฆ, ํน์ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด์, ํ๋ ์ด์์ ์ ์๊ฐ ๊ฐ๋ฅํ ๋ฐฉ๋ฒ์ด๋ค.
Function Overloading
์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ๊ณผ ๋ฐฉ์์ด ๋์ผํ๋ค. ๋ฐ์์ ์๋ฅผ ๋ ๊ฒ์ ๋ณด๊ณ ์ดํดํด๋ณด์.
Operator Overloading
Point operator +(Point& pt){
Point result(this->x + pt.x, this->y + pt.y);
return result;
}
pt1(1, 2);
pt2(3, 4);
pt1 + pt2;
// ์ฌ๊ธฐ์ pt1์ + ์ฐ์ฐ์๋ฅผ ํธ์ถํ์ ๋, ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์ด๊ฐ๋ input parameter๊ฐ ๋๋ค.
// ์ด๋
์์ด ํธ์ถํ๋ค๊ณ ์ปดํ์ผ๋ฌ๋ ์ฒ๋ฆฌํ๋ค.
// ๋ฐ๋ผ์ ์ด๋
์์ด ํธ์ถํ๊ธฐ ๋๋ฌธ์ + ์ฐ์ฐ์์ ์์์
// ํธ์ถํ ๋
์์ ๋ฉค๋ฒ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์ฐ๊ธฐ ์ํด์๋ this ํฌ์ธํฐ๋ก ์ ๊ทผํ๋ฉด ๋๋ค.
4, 6
์ด ์ฐ์ฐ์๋, Point ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๊ณ โ+โ operator ์ ๋ํด์, ์ค๋ฅธ์ชฝ์ Point ๋ณ์๋ฅผ reference๋ก ๋ฐ๋๋ค.
๋ฐ์ Point ๊ฐ์ฒด์ ๋ํด, result ๋ผ๋ Point ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ , ์ด๊ธฐ๊ฐ์ pt1์ ๋ฉค๋ฒ๋ณ์ x์ ๋์ค์ ๋์จ pt2์ x๋ฅผ ๋ํ ๊ฐ์ result์ ๋ฉค๋ฒ๋ณ์ x๋ก ํ ๋นํ๋ค.
๊ฐ์ ์๋ฆฌ๋ก result์ ๋ฉค๋ฒ๋ณ์ y๋ ํ ๋นํ๋ค. ๊ทธ๋ฆฌ๊ณ Point ๊ฐ์ฒด result๋ฅผ ๋ฐํํ๋ค.
Point operator = (Point& pt){
this->x = pt.x;
this->y = pt.y;
return (*this);
}
// ์ฐ์ฐ์์ ์ผ์ชฝ์ ์๋ ๊ฐ์ฒด๊ฐ ํธ์ถํ๋ค๊ณ ์๊ฐํ๋ค.
// ๊ทธ๋์ ๊ทธ ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๋ ๊ฒ ์ฌ๊ธฐ์ this ํฌ์ธํฐ.
// ๊ทธ๋์ ํธ์ถํ ๊ฐ์ฒด์ x์ ๋ฐ์ input์ x๋ฅผ ํ ๋นํ๋ค.
// ๊ฐ์ ๋ฐฉ์์ผ๋ก y๋ ์ํํ๋ค.
pt1(1, 2);
pt2(3, 4);
pt3 = pt1 + pt2;
// ์ค๋ฅธ์ชฝ์ ๊ณ์ฐ๋ ๊ฐ์ ์ผ์ชฝ์ ํ ๋นํ๋ค.
์ธ๋ถ์์ ์ ์ํ operator overloading
ํด๋์ค ๋ด๋ถ์์ ๋ชจ๋ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ์ ํ๊ธฐ ํ๋ค ์ ์๋ค. ์๋ํ๋ฉด, ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํด๋์ค ๋ด๋ถ์์ ์ ์ํ๊ฒ ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๋ ๊ฒ ๋ ๊ฒฝ์ฐ ์ฝ๋๊ฐ ์ง์ ๋ถํด์ง๊ณ , ์์กด์ฑ์ด ์๊ธธ ์ ์๋ค.
๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ ํด๋์ค ์ธ๋ถ์์ ํจ์๋ฅผ ์ ์ํ๊ณ ์ด๊ฒ์ด ํด๋์ค์ ์ฐ๊ด ๋์ด ์๋ค๊ณ ์๋ ค์ฃผ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
์ด๊ฒ์ย friendย ์์ ๋ฐฐ์๋ณด๊ธฐ๋ก ํ๊ณ ์ผ๋จ์, ์ธ๋ถ์์ ์ ์ํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์.
ostream& operator <<(ostream& cout, Point& pt){
cout << pt.getX() << ", " << pt.getY();
return cout;
}
๊ธฐ๋ณธ์ ์ผ๋ก operator๋ ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ๋๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ๋๋ค.
์ฒซ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋, ์ฐ์ฐ์ ์ผ์ชฝ์ ๋์ธ ๊ฐ์ฒด, ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋, ์ฐ์ฐ์ ์ค๋ฅธ์ชฝ์ ๋์ธ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค.
์ด ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ์ ๋ง๋ก ์ค๋ช
ํด๋ณด๋ฉด, ์ด ์ฐ์ฐ์๋ ostream์ ๊ฐ์ฒด๋ฅผ reference ๋ก ๋ฆฌํดํ๋ค. <<
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์ด ์ผ์ชฝ์๋ ostream ์ ๊ฐ์ฒด๋ฅผ cout์ด๋ ์ด๋ฆ์ผ๋ก callbyreferenceํ๋ค.
์ค๋ฅธ์ชฝ์๋ Point ๊ฐ์ฒด๋ฅผ pt๋ผ๋ ์ด๋ฆ์ผ๋ก callbyreference ํ๋ค.
๊ทธ๋ ๊ฒ ํ์ ๋, cout์ผ๋ก ๊ฐ๊ฐ์ ์ถ๋ ฅํ๊ณ , cout ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค.
์ฌ๊ธฐ์, ์ฐ๋ฆฌ๊ฐ cout ๊ฐ์ฒด๋ก ์ฌ๋ฌ๋ฒ ์ถ๋ ฅ์ด ๊ฐ๋ฅํ ์ด์ ๋, cout ๊ฐ์ฒด๋ฅผ ๋ฐํย ํ๊ธฐ ๋๋ฌธ์ด๋ผ๋ ๊ฒ์ ์ ์ ์๋ค.
๊ฐ์ ์๋ฆฌ๋ก, int, double๊ณผ ๊ฐ์ ๋ชจ๋ ์๋ฃํ์ ๋ํดย ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉย ์ ๋ฐฉ์์ผ๋ก ์์ ๊ฐ์ด ์ ์ธ๋์ด ์๋ค.
ํจ์ ์ค๋ฒ๋ก๋ฉ๋ ์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ตฌํ๋์ด ์๋ค.
์ฌ๊ธฐ๊น์ง ๊ตฌํํ ์ ์ฒด ์ฝ๋
#include <iostream>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(): x(0), y(0){}
Point(int _x, int _y): x(_x), y(_y) {}
void setXY(int _x, int _y){
this->x = _x;
this->y = _y;
}
int getX() const { return this->x; }
int getY() const { return this->y; }
Point operator + (Point& pt){
Point result(this->x + pt.x, this->y + pt.y);
return result;
}
Point operator - (Point& pt){
Point result(this->x - pt.x, this->y - pt.y);
return result;
}
Point operator = (Point& pt){
this->x = pt.x;
this->y = pt.y;
return (*this);
}
};
void print(const Point& pt){
cout << pt.getX() << ", " << pt.getY() << endl << endl;
// ๋ง์ฝ x, y๋ฅผ public ์ผ๋ก ์ ์ธํ๋ค๋ฉด ๋ค์๋ ๊ฐ์ ์ฝ๋
// cout << pt.x << ", " << pt.y << endl << endl;
}
ostream& operator << (ostream& cout, Point& pt){
cout << pt.getX() << ", " << pt.getY();
return cout;
}
int main(){
// print ์ฌ์ฉ, const
Point pt1(1, 2);
print(pt1);
// ๋์ ํ ๋น, ํด์ , new, delete
Point* pPt2 = new Point(10,20);
print(*pPt2);
// -> ์ฌ์ฉ
cout << pPt2->getX() << ", " << pPt2->getY() << endl << endl;
delete pPt2;
// ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
Point pt3(5, 6), pt4(7, 8);
Point pt5 = pt3 + pt4;
cout << pt3 << endl;
cout << pt4 << endl;
cout << pt5 << endl;
return 0;
}