미누에요
[C++] 윤성우의 열혈 C++ 10-1 두 가지 방법의 연산자 오버로딩 문제풀이 본문
728x90
반응형
SMALL
문제 1
Point 클래스에 대해서 다음 조건을 만족하는 형태로 - 연산자를 오버로딩 해보자.
- 전역함수 기반으로 오버로딩
- 멤버 별 - 연산의 결과를 담은 Point 객체 반환
정답
소스 코드
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0) : xpos(x), ypos(y) {}
void ShowPosition() const
{
cout << "[" << xpos << ", " << ypos << "]" << endl;
}
friend Point operator-(const Point &pos1, const Point &pos2);
};
Point operator-(const Point &pos1, const Point &pos2)
{
Point pos(pos1.xpos - pos2.xpos, pos1.ypos - pos2.ypos);
return pos;
}
int main(void)
{
Point pos1(10, 24);
Point pos2(3, 8);
Point pos3 = pos1 - pos2;
pos1.ShowPosition();
pos2.ShowPosition();
pos3.ShowPosition();
}
실행 결과
문제 2
Point 클래스에 대해서 다음 조건을 만족하는 형태로 += 연산자와 -= 연산자를 오버로딩 해보자.
- 멤버함수 기반으로 오버로딩
- 연산 'pos1 += pos2'의 결과로 pos1의 멤버변수 값이 pos2의 멤버변수 값만큼 멤버 별 증가
- 연산 'pos1 -= pos2'의 결과로 pos1의 멤버변수 값이 pos2의 멤버변수 값만큼 멤버 별 감소
- 연산의 결과로 값이 증가 및 감소한 pos1의 객체를 반환하도록 (이왕이면 참조형으로 반환하도록) 연산자 오버로딩
정답
소스 코드
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0) : xpos(x), ypos(y) {}
void ShowPosition() const
{
cout << "[" << xpos << ", " << ypos << "]" << endl;
}
Point operator+=(const Point &pos2)
{
Point pos(this->xpos + pos2.xpos, this->ypos + pos2.ypos);
return pos;
}
Point operator-=(const Point &pos2)
{
Point pos(this->xpos - pos2.xpos, this->ypos - pos2.ypos);
return pos;
}
};
int main(void)
{
Point pos1(3, 4);
Point pos2(10, 20);
Point pos3 = pos1 += pos2;
Point pos4 = pos2 -= pos1;
pos1.ShowPosition();
pos2.ShowPosition();
pos3.ShowPosition();
pos4.ShowPosition();
}
실행 결과
문제 3
Point 클래스에 대해서 다음 조건을 만족하는 형태로 == 연산자와 != 연산자를 오버로딩 해보자.
- 둘 다 전역함수의 형태로 오버로딩
- 연산 'pos1==pos2'의 결과로 모든 멤버의 값이 같다면 true, 그렇지 않다면 false 반환
- 연산 'pos1!=pos2'의 결과로 모든 멤버의 값이 같다면 false, 그렇지 않다면 true 반환
- 연산자 ==를 먼저 오버로딩한 다음에, 이를 이용하는 형태로 != 연산자를 오버로딩
정답
소스코드
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0) : xpos(x), ypos(y) {}
void ShowPosition() const
{
cout << "[" << xpos << ", " << ypos << "]" << endl;
}
friend bool operator==(const Point &pos1, const Point &pos2);
friend bool operator!=(const Point &pos1, const Point &pos2);
};
bool operator==(const Point &pos1, const Point &pos2)
{
if ((pos1.xpos == pos2.xpos) && (pos1.ypos == pos2.ypos))
{
return true;
}
else
{
return false;
}
};
bool operator!=(const Point &pos1, const Point &pos2)
{
return !(operator==(pos1, pos2));
}
int main(void)
{
Point pos1(3, 5);
Point pos2(2, 4);
Point pos3(2, 4);
if (pos1 == pos2)
{
cout << "pos1과 pos2는 같은 점입니다." << endl;
}
if (pos2 == pos3)
{
cout << "pos2와 pos3는 같은 점입니다." << endl;
}
}
결과
728x90
반응형
LIST
'C++' 카테고리의 다른 글
[C++] 윤성우의 열혈 C++ 10-3 입력을 위한 >> 연산자의 오버로딩 문제풀이 (0) | 2024.05.12 |
---|---|
[C++] 윤성우의 열혈 C++ 10-2 단항 연산자 오버로딩 문제풀이 (0) | 2024.05.12 |
[C++] 윤성우의 열혈 C++ 08-1 상속 관계의 확장과 추상 클래스 문제풀이 (0) | 2024.05.03 |
[C++] 윤성우의 열혈 C++ 프로그래밍 ch 07-2 : IS-A 관계의 상속 (0) | 2024.04.14 |
[C++] 윤성우의 열혈 C++ 프로그래밍 ch 07-1 : 상속과 생성자의 호출 문제풀이 (0) | 2024.04.14 |