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

2021. 3. 19. 19:40·SW개발/C++
반응형

연계, 제휴 관계 (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
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ IntArray  (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++ 10장  (0) 2021.03.19
'SW개발/C++' 카테고리의 다른 글
  • C++ IntArray
  • C++ 의존 관계 (Dependency)
  • C++ 구성 관계 (Composition Relationship)
  • C++ 객체들의 관계 (Object Relationship)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바