3 minute read

10-1 [두 가지 방법의 연산자 오버로딩]

  • 코드💻
    • 1,2,3번을 모두 합쳤음
      /*	
      *	문제 1.
      * 
      *	아래 조건을 만족하는 -연산자 오버로딩을 해보고자 한다
      * 
      *	1. 전역함수 기반으로 오버로딩
      *	2. 멤버 별 -연산의 결과를 담은 Point 객체 반환
      */
      /*
      *	문제 2.
      *
      *	아래 조건을 만족하는 +=, -=연산자 오버로딩을 해보고자 한다
      *
      *	1. 멤버함수 기반으로 오버로딩
      *	2. += 결과로 pos1의 멤버변수 값이 pos2의 멤버 변수 값만큼 멤버 별 증가
      *	3. -= 결과로 pos1의 멤버변수 값이 pos2의 멤버 변수 값만큼 멤버 별 감소
      *	4. 연산 결과로 값이 증감한 pos1의 객체를 반환하도록, 되도록이면 참조형으로 반환
      */
      /*
      *	문제 3.
      *
      *	아래 조건을 만족하는 !=, ==연산자 오버로딩을 해보고자 한다
      *
      *	1. 전역함수 기반으로 오버로딩
      *	2. == 결과로 pos1의 모든 멤버의 값이 같다면 true, 아니면 false
      *	3. != 결과로 pos1의 모든 멤버의 값이 같다면 false, 아니면 true
      *	4. == 를 먼저 오버로딩 한 다음에, 이를 이용하는 형태로 != 연산자 오버로딩
      */
    
      #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);
          friend Point operator-(const Point& pos1, const Point& pos2);	
    
          Point& operator+=(const Point& pos1);
          Point& operator-=(const Point& pos1);
    
          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 (operator==(pos1, pos2)) {
              return false;
          }
          return true;
      }
    
      Point& Point::operator+=(const Point& pos1) {
          xpos += pos1.xpos;
          ypos += pos1.ypos;
          return *this;
      }
    
      Point& Point::operator-=(const Point& pos1) {
          xpos -= pos1.xpos;
          ypos -= pos1.ypos;
          return *this;
      }
    
      Point operator+(const Point& pos1, const Point& pos2)
      {
          Point pos(pos1.xpos + pos2.xpos, pos1.ypos + pos2.ypos);
          return pos;
      }
    
      Point operator-(const Point& pos1, const Point& pos2) {
          Point pos(pos1.xpos - pos2.xpos, pos1.ypos - pos2.ypos);
          return pos;
      }
    

10-2 [단항 연산자 오버로딩]

  • 코드💻
/*
*	문제 1.
*
*	멤버함수 형태로 단항 연산자 ( - ) 오버로딩
*
*	int num2 = -num1;
*	num2에는 num1과 절대값은 같지만 부호가 다른 값이 저장된다. num1의 값에는 영향을 미치지 않는다. 
*	Point pos2 = -pos1;이 실행되면 pos2의 멤버변수는 pos1의 멤버변수 값과 다른 부호의 값으로 초기화되도록 오버로딩 해보자
*/
/*
*	문제 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 ShowPosition() const
	{
		cout << '[' << xpos << ", " << ypos << ']' << endl;
	}
	
	Point& operator++()
	{
		xpos += 1;
		ypos += 1;
		return *this;
	}
	
	Point operator-() {
		Point pos(-xpos, -ypos);
		return pos;
	}

	friend Point& operator--(Point& ref);
	friend Point operator~(const Point& ref);
};

Point& operator--(Point& ref)
{
	ref.xpos -= 1;
	ref.ypos -= 1;
	return ref;
}

Point operator~(const Point& ref) {
	Point pos(ref.ypos, ref.xpos);
	return ref;
}

10-3 [입력을 위한 » 연산자 오버로딩]

  • 코드💻
/*
*	문제 1.
*	main 함수에 보이는 대로 데이터 입력이 가능하도록, >> 연산자를 오버로딩 하고자한다
*/

#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 ostream& operator<<(ostream&, const Point&);
	friend istream& operator>>(istream&, const Point&);
};


ostream& operator<<(ostream& os, const Point& pos)
{
	os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
	return os;
}

istream& operator>>(istream& is, const Point& pos) {
	is >> pos.xpos >> pos.ypos;
	return is;
}

int main(void)
{
	Point pos1;
	cout << "x, y 좌표순으로 입력: ";
	cin >> pos1;
	cout << pos1;

	Point pos2;
	cout << "x, y 좌표순으로 입력: ";
	cin >> pos2;
	cout << pos2; 

	return 0;
}

Tags: ,

Categories:

Updated: