Chapter10 연산자 오버로딩
문제 1번 풀이
#include <iostream>
using namespace std;
class Point {
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0)
:xpos(x), ypos(y)
{
}
void ShowPoint() const {
cout << "[" << xpos << "," << ypos << "]" << endl;
}
Point operator+(const Point& pos2) { // Point 객체를 반환하는 멤버 함수 'operator +'e
Point pos(xpos + pos2.xpos, ypos + pos2.ypos);
return pos;
}
};
int main()
{
Point pos1(3, 4);
Point pos2(2, 5);
Point pos3 = pos1 + pos2;
pos3.ShowPoint();
return 0;
}
문제 2번 풀이
#include <iostream>
using namespace std;
class Point {
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0)
:xpos(x), ypos(y)
{
}
void ShowPoint() const {
cout << "[" << xpos << "," << ypos << "]" << endl;
}
Point operator+=(const Point& pos2) { // Point 객체를 반환하는 멤버 함수 'operator +'e
xpos += pos2.xpos;
ypos += pos2.ypos;
return *this;
}
Point operator -= (const Point & pos2)
{
xpos -= pos2.xpos;
ypos -= pos2.ypos;
return *this;
}
};
int main()
{
Point pos1(3, 4);
Point pos2(2, 5);
Point pos3(10, 2);
pos1 += pos2;
pos1.ShowPoint();
pos3 -= pos2;
pos3.ShowPoint();
return 0;
}
- 연산의 결과로 값이 증가 및 감소한 pos1의 객체를 반환하도로 연산자 오버로딩을 하라.
- this 포인터를 사용해 현재 클래스 내에서 위치하고 있는 객체를 역참조해 참조형으로 반환하였다.
문제 3번 풀이
#include <iostream>
using namespace std;
class Point {
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0)
:xpos(x), ypos(y)
{
}
void ShowPoint() 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) {
if (pos1 == pos2)
{
return false;
}
else
{
return true;
}
}
int main()
{
Point pos1(3, 4);
Point pos2(2, 5);
Point pos3(3, 4);
bool result1 = pos1 == pos2;
bool result2 = pos1 == pos3;
cout << result1 << " " << result2 << endl;
return 0;
}
출력 : 0 1
#include <iostream>
using namespace std;
class Point {
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0)
:xpos(x), ypos(y)
{
}
void ShowPoint() const {
cout << "[" << xpos << "," << ypos << "]" << endl;
}
Point& operator++() {
xpos += 1;
ypos += 1;
return *this;
}
friend Point& operator --(Point& ref);
Point& operator-() {
xpos = -xpos;
ypos = -ypos;
return *this;
}
Point& operator~() {
int temp = xpos;
xpos = ypos;
ypos = temp;
return *this;
}
};
Point& operator--(Point& ref)
{
ref.xpos -= 1;
ref.ypos -= 1;
return ref;
}
int main()
{
Point pos1(3, 4);
Point pos2 = -pos1;
Point pos3 = ~pos1;
pos2.ShowPoint();
pos3.ShowPoint();
return 0;
}
temp라는 키워드를 이용해 xpos와 ypos의 값을 바꾸는 것을 구현하였다.
#include <iostream>
using namespace std;
class Point {
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0)
:xpos(x), ypos(y)
{
}
void ShowPoint() const {
cout << "[" << xpos << "," << ypos << "]" << endl;
}
Point& operator++() {
xpos += 1;
ypos += 1;
return *this;
}
friend Point& operator --(Point& ref);
Point& operator-() {
xpos = -xpos;
ypos = -ypos;
return *this;
}
friend Point& operator~(Point& ref);
};
Point& operator~(Point &ref) {
int temp = ref.xpos;
ref.xpos = ref.ypos;
ref.ypos = temp;
return ref;
}
Point& operator--(Point& ref)
{
ref.xpos -= 1;
ref.ypos -= 1;
return ref;
}
int main()
{
Point pos1(3, 4);
Point pos2 = -pos1;
Point pos3 = ~pos1;
pos2.ShowPoint();
pos3.ShowPoint();
return 0;
}
~ 연산자를 멤버함수가 아닌 전역함수로 바꾼 코드
10-4 cout, cin, endl에 대하여
namespace mystd { //cout과 endl을 직접 구현한 namespace
using namespace std;
class ostream {
public:
void operator << (char* str)
{
printf("%s", str);
}
void operator << (char str)
{
printf("%c", str);
}
void operator << (int num)
{
printf("%d", num);
}
void operator << (ostream&(*fp)(ostream& ostm))
{
fp(*this);
}
};
ostream& endl(ostream& ostm)
{
ostm << '\n';
fflush(stdout); // 버퍼를 비우는 작업
return ostm;
}
}
int main()
{
using mystd::cout;
using mystd::endl;
cout << "Simpe STring";
cout << endl;
cout << 3.14;
}
마지막 세 cout 문은 다음과 같이 해석 된다.
cout.operator <<("Simple Stirng");
cout.operator << (endl);
cout.operator << (3.14);
하나의 cout으로 여러개의 출력을 내고싶으면 << 연산자가 cout 객체를 다시 반환해 여러번 사용될 수 있게 하면 된다.
ostream& operator << (char* str)
{
printf("%s", str);
return *this;
}
이렇게!
#include <iostream>
using namespace std;
namespace mystd { //cout과 endl을 직접 구현한 namespace
using namespace std;
class ostream {
public:
ostream& operator << (char* str)
{
printf("%s", str);
return *this;
}
void operator << (char str)
{
printf("%c", str);
}
void operator << (int num)
{
printf("%d", num);
}
void operator << (ostream& (*fp)(ostream& ostm))
{
fp(*this);
}
};
ostream& endl(ostream& ostm)
{
ostm << '\n';
fflush(stdout); // 버퍼를 비우는 작업
return ostm;
}
}
class Point {
private:
int xpos, ypos;
public:
Point(int x = 0, int y = 0)
:xpos(x), ypos(y)
{
}
void ShowPoint() const {
cout << "[" << xpos << "," << ypos << "]" << endl;
}
Point& operator++() {
xpos += 1;
ypos += 1;
return *this;
}
friend Point& operator --(Point& ref);
Point& operator-() {
xpos = -xpos;
ypos = -ypos;
return *this;
}
friend Point& operator~(Point& ref);
friend istream& operator >> (istream &, Point &pos);
};
istream& operator >>(istream& is, Point& pos)
{
is >> pos.xpos >> pos.ypos;
return is;
}
Point& operator~(Point &ref) {
int temp = ref.xpos;
ref.xpos = ref.ypos;
ref.ypos = temp;
return ref;
}
Point& operator--(Point& ref)
{
ref.xpos -= 1;
ref.ypos -= 1;
return ref;
}
int main()
{
using mystd::cout;
using mystd::endl;
cout << "Simpe STring";
cout << endl;
cout << 3.14;
}
istream 객체를 반환하는 >> 연산자 함수를 만들어서 Point객체에 대한 멤버변수의 입력을 받을 수 있도록 하였다.
'Langauge > C++' 카테고리의 다른 글
[C++] 비트마스크 (bitmask) 의 구현, 다양한 예제 (0) | 2023.06.19 |
---|---|
[열혈 C++] 연산자 오버로딩2 정리 및 예제 풀이 (0) | 2021.05.21 |
Constructor and Separating Files (0) | 2021.03.18 |
Introduction to classes and objects #1 (0) | 2021.03.10 |
[열혈 C++ 프로그래밍] Chapter 07(상속의 이해) (0) | 2021.03.01 |