C++ Chapter 10.2 : 구성 관계

Date:     Updated:

카테고리:

태그:

인프런에 있는 홍정모 교수님의 홍정모의 따라 하며 배우는 C++ 강의를 듣고 정리한 필기입니다. 😀
🌜 [홍정모의 따라 하며 배우는 C++]강의 들으러 가기!


chapter 10. 객체들 사이의 관계 : 구성 관계

관계 종류 관계를 표현하는 동사 관계 형태 다른 클래스에 속할 수 있는가? 멤버의 존재를
클래스가 관리 하는가?
방향성
구성 Part-of 전체/부품 No Yes 단방향

🔔 구조 설명

#include "position2d.h"
class Monster
{
private:
	std::string m_name;
	
	Position2D m_location;   // ⭐구성관계⭐ 

...

Monster goblin;  // Monstre 객체

Part-of*

Position2D타입의 객체 m_location은 Monster 객체를 구성하는 부품이다.

  • Position2D 타입의 객체 m_locationMonster 클래스의 Part, 부품이다.
    • m_locationMonstre의 다른 멤버에 대해 알 필요가 없다. 단지 부품일 뿐!
  • 다른 클래스에 속할 수 있는가? : No
    • Position2D 타입의 객체 m_locationMoster 클래스의 위치 정보가 되므로 다른 클래스에 속할 수 없다.
      • m_location 멤버는 Monster 객체의 일부분으로서 오직 Monster클래스의 위치 정보만 담을 수 있다.
        • Position2D 클래스 자체는 여러 클래스에 속할 수 있다.
          • 플레이어나 NPC 의 Part로서 그들의 위치 정보를 알려주는 멤버 객체를 찍어낼 수 있는 클래스다.
        • 그러나 플레이어 클래스의 m_location 객체 멤버와 몬스터 클래스의 m_location 객체 멤버는 서로 별개의 다른 존재다.
          • 몬스터의 위치가 플레이어 멤버로 속할 일은 없듯이.
        • 서로 다른 클래스들은 그들 각자가 가지고 있는 Position2D 타입의 객체 멤버를 각자 관리한다.
  • 멤버의 존재를 클래스가 관리 하는가? : Yes
    • Moster타입의 객체가 사라지면 그의 멤버인 Position2D 타입의 객체 m_location 도 사라진다.
    • Moster타입의 객체 없이는 m_location 도 없다.
    • 단방향임을 알 수 있다.


🔔 코드

📜 Position2D.h

#include <iostream>

class position2d
{
private:
	int m_x;
	int m_y;

public:
	position2d(const int & x_in, const int & y_in)
		: m_x(x_in), m_y(y_in)
	{}

	void set(const position2d & pos_target)
	{
		set(pos_target.m_x, pos_target.m_y);
	}

	void set(const int & x_target, const int & y_target)
	{
		m_x = x_target;
		m_y = y_target;
	}

	friend std::ostream & operator << (std::ostream & out, const position2d & pos2d)
	{
		out << pos2d.m_x << " " << pos2d.m_y 
		return out;
	}
};
  • Position2D객체를 출력할 때 호출할 출력 연산자 <<을 오버로딩을 해주어야 하므로 #include <iostream> 해준다.
  • set(const position2d & pos_target)
    • 같은 Position2D타입의 다른 객체를 인수로 받았을 때 그 객체의 멤버 값들을 내 멤버 값으로 복사한다.
    • 동일한 이름의 set(const int & x_target, const int & y_target) 호출
    • 복사생성자 같은 역할


📜 Monster.h

#pragma once

#include <string>
#include "position2d.h"
class Monster
{
private:
	std::string m_name;
	
	position2d m_location;


public: 
	Monster(const std::string name_in, const position2d & pos_in)
		: m_name(name_in), m_location(pos_in)
	{}

	void moveTo(const position2d & pos_target)
	{
		m_location.set(pos_target);
	}

	friend std::ostream & operator << (std::ostream & out, const Monster & monster)
	{
		out << monster.m_name << " " << monster.m_location 
		return out;
	}
};
position2d m_location;
  • 몬스터의 위치 정보를 나타낼 Position2D타입의 객체를 멤버로 하기 위해 #include “position2d.h” 해준다.
    • <iostream>은 이미 📜position2d.h 에 포함되어 있기 때문에 또 include 해줄 필요 없다.
  • void moveTo(const position2d & pos_target)
    • 이동할 위치롤 인수로 받아 몬스터 객체(자기 자신)를 그 위치로 이동시킨다.
  • 출력 연산자에서
    • out << monster.m_name « ” “ << monster.m_location
    • 인라인 코드블록으로 체크해준 << 부분에서 Position2D 클래스의 출력 연산자 오버로딩이 호출된다.
      • 재활용


📜 main.cpp

#include "Monster.h"

using namespace std;

int main()
{
	Monster mon1("Simpson", position2d(0, 0));

	cout << mon1 << endl; 

	{
		mon1.moveTo(position2d(1,1));
		cout << mon1 << endl; 
	}

	return 0; 
}
  • 몬스터 객체를 만들기 위해 #include “Monster.h” 해준다.
    • <iostream>은 이미 📜position2d.h 을 including 하는 📜Monster.h 에 포함되어 있기 때문에 또 include 해줄 필요 없다.


🌜 개인 공부 기록용 블로그입니다. 오류나 틀린 부분이 있을 경우 
언제든지 댓글 혹은 메일로 지적해주시면 감사하겠습니다! 😄

맨 위로 이동하기


Cpp 카테고리 내 다른 글 보러가기

댓글 남기기