Minwoo Dev.

[C++] 윤성우의 열혈 C++ ch 10-1 두 가지 방법의 연산자 오버로딩 문제풀이 본문

C++

[C++] 윤성우의 열혈 C++ ch 10-1 두 가지 방법의 연산자 오버로딩 문제풀이

itisminu 2024. 7. 18. 11:17
728x90
반응형
SMALL

문제 1

Point 클래스에 대해서 다음 조건을 만족하는 형태로 - 연산자를 오버로딩 해보자.

  • 전역함수 기반으로 오버로딩
  • 멤버 별 - 연산의 결과를 담은 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(7, 9);
    Point pos2(2, 5);
    Point pos3 = pos1 - pos2;
    pos3.ShowPosition();
    return 0;
}

 

실행 결과

 

 


 

문제 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 & pos){
        xpos += pos.xpos;
        ypos += pos.ypos;
        return *this;
    }
    Point operator-=(const Point &pos){
        xpos -= pos.xpos;
        ypos -= pos.ypos;
        return *this;
    }
};

int main(void){
    Point pos1(3, 6);
    Point pos2(1, 2);
    cout << "---- Before += ----" << endl;
    pos1.ShowPosition();
    cout << "---- After += ----" << endl;
    pos1 += pos2;
    pos1.ShowPosition();
    cout << "---- After -= ----" << endl;
    pos1 -= pos2;
    pos1.ShowPosition();
    return 0;
}

 

 

실행 결과

 

 


 

문제 3

Point 클래스에 대해서 다음 조건을 만족하는 형태로 == 연산자와 != 연산자를 오버로딩해보자.

  • 둘 다 전역함수의 형태로 오버로딩
  • 연산 'pos1 == pos2' 의 결과로 모든 멤버의 값이 같다면 true, 그렇지 않다면 false 반환
  • 연산 'pos1 != pos2'의 결과로 모든 멤버의 값이 같다면 false, 그렇지 않다면 true 반환
  • 연산자 == 를 먼저 오버로딩한 다음에, 이를 이용하는 형태로 != 연산자를 오버로딩

 

정답

// 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 << ']';
    }
    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;
    return false;
}

bool operator!=(const Point &pos1, const Point &pos2){
    if(pos1.xpos==pos2.xpos && pos1.ypos==pos2.ypos)
        return false;
    return true;
}


int main(void){
    Point pos1(3, 4);
    Point pos2(4, 8);
    Point pos3(3, 4);
    cout << "pos1==pos2 : " << (pos1 == pos2) << endl;
    cout << "pos2!=pos2 : " << (pos2 != pos3) << endl;
    cout << "pos1==pos3 : " << (pos1 == pos3) << endl;
    return 0;
}

 

 

실행 결과

728x90
반응형
LIST