-
[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) 'C++ > class' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C++] μμκ³Ό μμ±μ/μλ©Έμ (0) 2022.07.08 [C++] ν΄λμ€μ μ¬μ© κ΄κ³, ν¬ν¨ κ΄κ³ (0) 2022.07.01 [C++] κ°μ²΄ λ°°μ΄ (0) 2022.07.01 [C++] κ°μ²΄μ λμ μμ± (0) 2022.07.01 [C++] βοΈλ³΅μ¬ μμ±μκ° νΈμΆλλ κ²½μ° (0) 2022.07.01