카테고리 없음
2021. 3. 19. 19:40
집합 관계 (Aggregation Relationship)
- 교사 클래스와 학생 클래스로 비유
구성 관계로 작성했을 경우
각
Lecture
의Student
멤버는 서로 다른 메모리 공간을 차지하여 공유되지 않는다.main.cpp
#include "Lecture.h" int main() { using namespace std; Teacher t1("Prof. Hong"); Teacher t2("Prof. Good"); Student s1("Jack Jack", 0); Student s2("Dash", 1); Student s3("Violet", 2); Lecture lec1("Introduction to Computer Programming"); lec1.assignTeacher(t1); lec1.registerStudent(s1); lec1.registerStudent(s2); lec1.registerStudent(s3); Lecture lec2("Computational Thinking"); lec2.assignTeacher(t2); lec2.registerStudent(s1); cout << lec1 << endl; cout << lec2 << endl; lec2.study(); cout << lec1 << endl; cout << lec2 << endl; } /* stdout --- Introduction to Computer Programming --- Teacher Prof. Hong Students Jack Jack 0 Dash 1 Violet 2 --- Computational Thinking --- Teacher Prof. Good Students Jack Jack 0 << Computational Thinking studied! >> --- Introduction to Computer Programming --- Teacher Prof. Hong Students Jack Jack 0 Dash 1 Violet 2 --- Computational Thinking --- Teacher Prof. Good Students Jack Jack 1 */
Lecture.h
#pragma once #include <vector> #include "Student.h"; #include "Teacher.h"; class Lecture { std::string name_; Teacher teacher_; std::vector<Student> students_; public: Lecture(const std::string& name_in) : name_{name_in} {} void assignTeacher(const Teacher& const teacher_input) { teacher_ = teacher_input; } void registerStudent(const Student& const student_input) { students_.push_back(student_input); } void study() { for (auto& e : students_) e.setIntel(e.getIntel() + 1); std::cout << "<< " << name_ << " studied! >>\n\n"; } friend std::ostream& operator << (std::ostream& out, const Lecture& lecture) { out << " --- "<< lecture.name_ << " ---\n"; out << "Teacher\n\t\t" << lecture.teacher_ << '\n'; out << "Students\n"; for (auto& e : lecture.students_) out << "\t\t" << e << '\n'; return out; } };
Teacher.h
#pragma once #include <iostream> class Teacher { std::string name_; public: Teacher(const std::string& name_in = "No Name") : name_{name_in} {} void setName(const std::string& name_in) { name_ = name_in; } std::string getName() { return name_; } friend std::ostream& operator << (std::ostream& out, const Teacher& teacher) { out << teacher.name_; return out; } };
Student.h
#pragma once #include <iostream> class Student { std::string name_; int intel_; public: Student(const std::string & name_in = "No Name", const int &intel_in = 0) : name_{name_in}, intel_{intel_in} {} void setName(const std::string& name_in) { name_ = name_in; } void setIntel(const int& intel_in) { intel_ = intel_in; } int getIntel() { return intel_; } friend std::ostream& operator << (std::ostream& out, const Student& student) { out << student.name_ << ' ' << student.intel_; return out; } };
집합 관계로 작성했을 경우
포인터를 이용하여
main
함수의 인스턴스에 접근하는 것이므로 같은 메모리 공간을 가리킨다.main.cpp
#include "Lecture.h" int main() { using namespace std; Teacher t1("Prof. Hong"); Teacher t2("Prof. Good"); Student s1("Jack Jack", 0); Student s2("Dash", 1); Student s3("Violet", 2); Lecture lec1("Introduction to Computer Programming"); lec1.assignTeacher(&t1); lec1.registerStudent(&s1); lec1.registerStudent(&s2); lec1.registerStudent(&s3); Lecture lec2("Computational Thinking"); lec2.assignTeacher(&t2); lec2.registerStudent(&s1); cout << lec1 << endl; cout << lec2 << endl; lec2.study(); cout << lec1 << endl; cout << lec2 << endl; } /* stdout --- Introduction to Computer Programming --- Teacher Prof. Hong Students Jack Jack 0 Dash 1 Violet 2 --- Computational Thinking --- Teacher Prof. Good Students Jack Jack 0 << Computational Thinking studied! >> --- Introduction to Computer Programming --- Teacher Prof. Hong Students Jack Jack 1 Dash 1 Violet 2 --- Computational Thinking --- Teacher Prof. Good Students Jack Jack 1 */
Lecture.h
#pragma once #include <vector> #include "Student.h"; #include "Teacher.h"; class Lecture { std::string name_; Teacher *teacher_; std::vector<Student *> students_; public: Lecture(const std::string& name_in) : name_{name_in} {} void assignTeacher(Teacher * const teacher_input) { teacher_ = teacher_input; } void registerStudent(Student * const student_input) { students_.push_back(student_input); } void study() { for (auto& e : students_) e->setIntel(e->getIntel() + 1); std::cout << "<< " << name_ << " studied! >>\n\n"; } friend std::ostream& operator << (std::ostream& out, const Lecture& lecture) { out << " --- "<< lecture.name_ << " ---\n"; out << "Teacher\n\t\t" << *lecture.teacher_ << '\n'; out << "Students\n"; for (auto& e : lecture.students_) out << "\t\t" << *e << '\n'; return out; } };