'연산자 오버로딩'에 해당되는 글 2건

  1. 2012.10.23 연산자 오버로딩 - 맴버함수, 전역함수의 연산자 오버로딩

연산자 오버로딩의 목적 

Build-in타입에 대해 사용할수 없었던 연산들을 사용자 정의 데이터 타입에 대해서도 동일한 이름으로 동일한 연산을 수행하도록하여, 이해하기쉽고 간결하게 만듬


1) Build-In type

void func() {

int i=10, j=20, sum;

sum=i+j;

}


2) User-defined Type => 좀더 복잡한 연산을 할경우 이해하기가 어려워진다.

void func() {

UserType i=10, j=20, sum;

sum.add(i, j);

}


오버로딩 가능 연산자

+

-

*

/

%

^

&

|

~=

!

=

<

>

<=

>=

==

!=

+=

-=

*=

/=

%=

^=

&=

|=

<<=

>>=

<<

>>

&&

||

++

--

,

->*

->

()

[]

new

delete


오버로딩 제약사항

오버로딩 된 연산자는 적어도 피연산자가 사용자 정의 데이터 타입이어야 한다.

) intint를 더하는 +연산자를 오버로딩 할 수 없다.

오버로딩 된 연산자는 원래의 연산자에 적용되는 문법 규칙과 맞지 않게 사용할 수 없다.

) 두개의 연산자를 갖는 나눗셈 연산자(/)가 하나의 연산자만 갖도록 오버로딩 할 수 없다.

연산자 우선순위를 변경할 수 없다.

새로운 기호를 사용하여 연산자를 만들 수 없다.

기본적인 연산자 오버로딩

#include <iostream>

 

using namespace std;

 

class Point {

private:

         int x, y;

public:

         Point(int _x, int _y) : x(_x), y(_y) {}     //생성자

         void ShowPosition();

         void operator+(int val);

};

 

void Point::ShowPosition() {

         cout << x << " " << y << endl;

}

 

//+ operator오버로딩

void Point::operator+(int val) {

         x+=val;

         y+=val;

}

 

int main(void) {

         Point p(3, 4);

         p.ShowPosition(); // 출력결과: 3, 4

 

         p.operator +(10);

         p.ShowPosition(); // 출력결과: 13, 14

 

         p+10;

         p.ShowPosition(); // 출력결과: 23, 24

}

+에대해 연산자 오버로이드를 할경우 p.operator +(10); = p+10 서로 같은 의미로 사용된다.

p1+p2의 연산자 오버로딩

#include <iostream>

 

using namespace std;

 

class Point {

private:

         int x, y;

public:

         Point(int _x, int _y) : x(_x), y(_y) {}     //생성자

         void ShowPosition();

         Point operator+(const Point& p);

};

 

void Point::ShowPosition() {

         cout << x << " " << y << endl;

}

 

//+ operator오버로딩

Point Point::operator+(const Point& p) {

         Point temp(x+p.x, y+p.y);

         return temp;

}

 

int main(void) {

         Point p1(1, 2);

         Point p2(2, 1);

         Point p3=p1+p2;            //Point p3=p1+p2; => Point p3 = 리턴된 temp의 객체

        p3.ShowPosition();         //Point p3의출력값: 3, 3

}


전역함수로 오버로딩된경우

#include <iostream>

 

using namespace std;

 

class Point {

private:

         int x, y;

public:

         Point(int _x, int _y) : x(_x), y(_y) {}     //생성자

         void ShowPosition();

        

         //전역, +operator오버로딩된함수를friend로지정

         friend Point operator+(const Point &p1, const Point& p2);    

};

 

void Point::ShowPosition() {

         cout << x << " " << y << endl;

}

 

//전역함수+operator오버로딩

Point operator+(const Point &p1, const Point& p2) {

         Point temp(p1.x+p2.x, p1.y+p2.y);

         return temp;

}

 

int main(void) {

         Point p1(1, 2);

         Point p2(2, 1);

         Point p3=p1+p2;

         p3.ShowPosition();         //출력값: 3, 3

}


[] 연산자 오버라이딩

[]operator.cpp

'Developer > C / C++' 카테고리의 다른 글

스트림 - 파일 I/O  (0) 2012.10.24
스트림 - 표준입출력(cout, cin)  (0) 2012.10.24
예외처리  (0) 2012.10.24
STL(Standard Template Library) for C++  (0) 2012.10.23
템플릿 (클래스 템플릿, 함수 템플릿)  (0) 2012.10.23
Posted by No names
,