C Espresso 9 2010 All rights reserved 2010

  • Slides: 58
Download presentation
C++ Espresso 제 9장 다형성 © 2010 인피니티북스 All rights reserved

C++ Espresso 제 9장 다형성 © 2010 인피니티북스 All rights reserved

다형성이란? © 2010 인피니티북스 All rights reserved

다형성이란? © 2010 인피니티북스 All rights reserved

도형 예제 class Shape { protected: int x, y; public: void set. Origin(int x,

도형 예제 class Shape { protected: int x, y; public: void set. Origin(int x, int y){ this->x = x; this->y = y; } void draw() { cout <<"Shape Draw"; } }; © 2010 인피니티북스 All rights reserved

도형 예제 class Rectangle : public Shape { private: int width, height; public: void

도형 예제 class Rectangle : public Shape { private: int width, height; public: void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } void draw() { cout << "Rectangle Draw"; } }; © 2010 인피니티북스 All rights reserved

상향 형변환 · Shape *ps = new Rectangle(); · ps->set. Origin(10, 10); © 2010

상향 형변환 · Shape *ps = new Rectangle(); · ps->set. Origin(10, 10); © 2010 인피니티북스 All rights reserved // OK! 상향 형변환

하향 형변환 · Shape *ps = new Rectangle(); · 여기서 ps를 통하여 Rectangle의 멤버에

하향 형변환 · Shape *ps = new Rectangle(); · 여기서 ps를 통하여 Rectangle의 멤버에 접근하려면? 1. Rectangle *pr = (Rectangle *) ps; pr->set. Width(100); 2. ((Rectangle *) ps)->set. Width(100); 하향 형변환 © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Shape { // 일반적인도형을나타내는부모클래스 protected: int x,

예제 #include <iostream> using namespace std; class Shape { // 일반적인도형을나타내는부모클래스 protected: int x, y; public: void draw() { cout <<"Shape Draw" << endl; } void set. Origin(int x, int y){ this->x = x; this->y = y; } }; © 2010 인피니티북스 All rights reserved

예제 class Rectangle : public Shape { private: int width, height; public: void set.

예제 class Rectangle : public Shape { private: int width, height; public: void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } void draw() { cout << "Rectangle Draw"<< endl; } }; © 2010 인피니티북스 All rights reserved

예제 class Circle : public Shape { private: int radius; public: void set. Radius(int

예제 class Circle : public Shape { private: int radius; public: void set. Radius(int r) { radius = r; } void draw() { cout << "Circle Draw"<< endl; } }; © 2010 인피니티북스 All rights reserved

예제 int main() { } Shape *ps = new Rectangle(); ps->set. Origin(10, 10); ps->draw();

예제 int main() { } Shape *ps = new Rectangle(); ps->set. Origin(10, 10); ps->draw(); ((Rectangle *)ps)->set. Width(100); delete ps; Shape Draw 계속하려면 아무 키나 누르십시오. . . © 2010 인피니티북스 All rights reserved // OK! // Rectangle의set. Width() 호출

예제 class Shape {. . . } class Rectangle : public Shape {. .

예제 class Shape {. . . } class Rectangle : public Shape {. . . } int main() { Shape *ps = new Rectangle(); // OK! ps->draw(); } Shape 포인터 이기 때문에 Shape의 draw()가 호출 Shape Draw © 2010 인피니티북스 All rights reserved // 어떤 draw()가 호출되는가?

가상 함수 #include <iostream> #include <string> using namespace std; class Shape { protected: int

가상 함수 #include <iostream> #include <string> using namespace std; class Shape { protected: int x, y; public: void set. Origin(int x, int y){ this->x = x; this->y = y; } virtual void draw() { cout <<"Shape Draw" << endl; } }; © 2010 인피니티북스 All rights reserved 가상 함수 정의

가상 함수 class Rectangle : public Shape { private: int width, height; public: };

가상 함수 class Rectangle : public Shape { private: int width, height; public: }; void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } void draw() { cout << "Rectangle Draw" << endl; } © 2010 인피니티북스 All rights reserved 재정의

가상 함수 class Circle : public Shape { private: int radius; public: 재정의 void

가상 함수 class Circle : public Shape { private: int radius; public: 재정의 void set. Radius(int r) { radius = r; } void draw() { cout << "Circle Draw"<< endl; } }; © 2010 인피니티북스 All rights reserved

예제 int main() { } Shape *ps = new Rectangle(); // OK! ps->draw(); delete

예제 int main() { } Shape *ps = new Rectangle(); // OK! ps->draw(); delete ps; Shape *ps 1 = new Circle(); ps 1 ->draw(); delete ps 1; return 0; Rectangle Draw Circle Draw 계속하려면 아무 키나 누르십시오. . . © 2010 인피니티북스 All rights reserved // OK!

예제 #include <iostream> using namespace std; class Shape { protected: int x, y; public:

예제 #include <iostream> using namespace std; class Shape { protected: int x, y; public: virtual void draw() { cout <<"Shape Draw"; } void set. Origin(int x, int y){ this->x = x; this->y = y; } }; © 2010 인피니티북스 All rights reserved

예제 class Rectangle : public Shape { private: int width, height; public: void set.

예제 class Rectangle : public Shape { private: int width, height; public: void set. Width(int w) { width = w; } void set. Height(int h) { height = h; } void draw() { cout << "Rectangle Draw" << endl; } }; © 2010 인피니티북스 All rights reserved

예제 class Circle : public Shape { private: int radius; public: }; void set.

예제 class Circle : public Shape { private: int radius; public: }; void set. Radius(int r) { radius = r; } void draw() { cout << "Circle Draw" << endl; } © 2010 인피니티북스 All rights reserved

예제 class Triangle: public Shape { private: int base, height; public: void draw() {

예제 class Triangle: public Shape { private: int base, height; public: void draw() { cout << "Triangle Draw" << endl; } }; int main() { Shape *array. Of. Shapes[3]; array. Of. Shapes[0] = new Rectangle(); array. Of. Shapes[1] = new Triangle(); array. Of. Shapes[2] = new Circle(); for (int i = 0; i < 3; i++) { array. Of. Shapes[i]->draw(); } } © 2010 인피니티북스 All rights reserved

예제 Rectangle Draw Triangle Draw Circle Draw © 2010 인피니티북스 All rights reserved

예제 Rectangle Draw Triangle Draw Circle Draw © 2010 인피니티북스 All rights reserved

다형성의 장점 · 새로운 도형이 추가되어도 main()의 루프는 변경할 필요가 없다. class Parallelogram :

다형성의 장점 · 새로운 도형이 추가되어도 main()의 루프는 변경할 필요가 없다. class Parallelogram : Shape { public: void draw(){ cout << "Parallelogram Draw" << endl; } }; © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Animal { public: Animal() { cout <<"Animal

예제 #include <iostream> using namespace std; class Animal { public: Animal() { cout <<"Animal 생성자" << endl; } ~Animal() { cout <<"Animal 소멸자" << endl; } virtual void speak() { cout <<"Animal speak()" << endl; } }; class Dog : public Animal { public: Dog() { cout <<"Dog 생성자" << endl; } ~Dog() { cout <<"Dog 소멸자" << endl; } void speak() { cout <<"멍멍" << endl; } }; © 2010 인피니티북스 All rights reserved

예제 class Cat : public Animal { public: Cat() { cout <<"Cat 생성자" <<

예제 class Cat : public Animal { public: Cat() { cout <<"Cat 생성자" << endl; } ~Cat() { cout <<"Cat 소멸자" << endl; } void speak() { cout <<"야옹" << endl; } }; int main() { Animal *a 1 = new Dog(); a 1 ->speak(); Animal *a 2 = new Cat(); a 2 ->speak(); return 0; } © 2010 인피니티북스 All rights reserved

예제 #include <iostream> using namespace std; class Animal { public: virtual void speak() {

예제 #include <iostream> using namespace std; class Animal { public: virtual void speak() { cout <<"Animal speak()" << endl; } }; class Dog : public Animal { public: void speak() { cout <<"멍멍" << endl; } }; class Cat : public Animal { public: void speak() { cout <<"야옹" << endl; } }; © 2010 인피니티북스 All rights reserved

예제 int main() { } Dog d; Animal &a 1 = d; a 1.

예제 int main() { } Dog d; Animal &a 1 = d; a 1. speak(); Cat c; Animal &a 2 = c; a 2. speak(); return 0; 멍멍 야옹 © 2010 인피니티북스 All rights reserved

소멸자 문제 #include <iostream> using namespace std; class String { char *s; public: String(char

소멸자 문제 #include <iostream> using namespace std; class String { char *s; public: String(char *p){ cout << "String() 생성자" << endl; s = new char[strlen(p)+1]; strcpy(s, p); } ~String(){ cout << "String() 소멸자" << endl; delete[] s; } virtual void display() { cout << s; } }; © 2010 인피니티북스 All rights reserved

소멸자 문제 class My. String : public String { char *header; public: My. String(char

소멸자 문제 class My. String : public String { char *header; public: My. String(char *h, char *p) : String(p){ cout << "My. String() 생성자" << endl; header = new char[strlen(h)+1]; strcpy(header, h); } ~My. String(){ cout << "My. String() 소멸자" << endl; delete[] header; } void display() { cout << header; // 헤더출력 String: : display(); cout << header << endl; // 헤더출력 } }; © 2010 인피니티북스 All rights reserved

소멸자 문제 int main() { } String *p = new My. String("----", "Hello World!");

소멸자 문제 int main() { } String *p = new My. String("----", "Hello World!"); p->display(); delete p; return 0; String() 생성자 My. String() 생성자 ----Hello World!---String() 소멸자 My. String의 소멸자가 호 출되지 않음 © 2010 인피니티북스 All rights reserved // OK!

가상 소멸자 class String { char *s; public: String(char *p){. . . // 앞과동일

가상 소멸자 class String { char *s; public: String(char *p){. . . // 앞과동일 } virtual ~String(){ cout << "String() 소멸자" << endl; delete[] s; } }; class My. String : public String {. . . // 앞과동일 }; int main() { } . . . // 앞과동일 String() 생성자 My. String() 생성자 ----Hello World!---My. String() 소멸자 © 2010 인피니티북스 All rights reserved String() 소멸자

순수 가상 함수의 예 class Shape { protected: int x, y; public: … virtual

순수 가상 함수의 예 class Shape { protected: int x, y; public: … virtual void draw() = 0; }; class Rectangle : public Shape { private: int width, height; public: void draw() { cout << "Rectangle Draw" << endl; } }; © 2010 인피니티북스 All rights reserved

순수 가상 함수 int main() { Shape *ps = new Rectangle(); ps->draw(); delete ps;

순수 가상 함수 int main() { Shape *ps = new Rectangle(); ps->draw(); delete ps; } return 0; Rectangle Draw © 2010 인피니티북스 All rights reserved // OK! // Rectangle의draw()가호출된다.

예제 class Animal { virtual void move() = 0; virtual void eat() = 0;

예제 class Animal { virtual void move() = 0; virtual void eat() = 0; virtual void speak() = 0; }; class Lion : public Animal { void move(){ cout << "사자의 move() << endl; } void eat(){ cout << "사자의 eat() << endl; } void speak(){ cout << "사자의 speak() << endl; } }; © 2010 인피니티북스 All rights reserved

예제 class Remote. Control { // 순수가상함수정의 virtual void turn. ON() = 0; //

예제 class Remote. Control { // 순수가상함수정의 virtual void turn. ON() = 0; // 가전제품을켠다. virtual void turn. OFF() = 0; // 가전제품을끈다. } class Television : public Remote. Control { void turn. ON() { // 실제로TV의전원을켜기위한코드가들어간다. . } void turn. OFF() { // 실제로TV의전원을끄기위한코드가들어간다. . } } © 2010 인피니티북스 All rights reserved

예제 int main() { } Television *pt = new Television(); pt->turn. Off(); Refrigerator *pr

예제 int main() { } Television *pt = new Television(); pt->turn. Off(); Refrigerator *pr = new Refrigerator(); pr->turn. On(); pr->turn. Off(); delete pt; delete pr; return 0; © 2010 인피니티북스 All rights reserved

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

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