C++ 연계, 제휴 관계 (Association)

2021. 3. 19. 19:40·C++/Class

연계, 제휴 관계 (Association)

  • 의사 클래스와 환자 클래스로 비유

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Doctor;
    
    class Patient
    {
      string            name_;
      vector<Doctor*>    doctors_;
    
    public:
      Patient(const string &name_in)
        : name_{name_in}
      {}
    
      void    addDoctor(Doctor* new_doctor)
      {
        doctors_.push_back(new_doctor);
      }
    
      void    meetDoctors();
    
      friend class Doctor;
    };
    
    class Doctor
    {
      string                name_;
      vector<Patient*>    patients_;
    
    public:
      Doctor(const string &name_in)
        : name_{name_in}
      {}
    
      void    addPatient(Patient* new_patient)
      {
        patients_.push_back(new_patient);
      }
    
      void    meetPatients()
      {
        for (auto& e : patients_)
          cout << "Meet patient : " << e->name_ << '\n';
      }
    
      friend class Patient;
    };
    
    void    Patient::meetDoctors()
    {
      for (auto& e : doctors_)
        cout << "Meet doctor : " << e->name_ << '\n';
    }
    
    int        main()
    {
      Patient* p1 = new Patient("Jack Jack");
      Patient* p2 = new Patient("Dash");
      Patient* p3 = new Patient("Violet");
    
      Doctor* d1 = new Doctor("Doctor K");
      Doctor* d2 = new Doctor("Doctor L");
    
      p1->addDoctor(d1);
      d1->addPatient(p1);
    
      p2->addDoctor(d2);
      d2->addPatient(p2);
    
      p2->addDoctor(d1);
      d1->addPatient(p2);
    
      cout << "p1\n";
      p1->meetDoctors();
    
      cout << "\nd1\n";
      d1->meetPatients();
    
      delete p1, p2, p3, d1, d2;
    
    }
    
    /* stdout
    p1
    Meet doctor : Doctor K
    
    d1
    Meet patient : Jack Jack
    Meet patient : Dash
    */
저작자표시 (새창열림)

'C++ > Class' 카테고리의 다른 글

C++ 상속 (Inheritance)  (0) 2021.03.19
C++ 의존 관계 (Dependency)  (0) 2021.03.19
C++ 구성 관계 (Composition Relationship)  (0) 2021.03.19
C++ 객체들의 관계 (Object Relationship)  (0) 2021.03.19
C++ Nested Types  (0) 2021.03.16
'C++/Class' 카테고리의 다른 글
  • C++ 상속 (Inheritance)
  • C++ 의존 관계 (Dependency)
  • C++ 구성 관계 (Composition Relationship)
  • C++ 객체들의 관계 (Object Relationship)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    KakaoTalk
    vscode
    로지텍 마우스 제스처
    백기선
    dism
    Solaris 10
    citrix workspace
    알림
    Workspace
    윈도우 명령어
    그림판
    Windows 11
    unix
    SunOS 5.1
    스프링 프레임워크 핵심 기술
    java
    맥북 카카오톡 알림 안뜸
    스프링
    mspaint
    logi options
    SFC
    MacOS
    EXCLUDE
    시스템 복구
    윈도우
    windows
    spring
    제외
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 연계, 제휴 관계 (Association)
상단으로

티스토리툴바