ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] friend ν•¨μˆ˜μ™€ μ—°μ‚°μž 쀑볡
    C++/class 2022. 7. 5. 00:32
    πŸ’‘ friend ν•¨μˆ˜λž€?

     

    1. 멀버 ν•¨μˆ˜κ°€ μ•„λ‹Œ 일반 ν•¨μˆ˜λ‚˜ ν΄λž˜μŠ€μ— ν•œν•˜μ—¬ 클래슀의 λ‚΄λΆ€ 데이터에 μ ‘κ·Όν•  수 μžˆλ„λ‘ ν•˜λŠ” 것

    2. ν•¨μˆ˜μ˜ μ›ν˜• μ•žμ— friend ν‚€μ›Œλ“œ(μ˜ˆμ•½μ–΄)λ₯Ό 뢙인닀.

    3. ν”„λ Œλ“œ ν•¨μˆ˜λŠ” 클래슀의 멀버가 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— publicμ΄λ‚˜ private의 영ν–₯을 받지 μ•ŠλŠ”λ‹€.

    4. ν”„λ Œλ“œ ν•¨μˆ˜μ˜ μ›ν˜•μ€ 클래슀 μ•ˆμ—, ν•¨μˆ˜μ˜ κ΅¬ν˜„μ€ μ™ΈλΆ€μ—μ„œ λ”°λ‘œ μ •μ˜λœλ‹€.

    5. 

     

    μ˜ˆμ‹œ 1) λ‚ μ§œ 비ꡐ.cpp (두 개의 객체 비ꡐ)

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Date{
        //ν”„λ Œλ“œ ν•¨μˆ˜ μ›ν˜• μ„ μ–Έ
        friend bool isEqual(Date d1, Date d2);
        
    private:
        int year;
        int month;
        int day;
        
    public:
        Date(int y, int m, int d) : year(y), month(m), day(y){};
        
    };
    
    //클래슀 ν•¨μˆ˜ 본체 μ •μ˜
    bool isEqual(Date d1, Date d2){
        if(d1.year == d2.year && d1.month == d2.month && d1.day == d2.day)
            return true;
        else
            return false;
    }
    
    int main(){
        Date d1(1999, 07, 12), d2(1999, 07, 12);
        cout << isEqual(d1, d2) << endl;
        
        return 0;
    }

     

    μ˜ˆμ‹œ 2) 두 개의 λ³΅μ†Œμˆ˜ λ”ν•˜κΈ°.cpp (두 개의 객체 μ—°μ‚°)

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Complex{
        friend Complex add(Complex, Complex);
    private:
        double re, im;
    public:
        Complex(double r, double i) : re(r), im(i){};
        Complex(double r) : re(r), im(0){};
        Complex(){ re = im = 0;}
        
        void output(){
            cout << re << " + " << im << "i" << endl;
        }
    };
    
    Complex add(Complex a1, Complex a2){
        return Complex(a1.re+a2.re, a1.im+a2.im);
    }
    
    int main(){
        Complex c1(1,2), c2(3,4);
        Complex c3 = add(c1, c2);
        c3.output();
    }

    μ‹€ν–‰κ²°κ³Ό

     

     

     

     

    πŸ’‘ μ—°μ‚°μž 쀑볡 

     

    1. C++μ—μ„œλŠ” μ—°μ‚°μžλ₯Ό μ‚¬μš©μžκ°€ λ‹€μ‹œ μ •μ˜ν•  수 μžˆλ‹€.

    2. μ—°μ‚°μž μ€‘λ³΅μ΄λž€ μ—¬λŸ¬κ°€μ§€ μ—°μ‚°μžλ“€μ„ 기초 νƒ€μž…(μ •μˆ˜, μ‹€μˆ˜, ...)뿐만 μ•„λ‹ˆλΌ 객체에 λŒ€ν•΄μ„œλ„ μ μš©ν•˜λŠ” 것이닀.

    [μ—°μ‚°μž μ€‘λ³΅μ˜ ν˜•μ‹]

    λ°˜ν™˜ν˜• + operator + μ—°μ‚°μž (λ§€κ°œλ³€μˆ˜λͺ©λ‘)


    ' + ' μ—°μ‚°μž 쀑볡

     

    μ˜ˆμ‹œ 1) 두 개의 벑터λ₯Ό 합쳐 ν•˜λ‚˜μ˜ 벑터 λ§Œλ“€κΈ°.cpp (μΌλ°˜ν•¨μˆ˜λ‘œ κ΅¬ν˜„)

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Vector{
    private:
        double x, y;
    public:
        Vector(double x, double y){
            this->x = x;
            this->y = y;
        }
        
        //ν”„λ Œλ“œ ν•¨μˆ˜ μ›ν˜• μ •μ˜
        friend Vector operator+(const Vector& v1, const Vector& v2);
        
        void display(){
            cout << "(" << x << ", " << y <<")" << endl;
        }
    };
    
    Vector operator+(const Vector& v1, const Vector& v2){
        Vector v(0.0, 0.0);
        v.x = v1.x + v2.x;
        v.y = v1.y + v2.y;
        return v;
    }
    
    int main(){
        Vector v1(1,2), v2(3,4);
        Vector v3 = v1 + v2;
        v3.display();
        
        return 0;
    }

     

    Key Point
    Vector v3 = v1 + v2; // λ‘˜μ΄ λ”ν–ˆλ‹€!
    // μ»΄νŒŒμΌλŸ¬κ°€ μš”λ¬Έμž₯을 λ§Œλ‚˜λ©΄ μ»΄νŒŒμΌμ„ μœ„μ—μ„œλΆ€ν„° μ­‰ λ‚΄λ €μ˜€λ‹€κ°€ μ „μ—­ ν•¨μˆ˜ operator을 λ§Œλ‚¬μ—ˆλ‹€.
    // μš” λ¬Έμž₯을 operator+(v1, v2); μ΄λ ‡κ²Œ λ°”κΏˆ
    // μ™œλƒν•˜λ©΄ v1 + v2 λŠ” ν•¨μˆ˜ ν˜ΈμΆœλ¬Έμ΄ μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— μ»΄νŒŒμΌλŸ¬κ°€ λ°”κΏ”μ£ΌλŠ” κ²ƒ(μœ„μ— operator+ λ§Œλ“€μ–΄λ†“은 κ²ƒ λ°”νƒ•μœΌλ‘œ)


     

    μ˜ˆμ‹œ 2) 두 개의 벑터λ₯Ό 합쳐 ν•˜λ‚˜μ˜ 벑터 λ§Œλ“€κΈ°.cpp (λ©€λ²„ν•¨μˆ˜λ‘œ κ΅¬ν˜„)

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Vector{
    private:
        double x, y;
    public:
        Vector(double x, double y){
            this->x = x;
            this->y = y;
        }
    
        Vector operator+(Vector& v2){
            Vector v(0.0, 0.0);
            v.x = this -> x + v2.x;
            v.y = this -> y + v2.y;
            return v;
        }
        
        void display(){
            cout << "(" << x << ", " << y <<")" << endl;
        }
    };
    
    int main(){
        Vector v1(1,2), v2(3,4);
        Vector v3 = v1 + v2;
        v3.display();
        return 0;
    }

     

    μΌλ°˜ν•¨μˆ˜ κ΅¬ν˜„ λ©€λ²„ν•¨μˆ˜ κ΅¬ν˜„
    λͺ¨λ“  맀개 λ³€μˆ˜ ν”Όμ—°μ‚°μžμ˜ κ°œμˆ˜λ³΄λ‹€ ν•˜λ‚˜ 적은 맀개 λ³€μˆ˜
    Vector operator+(const Vector& v1, const Vector& v2){
        Vector v(0.0, 0.0);
        v.x = v1.x + v2.x;
        v.y = v1.y + v2.y;
        return v;
    }
        Vector operator+(Vector& v2){
            Vector v(0.0, 0.0);
            v.x = this -> x + v2.x;
            v.y = this -> y + v2.y;
            return v;
        }
    operator+(v1, v2)
    맀개 λ³€μˆ˜λ₯Ό 2개 가짐
    v1.operator+(v2)
    멀버 ν•¨μˆ˜: 첫 번째 ν”Όμ—°μ‚°μžκ°€ 객체(.dot operator)
    맀개 λ³€μˆ˜μ— (v1, v2)둜 쓰지 μ•Šλ„λ‘ 유의

     


    <<, >> μ—°μ‚°μž 쀑볡

    μΌλ°˜ν•¨μˆ˜ κ΅¬ν˜„ λ©€λ²„ν•¨μˆ˜ κ΅¬ν˜„
    cout << c1 c1 << cout
    operator<<(cout, c1) c1.operator<<(cout)

     

Designed by Tistory.