연산자 중복

shinyeongwoon·2022년 11월 23일
0

C++

목록 보기
9/10

일상 생활에서의 기호 사용

  • + 기호
    숫자 더하기 2 + 3 , 색혼합 red + blue , 생활 남자 + 여자 결혼
    숫자 or 물체 사용 (중복 사용)
    간결한 의미 전달
    다형성 활용

  • C++ 언어에서도 연산자 중복 가능
    c++ 언어의 연산자에 새로운 의미 정의
    높은 프로그램 가독성

연산자 중복 사례

정수 더하기, 문자열 더하기, 색 섞기, 배열 합치기
ex) 색섞기

Color a(BLUE),b(RED),c;
c = a + b //c=VIOLET

배열 합치기

SortedArray a(2,5,9),b(3,7,10),c;
c = a + b; c={2,3,5,7,9,10}

연잔자 중복의 특징

C++ 본래에 있는 연산자만 중복 가능
피 연산자 타입이 다른 새로운 연산 정의
연산자는 함수 형태로 구현 - 연산자 함수 (Operator function)
반드시 클래스와 관계를 가짐
피연산자의 개수를 바꿀 수 없음
연산의 우선 순위 변경 안됨
모든 연산자가 중복 가능 하지 않음

연산자 함수

연산자 함수를 구현하는 방법 2가지
1. 클래스의 멤버 함수로 구현
2. 외부 함수로 구현하고 클래스에 프렌드 함수로 선언

연산자 함수 형식

리턴타입 aperator 연산자 (매개변수리스트);

+와 == 연산자의 작성 사례

클래스의 멤버 함수로 작성되는 경우

class Color{
	....
    Color operator+ (Color op2);
    bool operator== (Color op2);
}

외부 함수로 구현되고 클래스에 프렌드로 선언되는 경우

Color operator + (Color op1, Color op2);
bool operator == (Color op1, Color op2);

class Color{
	...
    friend Color operator + (Color op1, Color op2);
    friend bool operator == (Color op1, Color op2);
}

이항 연산자 중복 : + 연산자

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  Power operator + (Power op2);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power Power::operator+(Power op2){
  Power tmp;
  tmp.kick = this -> kick + op2.kick;
  tmp.punch = this -> punch + op2.punch;
  return tmp;
}

int main(){
  Power a(3,5), b(4,6),c;
  c = a + b;
  a.show();
  b.show();
  c.show();
}

결과
3 5
4 6
7 11

c = a + b; => 컴파일러에 의한 변환 => c = a.+(b);

a. => a의 객체 접근
+ => a.+ 라는 표현은 a의 객체의 멤버 함수 operator + 에 접근한다는 뜻
b => 오른쪽 피연산자는 a 객체의 멤버 함수 operator + 함수의 매개 변수로 전달 됨

이항 연산자 중복 : == 연산자

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  bool operator == (Power op2);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

bool Power::operator==(Power op2){
  if(this->kick == op2.kick && this->punch == op2.punch) return true;
  return false;
}

int main(){
  Power a(3,5), b(4,6);
  a.show();
  b.show();
  if(a == b) cout << "equal" << endl;
  else cout << "not equal" << endl;
}

결과
3 5
4 6
not equal

연산자 중복 : += 연산자


#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  Power operator += (Power op2);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power Power::operator+=(Power op2){
  kick = kick + op2.kick;
  punch = punch + op2.punch;
  return *this;
}

int main(){
  Power a(3,5), b(4,6),c;
  a.show();
  b.show();
  c = a += b;
  a.show();
  c.show();
}

결과
3 5
4 6
7 11
7 11

*this; : this 는 자기 자신 객체의 주소를 담고 있음 * 통해 값으로 변경 후 return

this 의 값 = 객체 자체

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  Power operator + (int op2);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power Power::operator+(int op2){
  Power tmp;
  tmp.kick = kick + op2;
  tmp.punch = punch + op2;
  return tmp;
}

int main(){
  Power a(3,5), b(4,6);
  a.show();
  b.show();
  b = a + 2;
  a.show();
  b.show();
}

결과
3 5
4 6
3 5
5 7

단항 연산자 중복

단항 연산자 : 피연산자가 하나 뿐인 연산자

  • 단항 연산자의 종류
    전위 연산자 (prefix operator)
    !op , ~op, ++op, --op
    후위 연산자 (postfix operator)
    op++, op--

전위 연산자 ++a

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  Power& operator ++();
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power& Power::operator++(){
  kick++;
  punch++;
  return *this;
}

int main(){
  Power a(3,5), b(4,6);
  a.show();
  b.show();
  b = ++a;
  a.show();
  b.show();
}

결과
3 5
4 6
4 6
4 6

전위 연산자 !a

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  bool operator!();
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

bool Power::operator!(){
  if(kick == 0 && punch == 0) return true;
  return false;
}

int main(){
  Power a(0,0), b(5,5);
  if(!a) cout << "a의 파워가 0" << endl;
  else cout << "a의 파워 0이 아님" << endl;
  if(!b) cout << "b의 파워가 0" << endl;
  else cout << "b의 파워 0이 아님" << endl;
}

결과
a의 파워가 0
b의 파워는 0이 아남

후위 연산자 a++

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  Power operator++(int x);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power Power::operator++(int x){
  Power tmp = *this;
  kick ++;
  punch ++;
  return tmp;
}

int main(){
  Power a(3,5), b;
  a.show();
  b.show();
  b = a++;
  a.show();
  b.show();
}

결과
3 5
0 0
4 6
3 5

2 + a 덧셈을 위한 + 연산자 함수 작성

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  friend Power operator+(int op1, Power op2);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power operator+(int op1, Power op2){
  Power tmp;
  tmp.kick = op1 + op2.kick;
  tmp.punch = op1 + op2.punch;
  return tmp;
}

int main(){
  Power a(3,5), b;
  a.show();
  b.show();
  b = 2 + a;
  a.show();
  b.show();
}

결과
3 5
0 0
3 5
5 7

참조를 리턴하는 << 연산자 작성

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Power{
  int kick;
  int punch;
public:
  Power(int kick=0,int punch = 0){
    this->kick = kick;
    this-> punch = punch;
  }
  void show();
  Power& operator << (int op1);
};

void Power::show(){
  cout << kick << ' ' << punch << endl;
}

Power& Power::operator<<(int op1){
  kick += op1;
  punch += op1;
  return *this;
}

int main(){
  Power a(1,2);
  a.show();
  a << 3 << 5 << 6;
  a.show();
}

결과
1 2
15 16

0개의 댓글