250x250
Notice
Recent Posts
«   2024/07   »
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
관리 메뉴

일상 코딩

[C++/10.03] Aggregation 집합 관계 본문

C++/따배C++ 10강 객체들 사이의 관계

[C++/10.03] Aggregation 집합 관계

polarcompass 2021. 11. 2. 02:37
728x90

1. main

#include <iostream>
#include <vector>
#include "Lecture.h"

int main()
{
    using namespace std;
    std::string lec_title1("Introduction to Computer Programming");
    std::string lec_title2("Computational Thinking");

    Student *std1 = new Student("Jack Jack", 0);
    Student *std2 = new Student("Dash", 1);
    Student *std3 = new Student("Violet", 2);

    Teacher *teacher1 = new Teacher("Prof. Hong");
    Teacher *teacher2 = new Teacher("Prof. Good");

    //Composition Relationship
    Lecture lec1(lec_title1);
    lec1.assignTeacher(teacher1);
    lec1.registerStudent(std1);
    lec1.registerStudent(std2);
    lec1.registerStudent(std3);

    Lecture lec2(lec_title2);
    lec2.assignTeacher(teacher2);
    lec2.registerStudent(std1);

    //test
    {
        cout << lec1 << endl;
        cout << lec2 << endl;

        // event
        lec2.study();

        cout << lec1 << endl;
        cout << lec2 << endl;
    }

    // TODO : class HobbyClub
    
    // delete memory (if necessary)
    delete std1;
    delete std2;
    delete std3;

    delete teacher1;
    delete teacher2;

    return 0;
}

2. Lecture.h

#pragma once

#include <vector>
#include "Student.h"
#include "Teacher.h"

class Lecture
{
private:
    std::string m_name;

    Teacher *teacher;
    std::vector<Student *> students;

public:
    Lecture(const std::string &name_in)
        : m_name(name_in)
    {
    }

    ~Lecture()
    {
        // do Not delete teacher
        // do Not delete students
    }

    void assignTeacher(Teacher *const teacher_input)
    {
        teacher = teacher_input;
    }

    void registerStudent(Student *const student_input)
    {
        students.push_back(student_input);

        // &student_input != & student[0]
    }

    void study()
    {
        std::cout << m_name << " Study" << std::endl << std::endl;

        for (auto &element : students) //Note : 'auto element' works
            (*element).setIntel((*element).getIntel() + 1);
        //element->setIntel(element->getIntel() + 1);
    }

    friend std::ostream &operator<<(std::ostream &out, const Lecture &lecture)
    {
        out << "Lecture name : " << lecture.m_name << std::endl;

        out << *lecture.teacher << std::endl;

        for (auto &element : lecture.students)
            out << *element << std::endl;

        return out;
    }
};

3. Teacher.h

#pragma once

#include <string>

class Teacher
{
private:
    std::string m_name;
    // TODO : more members like home address, salary, age, evaluation, etd...

public:
    Teacher(const std::string &name_in = "No Name")
        : m_name(name_in)
    {
    }

    void setName(const std::string &name_in)
    {
        m_name = name_in;
    }

    std::string getName()
    {
        return m_name;
    }

    friend std::ostream &operator<<(std::ostream &out, const Teacher &teacher)
    {
        out << teacher.m_name;
        return out;
    }
};

4. Student.h

#pragma once

#include <iostream>
#include <string>

class Student
{
private:
    std::string m_name;
    int m_intel; // intelligence; 지능

    // TODO : add more members like address, phone, favorate food, habits,...
public:
    Student(const std::string &name_in = "No Name", const int &intel_in = 0)
        : m_name(name_in), m_intel(intel_in)
    {
    }

    void setName(const std::string &name_in)
    {
        m_name = name_in;
    }

    void setIntel(const int &intel_in)
    {
        m_intel = intel_in;
    }

    int getIntel()
    {
        return m_intel;
    }

    friend std::ostream &operator<<(std::ostream &out, const Student &student)
    {
        out << student.m_name << " " << student.m_intel;
        return out;
    }
};
728x90