# 객체간의 상호작용(message passing)

객체와 객체간의 서로 필요한 기능을 실행하기 위해 상대 객체의 기능을 실행하도록 요구하는 것을 메세지를 보낸다고 한다.

이번에는 과일장수 클래스와 구매자 클래스를 구현해보고 클래스간의 상호작용을 해보도록하겠습니다.

## 과일장수 클래스 <a href="#ea-b3-bc-ec-9d-bc-ec-9e-a5-ec-88-98-ed-81-b4-eb-9e-98-ec-8a-a4" id="ea-b3-bc-ec-9d-bc-ec-9e-a5-ec-88-98-ed-81-b4-eb-9e-98-ec-8a-a4"></a>

**과일장수 클래스의 속성**

| 속성               | 설명     |
| ---------------- | ------ |
| int APPLE\_PRICE | 사과의 가격 |
| int numOfApple   | 사과 개수  |
| int myMoney      | 돈      |

**과일장수 클래스의 메서드**

| 메서드           | 설명                 |
| ------------- | ------------------ |
| initMembers() | 멤버 변수를 초기값을 설정합니다. |
| saleApples()  | 사과를 판매합니다.         |
| showStatus()  | 사과의 개수와 금액을 출력합니다. |

{% code title="Seller.h" %}

```cpp
#ifndef __SELLER_H__
#define __SELLER_H__

class Seller
{
  int APPLE_PRICE;
  int numOfApple;
  int myMoney;
public:
  void initMembers(int price, int num);
  int saleApples(int money);
  void showStatus();
};

#endif
```

{% endcode %}

{% code title="Seller.cpp" %}

```cpp
#include <iostream>
#include "Seller.h"
using namespace std;

void Seller::initMembers(int price, int num) {
  APPLE_PRICE = price;
  numOfApple = num;
  myMoney = 0;
}

int Seller::saleApples(int money) {
  int num = money / APPLE_PRICE;
  numOfApple -= num;
  myMoney += money;
  return num;
}

void Seller::showStatus() {
  cout << "남은 사과 : " << numOfApple << endl;
  cout << "판매 수익 : " << myMoney << endl;
}
```

{% endcode %}

## 구매자 클래스 <a href="#ea-b5-ac-eb-a7-a4-ec-9e-90-ed-81-b4-eb-9e-98-ec-8a-a4" id="ea-b5-ac-eb-a7-a4-ec-9e-90-ed-81-b4-eb-9e-98-ec-8a-a4"></a>

**구매자 클래스 속성**

| 속성             | 설명    |
| -------------- | ----- |
| int myMoney    | 돈     |
| int numOfApple | 사과 개수 |

**구매자 클래스 메서드**

| 메서드           | 설명                 |
| ------------- | ------------------ |
| initMembers() | 멤버 변수를 초기값을 설정합니다. |
| buyApples()   | 사과를 구매합니다.         |
| showStatus()  | 사과의 개수와 금액을 출력합니다. |

{% code title="Buyer.h" %}

```cpp
#ifndef __BUYER_H__
#define __BUYER_H__
#include "Seller.h"

class Buyer
{
  int myMoney;
  int numOfApple;
public:
  void initMembers(int money);
  void buyApples(Seller &seller, int money);
  void showStatus();
};

#endif
```

{% endcode %}

{% code title="Buyer.cpp" %}

```cpp
#include <iostream>
#include "Buyer.h"
using namespace std;

void Buyer::initMembers(int money) {
  myMoney = money;
  numOfApple = 0;
}

void Buyer::buyApples(Seller &seller, int money) {
  numOfApple += seller.saleApples(money);
  myMoney -= money;
}

void Buyer::showStatus() {
  cout << "사과 개수 : " << numOfApple << endl;
  cout << "현재 잔액 : " << myMoney << endl;
}
```

{% endcode %}

## 객체간의 상호작용(Message Passing) <a href="#ea-b0-9d-ec-b2-b4-ea-b0-84-ec-9d-98-ec-83-81-ed-98-b8-ec-9e-91-ec-9a-a9message-passing" id="ea-b0-9d-ec-b2-b4-ea-b0-84-ec-9d-98-ec-83-81-ed-98-b8-ec-9e-91-ec-9a-a9message-passing"></a>

객체 생성은 `일반적인 변수 선언 방식`과 `동적할당 방식(포인터)`으로 두가지가 존재 한다.\
`main.cpp` 예제에서 주석으로 설명합니다.

{% code title="main.cpp" %}

```cpp
#include <iostream>
using namespace std;
#include "Seller.h"
#include "Buyer.h"

int main() {
  Seller seller; // 일반적인 변수 선언 방식(스택)
  seller.initMembers(1000, 20); // 일반변수이므로 .로 접근
    
  Buyer* buyer = new Buyer(); // 동적 할당방식(힙)
  buyer->initMembers(20000); // 포인터이므로 ->로 접근

  buyer->buyApples(seller, 5000); // 5천원만큼 사과 구매
  
  cout << "판매자 현황" << endl;
  seller.showStatus();
  cout << endl << "구매자 현황" << endl;
  buyer->showStatus();
  return 0;
}

```

{% endcode %}
