연관(Association)
1. aggregation: 소유, 집합.
ownership. 클래스가 독립적이다.
person - date
date는 다른 클래스에서 사용해도 되는 독립적인 객체이다.
date 클래스 객체를 생성해서 person 에서 사용한다.
#ifndef PERSON_H
#define PERSON_H
#include "date.h"
// Person 클래스의 정의
class Person
{
private:
long identity;
Date birthDate; // 멤버변수가 객체 형태로
public:
Person(long identity, Date birthDate); // 생성자의 변수로도 사용
~Person();
void print() const;
};
#endif
#include "person.h"
int main()
{
// 인스턴스화
Date date1(5, 6, 1980);
Person person1(111111456, date1); //date 클래스 객체 생성. person 에서 사용
Date date2(4, 23, 1978);
Person person2(345332446, date2);
// 출력
person1.print();
person2.print();
return 0;
}
2. composition
구성. aggregation보다 강한 집합관계. 몸-장기
별도의 name 클래스를 생성하지 않고, employee 안에다가 name을 만들어 놓음.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "name.h"
class Employee
{
private:
Name name;
double salary;
public:
Employee(string first, string init, string last, double salary);
~Employee();
void print() const;
};
#endif
#include "employee.h"
int main()
{
// 인스턴스화
Employee employee1("Mary", "B", "White", 22120.00);
Employee employee2("William", "S", "Black", 46700.00);
Employee employee3("Ryan", "A", "Brown", 12500.00);
// 출력
employee1.print();
employee2.print();
employee3.print();
return 0;
}
'언어 공부 > C++' 카테고리의 다른 글
다형성(Polymorphism) (0) | 2022.06.27 |
---|---|
클래스 간의 관계: 의존 (0) | 2022.06.26 |
클래스 간의 관계: 상속 - 3(person-student-employee) (0) | 2022.06.26 |
클래스 간의 관계: 상속 - 2(person-student) (0) | 2022.06.23 |
클래스 간의 관계: 상속 - 1 (0) | 2022.06.22 |
댓글