> For the complete documentation index, see [llms.txt](https://ezcode.gitbook.io/bnb-cpp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ezcode.gitbook.io/bnb-cpp/operator-overloading/exchange.md).

# 교환법칙

## 교환법칙 <a href="#ea-b5-90-ed-99-98-eb-b2-95-ec-b9-99" id="ea-b5-90-ed-99-98-eb-b2-95-ec-b9-99"></a>

#### A+B 는 B+A와 결과가 같다. <a href="#ab-eb-8a-94-ba-ec-99-80-ea-b2-b0-ea-b3-bc-ea-b0-80-ea-b0-99-eb-8b-a4" id="ab-eb-8a-94-ba-ec-99-80-ea-b2-b0-ea-b3-bc-ea-b0-80-ea-b0-99-eb-8b-a4"></a>

곱셈 연산과 덧셈연산은 교환법칙이 성립하는 연산이다.\
이때 두 자료형이 다른경우에는 자료형에 맞는 연산자 다중정의를 해야 한다.

```cpp
#include <iostream>
using namespace std;

class Point {
  int xPos;
  int yPos;
public:
  Point() {}

  Point(int x, int y)
    : xPos(x), yPos(y) {}
  
  void showPosition() const {
    cout << "[" << xPos << ", " << yPos << "]" << endl;
  }

  Point operator*(int num) {
    Point pos(xPos*num, yPos*num);
    return pos;
  }

  friend Point operator*(int num, Point& ref);
};

Point operator*(int num, Point& ref) {
  return ref * num;
}

int main() {
  Point p1(3, 5);
  Point p2 = p1 * 10;
  Point p3 = 5 * p1;

  p1.showPosition();
  p2.showPosition();
  p3.showPosition();
  return 0;
}
```

`Point operator*(int num)` 함수와 `friend Point operator*(int num, Point& ref)` 함수가 교환 법칙을 이루고 있다.

그리고 이러한 형태로 **서로 다른 자료형을 대상으로 한 연산자 오버로딩**도 구현이 가능하다는 것이다.
