Advertisement
junghu1124

source1

Dec 3rd, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class B {
  6.     int a;
  7. public:
  8.     B(): a(0) {}
  9.     B(int a): a(a) {}
  10.     void print() {
  11.         std::cout << a << std::endl;
  12.     }
  13.  
  14.     friend void f1() {
  15.         std::cout << "aa" << std::endl;
  16.     }
  17.  
  18.     friend void f2(B& b) { // 함수 정의도 friend
  19.         B b1{};
  20.         b1.a = 10;
  21.         std::cout << b1.a << std::endl;
  22.     }
  23.  
  24.     friend void f3(); // 함수 선언만 friend
  25.  
  26.     friend B operator+(const B& b1, const B& b2) {
  27.         return {b1.a + b2.a};
  28.     }
  29. };
  30.  
  31. void f3() {
  32.     B b;
  33.     std::cout << b.a << std::endl;
  34. }
  35.  
  36. int main() {
  37.     B b;
  38.     // f1(); // 해당 함수 찾을 수 없음
  39.     f2(b);
  40.     f3();
  41.  
  42.     B b1(10), b2(20);
  43.     B b3 = b1 + b2;
  44.     b3.print();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement