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;
}