티스토리 뷰

언어/C++

10.4 제휴 관계

js0331 2020. 4. 27. 20:19

제휴 관계일 경우에는

 

어느 클래스를 전방선언 해줘야하고

 

일부 함수를 따로 분리해줘서 설계를 해줄 필요가 있음.

 

 

제휴관계란 동등한 관계. 서로가 서로를 사용하는 입장.

 

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Doctor;

class Patient
{
	private:
	string m_name;
	vector<Doctor*> m_doctors;
	
	public:
	Patient(string name_in)
		: m_name(name_in)
	{}
	
	void addDoctor(Doctor * new_doctor)
	{
		m_doctors.push_back(new_doctor);
	}
	
	void meetDoctors();

	friend class Doctor;
};

class Doctor
{
	private:
	string m_name;
	vector<Patient*> m_patients;
 	
	public:
	Doctor(string name_in)
		: m_name(name_in)
	{}
	
	void addPatient(Patient * new_patient)
	{
		m_patients.push_back(new_patient);
	}
	
	void meetPatients()
	{
		for(auto & element : m_patients)
		{
			cout << "Meet Patient : " << element->m_name << endl;
		}
	}
	
	friend class Patient;
};

void Patient::meetDoctors()
	{
		for(auto & element : m_doctors)
		{
			cout << "Meet doctor : " << element->m_name << endl;
		}
	}

int main()
{
	Patient *p1 = new Patient("Jack Jack");
	Patient *p2 = new Patient("Hong");
	Patient *p3 = new Patient("choi");
	
	Doctor *d1 = new Doctor("Mr.oh");
	Doctor *d2 = new Doctor("Ms.Chu");
	
	p1->addDoctor(d1);
	d1->addPatient(p1);
	
	p2->addDoctor(d2);
	d2->addPatient(p2);
	
	p2->addDoctor(d1);
	d1->addPatient(p2);
	
	p1->meetDoctors();
	p2->meetDoctors();
	d1->meetPatients();
}

// Meet doctor : Mr.oh
// Meet doctor : Ms.Chu
// Meet doctor : Mr.oh
// Meet Patient : Jack Jack
// Meet Patient : Hong

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함