C Espresso 10 2010 All rights reserved include

  • Slides: 81
Download presentation
C++ Espresso 제 10장 프렌드와 연산자 중복 © 2010 인피니티북스 All rights reserved

C++ Espresso 제 10장 프렌드와 연산자 중복 © 2010 인피니티북스 All rights reserved

예제 #include <iostream> #include <string> using namespace std; class Company { private: int sales,

예제 #include <iostream> #include <string> using namespace std; class Company { private: int sales, profit; public: // sub()는Company의 전용부분에 접근할 수있다. friend void sub(Company& c); Company(): sales(0), profit(0) { } }; void sub(Company& c) { cout << c. profit << endl; } © 2010 인피니티북스 All rights reserved

예제 int main() { } Company c 1; sub(c 1); return 0; 0 ©

예제 int main() { } Company c 1; sub(c 1); return 0; 0 © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Date { private: public: } friend bool

예제 #include <iostream> using namespace std; class Date { private: public: } friend bool equals(Date d 1, Date d 2); int year, month, day; Date(int y, int m, int d) { year = y; month = m; day = d; } © 2010 인피니티북스 All rights reserved

예제 // 프렌드함수 bool equals(Date d 1, Date d 2) { return d 1.

예제 // 프렌드함수 bool equals(Date d 1, Date d 2) { return d 1. year == d 2. year && d 1. month == d 2. month && d 1. day == d 2. day; } int main() { } Date d 1(1960, 5, 23), d 2(2002, 7, 23); cout << equal_f(d 1, d 2) << endl; 멤버 변수 접근 가 능 © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Complex { public: private: }; friend Complex

예제 #include <iostream> using namespace std; class Complex { public: private: }; friend Complex add (Complex, Complex); Complex (double r, double i) {re=r; im=i; } Complex(double r) { re=r; im=0; } Complex () { re = im = 0; } void Output(){ cout << re << " + " << im <<"i" << endl; } double re, im; © 2010 인피니티북스 All rights reserved

예제 Complex add(Complex a 1, Complex a 2) { return Complex (a 1. re+a

예제 Complex add(Complex a 1, Complex a 2) { return Complex (a 1. re+a 2. re, a 1. im+a 2. im); } int main() { } Complex c 1(1, 2), c 2(3, 4); Complex c 3 = add(c 1, c 2); c 3. Output(); return 0; 4 + 6 i 계속하려면 아무 키나 누르십시오. . . © 2010 인피니티북스 All rights reserved

원점 벡터 예제 class Vector { private: double x, y; public: Vector(double x, int

원점 벡터 예제 class Vector { private: double x, y; public: Vector(double x, int double){ this->x = x; this->y = y; } } © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Vector { private: double x, y; public:

예제 #include <iostream> using namespace std; class Vector { private: double x, y; public: Vector(double x, double y){ this->x = x; this->y = y; } friend Vector operator+(const Vector& v 1, const Vector& v 2); void display() { cout << "(" << x << ", " << y << ")" << endl; } }; © 2010 인피니티북스 All rights reserved

예제 Vector operator+(const Vector& v 1, const Vector& v 2) { Vector v(0. 0,

예제 Vector operator+(const Vector& v 1, const Vector& v 2) { Vector v(0. 0, 0. 0); v. x = v 1. x + v 2. x; v. y = v 1. y + v 2. y; return v; } int main() { Vector v 1(1, 2), v 2(3, 4); Vector v 3 = v 1 + v 2; v 3. display(); return 0; } (4, 6) © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Vector { private: double x, y; public:

예제 #include <iostream> using namespace std; class Vector { private: double x, y; public: Vector(double x, double y){ this->x = x; this->y = y; } Vector operator+(Vector& v 2) { Vector v(0. 0, 0. 0); v. x = this->x + v 2. x; v. y = this->y + v 2. y; return v; } © 2010 인피니티북스 All rights reserved

예제 void display() { cout << "(" << x << ", " << y

예제 void display() { cout << "(" << x << ", " << y << ")" << endl; } }; int main() { Vector v 1(1. 0, 2. 0), v 2(3. 0, 4. 0); Vector v 3 = v 1 + v 2; v 3. display(); return 0; } (4, 6) © 2010 인피니티북스 All rights reserved

= 연산자 중복 © 2010 인피니티북스 All rights reserved

= 연산자 중복 © 2010 인피니티북스 All rights reserved

변환 생성자 #include <iostream> #include <string> using namespace std; class Book { private: int

변환 생성자 #include <iostream> #include <string> using namespace std; class Book { private: int isbn; // 책의ISBN string title; // 책의제목 public: Book() { // 생성자 isbn = 0; title = “unknown"; } Book(int isbn) { this->isbn = isbn; this->title = "unknown"; } void display() { cout << isbn << ": " << title << endl; } © 2010 인피니티북스 All rights reserved 변환 생성자 (int->Book)

변환 연산자의 중복 정의 #include <iostream> #include <string> using namespace std; class Book {

변환 연산자의 중복 정의 #include <iostream> #include <string> using namespace std; class Book { private: int isbn; string title; public: Book(int isbn, string& title) { this->isbn = isbn; this->title = title; } Book(int isbn) { this->isbn = isbn; this->title = "unknown"; } operator int() const { return isbn; } © 2010 인피니티북스 All rights reserved 변환 생성자 (int->Book) 변환 연산자 (Book->int)

예제 void display() { }; } cout << isbn << ": " << title

예제 void display() { }; } cout << isbn << ": " << title << endl; bool check(int isbn) { cout << isbn << endl; return true; } int main() { } Book b 1 = 9782001; b 1. display(); int isbn = b 1; cout << isbn << endl; check(b 1); return 0; © 2010 인피니티북스 All rights reserved // 변환생성자실행! // 변환연산자실행!

My. String #include <iostream> using namespace std; class My. String { private: char *p.

My. String #include <iostream> using namespace std; class My. String { private: char *p. Buf; public: }; //동적으로할당된메모리의주소값저장 My. String(const char *s=NULL); My. String(My. String& s); ~My. String(); void print(); // 문자열을화면에출력 int get. Size(); // 문자열의길이반환 My. String operator+(My. String& s); // + 연산자중복정의 © 2010 인피니티북스 All rights reserved

My. String // 생성자 My. String: : My. String(const char *s) { if( s

My. String // 생성자 My. String: : My. String(const char *s) { if( s == NULL ) { p. Buf = new char[1]; p. Buf[0] = NULL; } else { p. Buf = new char[: : strlen(s)+1]; strcpy(p. Buf, s); } } // 복사생성자 My. String: : My. String(My. String &s) { p. Buf = new char[s. get. Size()+1]; strcpy(p. Buf, s. p. Buf); } © 2010 인피니티북스 All rights reserved

My. String: : ~My. String() { if ( p. Buf ) delete [] p.

My. String: : ~My. String() { if ( p. Buf ) delete [] p. Buf; } void My. String: : print() { cout << p. Buf << endl; } int My. String: : get. Size() { return strlen(p. Buf); } My. String: : operator+(My. String& s) { char *temp = new char[get. Size() + s. get. Size() + 1]; strcpy(temp, p. Buf); strcat(temp, s. p. Buf); My. String r(temp); delete [] temp; return r; } © 2010 인피니티북스 All rights reserved

My. String int main() { My. String s 1("Hello "); My. String s 2("World!");

My. String int main() { My. String s 1("Hello "); My. String s 2("World!"); My. String s 3 = s 1 + s 2; s 1. print(); s 2. print(); s 3. print(); } return 0; Hello World! © 2010 인피니티북스 All rights reserved

My. Array #include <iostream> #include <assert. h> using namespace std; // 향상된배열을나타낸다. class My.

My. Array #include <iostream> #include <assert. h> using namespace std; // 향상된배열을나타낸다. class My. Array { friend ostream& operator<<(ostream &, const My. Array &); 출력연산자<< private: int *data; // 배열의데이터 int size; // 배열의크기 public: }; My. Array(int size = 10); ~My. Array(); // // 디폴트생성자 // 소멸자 int get. Size() const; // 배열의크기를반환 My. Array& operator=(const My. Array &a); // = 연산자중복정의 int& operator[](int i); // [] 연산자중복: 설정자 © 2010 인피니티북스 All rights reserved

My. Array: : My. Array(int s) { size = (s > 0 ? s

My. Array: : My. Array(int s) { size = (s > 0 ? s : 10); data = new int[size]; // 디폴트크기를 10으로한다. // 동적메모리할당 for (int i = 0; i < size; i++) data[i] = 0; // 요소들의초기화 } My. Array: : ~My. Array() { delete [] data; // 동적메모리반납 data = NULL; } My. Array& My. Array: : operator=(const My. Array& a) { if (&a != this) { // 자기자신인지를체크 delete [] data; // 동적메모리반납 size = a. size; // 새로운크기를설정 data = new int[size]; // 새로운동적메모리할당 for (int i = 0; i < size; i++) data[i] = a. data[i]; // 데이터복사 } } return *this; © 2010 인피니티북스 All rights reserved // a = b = c와같은경우를대비

My. Array int My. Array: : get. Size() const { return size; } int&

My. Array int My. Array: : get. Size() const { return size; } int& My. Array: : operator[](int index) { assert(0 <= index && index < size); // 인데스가범위에있지않으면중지 return data[index]; } // 프렌드함수정의 ostream& operator<<(ostream &output, const My. Array &a) { int i; for (i = 0; i < a. size; i++) { output << a. data[i] << ' '; } output << endl; return output; // cout << a 1 << a 2 << a 3와같은경우대비 } © 2010 인피니티북스 All rights reserved

My. Array int main() { My. Array a 1(10); a 1[0] = 1; a

My. Array int main() { My. Array a 1(10); a 1[0] = 1; a 1[1] = 2; a 1[2] = 3; a 1[3] = 4; cout << a 1 ; } return 0; 1234000000 © 2010 인피니티북스 All rights reserved

Q&A © 2010 인피니티북스 All rights reserved

Q&A © 2010 인피니티북스 All rights reserved