28
C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming 연산자 연산자 다중 다중 정의 정의 연산자 연산자 다중 다중 정의 정의 Seo Seo, Doo , Doo-ok ok [email protected] [email protected] http://www.Clickseo.com http://www.Clickseo.com

연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

C++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ Programming

연산자연산자 다중다중 정의정의연산자연산자 다중다중 정의정의

SeoSeo, Doo, Doo--okok

[email protected]@gmail.comhttp://www.Clickseo.comhttp://www.Clickseo.com

Page 2: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

목목 차차

연산자연산자 다중다중 정의정의연산자연산자 다중다중 정의정의

C++ C++ 스타일의스타일의 문자열문자열

2

Page 3: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

연산자연산자 다중다중 정의정의중중 정정

연산자연산자 다중다중 정의정의 연산자연산자 다중다중 정의정의

단항 연산자 다중 정의단항 연산자 다중 정의

이항 연산자 다중 정의이항 연산자 다중 정의

cin, cout 그리고 endl cin, cout 그리고 endl

C++C++ 스타일의스타일의 문자열문자열 C++ C++ 스타일의스타일의 문자열문자열

3

Page 4: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

연산자연산자 다중다중 정의정의중중 정정

연산자 다중 정의 (Operator Overloading)

operator와 연산자 기호를 통해 연산자의 기능을 다중 정의

• “클래스를 C++의 기본 자료형과 같이 다룰 수 있는 방법”

operator+()operator+()

{

// 주어진 연산자의 기능 정의

};

4

Page 5: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

이항이항 연산자연산자 다중다중 정의정의항항 중중 정정

이항 연산자(binary operator) 다중 정의

이항 연산자 : 두 개의 연산자에 대한 연산을 수행하는 연산자

• 대표적인 이항 연산자 : +, -, *, /, %

p.operator+(int)

p + 10

멤버 함수로 다중 정의한 함수

operator+(const Point &p, int)Point

일반 함수로 다중 정의한 함수

Pointint x;int y;(생략)

5

Page 6: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

이항이항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정

멤버 함수에 의한 이항 연산자 다중 정의

class Point{

int x, y;public:

P i t(i t 0 i t 0)

Pointint x;int y;Point(int = 0 int = 0);Point(int x = 0, int y = 0);

void ShowPosition(void);

Point operator+(int num){

Point(int = 0, int = 0);void ShowPosition();Point operator+(int);

{Point temp(x + num, y + num);

return temp;}

}};

int main(void){

Point a(10, 20);Point a(10, 20);

Point res = a + 10; // Point res = a.operator+(10);

return 0;}

6

}

Page 7: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

이항이항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정

일반 함수에 의한 이항 연산자 다중 정의

class Point{

int x, y;public:

P i t(i t 0 i t 0)Point(int x = 0, int y = 0);void ShowPosition(void);friend Point operator+(const Point &, int num);

};

Point::Point(int x, int y){

this->x = x;this->y = y;

}}

int main(void){

Point a(10, 20); PointPoint a(10, 20);

Point res = a + 10;

return 0;}

int x;int y;Point(int = 0, int = 0);oid Sho Position()

7

} void ShowPosition();friend Point operator+(const Point &, int);

Page 8: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

이항이항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정

이항 연산자의 교환 법칙

교환 법칙 성립환 법칙 성립

p + 10 10 + p==

Pointint x;i tint y;(생략)

8

Page 9: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

이항이항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정프로그램 예제 : 이항 연산자의 다중 정의 (1/2)

#include <iostream>#include <iostream>

using std::cin;using std::cout;using std::endl;

class Point{

int x, y;public:

Point(int x = 0, int y = 0);Point(int x 0, int y 0);void ShowPosition();Point operator+(int num);friend Point operator+(int num, Point &p);

};

Point::Point(int x, int y){

this->x = x;this->y = y;

}}

void Point::ShowPosition(){

cout << "(" << x << ", " << y << ")" << endl;}

9

}

Page 10: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

이항이항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정프로그램 예제 : 이항 연산자의 다중 정의 (2/2)

Point Point::operator+(int num)Point Point::operator+(int num){

Point temp(x + num, y + num);return temp;

}

Point operator+(int num, Point &p){

return p + num;}

i t i ()int main(){

Point p1(10, 20);cout << "p1 : "; p1.ShowPosition();

i 2 0Point p2 = p1 + 10;cout << "\np1 + 10" << endl;cout << "p2 : "; p2.ShowPosition();

p2 = 10 + p1;p2 10 + p1;cout << "\np2 = 10 + p1" << endl;cout << "p2 : "; p2.ShowPosition();

return 0;}

10

}

Page 11: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

단항단항 연산자의연산자의 다중다중 정의정의항항 중중 정정

단항 연산자(unary operator) 다중 정의

단항 연산자 : 하나의 피연산자에 대한 연산을 수행하는 연산자

• 대표적인 단항 연산자 : ++, --

p.operator++()

멤버 함수로 다중 정의한 함수

++p

멤버 함수로 다중 정의한 함수

operator++(Point &p)Point

일반 함수로 다중 정의한 함수int x;int y;(생략)

11

Page 12: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

단항단항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정

멤버 함수에 의한 단항 연산자 다중 정의

class Point{

int x, y;public:

P i t(i t 0 i t 0)

Pointint x;int y;Point(int = 0 int = 0);Point(int x = 0, int y = 0);

void ShowPosition(void);Point &operator++(){

++x;

Point(int = 0, int = 0);void ShowPosition();Point &operator++();

++x;++y;

return *this;}

}};

int main(void){

Point a(10, 20);Point a(10, 20);

++a; // a.operator++();

return 0;}

12

}

Page 13: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

단항단항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정

일반 함수에 의한 단항 연산자 다중 정의

class Point{

int x, y;public:

P i t(i t 0 i t 0)Point(int x = 0, int y = 0);void ShowPosition(void);friend Point &operator++(Point &);

}

Point &operator++(Point &p){

++p.x;++p.y;

return p;}

int main(void){ Point

Point a(10, 20);

++a;

return 0;

int x;int y;Point(int = 0, int = 0);oid Sho Position()

13

} void ShowPosition();friend Point &operator++(Point &);

Page 14: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

단항단항 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)항항 중중 정정

전위와 후위 증감 연산자

증감 연산자 : ++, --

++ t ++()++p p.operator++()

p.operator++(int)p++ p.operator (int)p

--p p.operator--()

p.operator--(int)p--

14

Page 15: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

cin, cout cin, cout 그리고그리고 endlendl,,

cin 과 cout

cout << “Hi~ Clickseo”;cout << 100;cout << 3 14159;cout << 3.14159;cout << endl;cout << “Hi~ Clickseo” << 100 << 3.14159 << endl;

int n;double d;char name[12];

cin >> n;;cin >> d;cin >> name;cin >> n >> d >> name;

15

cin >> n >> d >> name;

Page 16: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

cin, cout cin, cout 그리고그리고 endl endl (cont(cont’’d)d),,프로그램 예제 : cin, cout 그리고 endl (1/2)

#include <iostream>#include <iostream>#include <cstdio>

namespace mystd{

char *endl = "₩n";class ostream{public:

ostream &operator<<(char *str){

i tf(" %s " t )printf(" %s ", str);return *this;

}

ostream &operator<<(int n){

%d{

printf(" %d ", n);return *this;

}

ostream &operator<<(double d){{

printf(" %f ", d);return *this;

}};

ostream cout;

16

ostream cout;}

Page 17: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

cin, cout cin, cout 그리고그리고 endl endl (cont(cont’’d)d),,프로그램 예제 : cin, cout 그리고 endl (2/2)

i td tusing mystd::cout;using mystd::endl;

int main()int main(){

cout << "Hi~ Clickseo" << endl;cout << 100 << endl;cout << 3.14159 << endl;

cout << "Hi~ Clickseo" << 100 << 3.14159 << endl;

return 0;}

17

Page 18: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

C++ C++ 스타일의스타일의 문자열문자열

연산자연산자 다중다중 정의의정의의 이해이해 연산자연산자 다중다중 정의의정의의 이해이해

C++ C++ 스타일의스타일의 문자열문자열

C/C++ 스타일의 문자열

문자열과 연산자 다중 정의

string 클래스 멤버 함수

18

Page 19: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

C/C++ C/C++ 스타일의스타일의 문자열문자열

C 스타일의 문자열

C++에서 제공하는 문자열 조작 함수를 사용 : <cstring> C++에서 제공하는 문자열 조작 함수를 사용 : <cstring>

#include <iostream>#include <cstring>g

int main(void){

char str[] = "Hi~ Clickseo!!!";char copy[1024];int len = strlen(str);

strcpy(copy, str);

std::cout << "str : " << str << std::endl;std::cout << "copy : " << copy << std::endl;

return 0;

19

}

Page 20: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

C/C++ C/C++ 스타일의스타일의 문자열문자열 (cont(cont’’d)d)

C++ 스타일의 문자열

string : basic_string 클래스를 재정의한 형태

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

using std::cout;using std::endl;

using std::string;

int main(void)( ){

string s = "Hello World!!!!";

cout << s << endl;cout << s << endl;

return 0;}

20

Page 21: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

문자열과문자열과 연산자연산자 다중다중 정의정의중중 정정프로그램 예제 : 문자열 복사

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

using std::cout;using std::endl;

using std::string;

int main(void){{

string str = "Hello World!!!!";string copy;

cout << "str : " << str << endl;cout << "copy : " << copy << endl;

copy = str;

cout << "str : " << str << endl;

문자열 복사 : str2 = str1;

cout << "str : " << str << endl;cout << "copy : " << copy << endl;

return 0;}

21

}

Page 22: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

문자열과문자열과 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)중중 정정프로그램 예제 : 문자열의 결합

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

using std::cout;using std::endl;

문자열의 결합 : str1 str2

using std::string;

int main(void){ 문자열의 결합 : str1 + str2{

string str1 = "Hi~ ";string str2 = "Clickseo";

cout << "str1 : " << str1 << endl;cout << "str2 : " << str2 << endl;

str1 = str1 + str2;;

cout << "str1 : " << str1 << endl;cout << "str2 : " << str2 << endl;

22

return 0;}

Page 23: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

문자열과문자열과 연산자연산자 다중다중 정의정의 (cont(cont’’d)d)중중 정정프로그램 예제 : 문자열의 비교

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

using std::cout;using std::endl;

문자열의 비교 : str1 == str2

using std::string;

int main(void){

str1 != str2{

string str1 = "Hi~ ";string str2 = "Clickseo";

cout << "str1 : " << str1 << endl;cout << "str2 : " << str2 << endl;

if( str1 == str2 ) cout << "일치!!!" << endl;( )

if( str1 != str2 ) cout << "불일치!!!" << endl;

return 0;

23

}

Page 24: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

string string 클래스클래스 멤버멤버 함수함수 (cont(cont’’d)d)gg프로그램 예제 : 공백이 포함된 문자열 입력

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

using std::cout;using std::endl;using std::endl;

using std::string;

int main(void)int main(void){

char name[12]; // C 스타일string str; // C++ 스타일

cin.getline(name, 12); // C 스타일getline(cin, str); // C++ 스타일

cout << "name : " << name << endl;cout << name : << name << endl;cout << "str : " << str << endl;

return 0;}

24

}

Page 25: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

string string 클래스클래스 멤버멤버 함수함수 (cont(cont’’d)d)gg프로그램 예제 : 문자열의 길이

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

using std::cout;using std::endl;

using std::string;

int main(void){

문자열의 길이 : str.size();

{

string str1;

string str2 = "Hello World!!!";

istring str3 = "12345";

cout << "str1 : " << str1.size() << endl;

cout << "str2 : " << str2.size() << endl;

cout << "str3 : " << str3.size() << endl;

0

25

return 0;}

Page 26: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

string string 클래스클래스 멤버멤버 함수함수 (cont(cont’’d)d)gg프로그램 예제 : 문자열의 검색

#i l d <i t >#include <iostream>#include <string>

using std::cout;using std::cout;using std::endl;

using std::string;

문자열의 검색 : str.find(s);int main(void){

string t "Hi Cli k "string str = "Hi~ Clickseo";

cout << "str : " << str << endl;

cout << str.find("Clickseo") << endl;

return 0;

26

}

Page 27: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

string string 클래스클래스 멤버멤버 함수함수 (cont(cont’’d)d)gg프로그램 예제 : 문자열의 일부분 얻기

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

using std::cout;

일부 문자열 추출 : str.substr(begin, size);

using std::endl;

using std::string;

int main(void){

string str = "Hi~ Clickseo";

int len = str.size();string sub = str.substr(len - 3, 3);

cout << "str : " << str << endl;cout << "sub : " << sub << endl;

return 0;

27

return 0;}

Page 28: 연산자연산자다중연산자다중정의정의 - Clickseo.com · 2014. 10. 4. · [1] 윤성우, “열혈강의C++ 프로그래밍”, 프리렉, 2007. [2] 이현창, “뇌를자극하는C++

참고문헌참고문헌[1] 윤성우, “열혈강의 C++ 프로그래밍”, 프리렉, 2007.

[2] 이현창, “뇌를 자극하는 C++ 프로그래밍”, 한빛미디어, 2008.

[3] H M D it l P J D it l “C HOW TO PROGRAM 6th Editi ” P ti H ll 2009[3] H.M. Deitel, P. J. Deitel, “C++ HOW TO PROGRAM : 6th Edition”, Prentice Hall, 2009.

[4] Wikipedie, http://www.wikipedia.org/.

이 강의자료는 저작권법에 따라 보호받는 저작물이므로 무단 전제와 무단 복제를 금지하며,

내용의 전부 또는 일부를 이용하려면 반드시 저작권자의 서면 동의를 받아야 합니다.

C i ht © Cli k All i ht d

28

Copyright © Clickseo.com. All rights reserved.