c++ programming: chapter 7 – inheritence

20
C++ Programming: chapter 7 – inheritence 2014, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik 1

Upload: kevina

Post on 17-Jan-2016

29 views

Category:

Documents


1 download

DESCRIPTION

C++ Programming: chapter 7 – inheritence. 2014, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik. class Student { string name; string ID; short year; string dept; float score; };. class Professor { string name; string ID; shortage; string dept; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++ Programming:  chapter 7 – inheritence

C++ Programming: chapter 7 – inheritence

2014, SpringPusan National UniversityKi-Joune Lihttp://isel.cs.pnu.edu/~lik

1

Page 2: C++ Programming:  chapter 7 – inheritence

PNU

class Student { string name; string ID; short year; string dept; float score;};

class Professor { string name; string ID; shortage; string dept; string url;};

common

어떻게 효과적으로 처리하는 방법은 ?

Page 3: C++ Programming:  chapter 7 – inheritence

PNU

3

class Student : public Person { short year; float score;};

class Professor : public Person { shortage; string url;};

class Person { string name; string ID; string dept;

};

common

Inheritance

Superclass

subclasssubclass

Page 4: C++ Programming:  chapter 7 – inheritence

PNU Superclass A, Subclass A1, A2

Superclass 의 모든 Attribute 와 member function 은 Subclass 로 상속

4

class Student : public Person { short year; float score;};

class Person { string name; string ID; string dept;

};

sizeof(person): 12 bytes

sizeof(Student): 12 + (2+4) bytes

Page 5: C++ Programming:  chapter 7 – inheritence

PNU

5

#include <iostream> #include <cstring> using namespace std; class Shape { public: double width; double height; void display() { cout << "Width and height are " << width << " and " << height << "\n"; } }; // Triangle is derived from Shape. class Triangle : public Shape { public: char style[20]; double area() { return width * height / 2; } void showStyle() { cout << "Triangle is " << style << "\n"; } };

#include <iostream> #include <cstring> using namespace std; int main() { Triangle t1; Triangle t2; t1.width = 4.0; t1.height = 4.0; strcpy(t1.style, "isosceles"); t2.width = 8.0; t2.height = 12.0; strcpy(t2.style, "right"); t1.showStyle(); t1.display(); cout << "Area is " << t1.area() << "\n"; t2.showStyle(); t2.display(); cout << "Area is " << t2.area() << "\n"; return 0; }

Page 6: C++ Programming:  chapter 7 – inheritence

PNU Constructor

6

class Shape { private: double width; double height; public: void display() { cout << "Width and height are " << width << " and " << height << "\n"; } // accessor functons double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } };

int main() { Triangle t1("isosceles", 4.0, 4.0); Triangle t2("right", 8.0, 12.0);

t1.showStyle(); t1.display(); cout << "Area is " << t1.area() << "\n"; t2.showStyle(); t2.display(); cout << "Area is " << t2.area() << "\n"; return 0; }

// Triangle is derived from Shape. class Triangle : public Shape { char style[20]; // now private public: // Constructor for Triangle. Triangle(char *str, double w, double h) { setWidth(w); setHeight(h); strcpy(style, str); } double area() { return getWidth() * getHeight() / 2; } void showStyle() { cout << "Triangle is " << style << "\n"; } };

Page 7: C++ Programming:  chapter 7 – inheritence

PNU private, protected, public and class inheritance

7

class Shape { private: double width; double height; public: void display() { cout << "Width and height are " << width << " and " << height << "\n"; }

Shape(double w,double h): width(w),height(h){}

// accessor functons double getWidth() { return width; } double getHeight() { return height; } };

int main() { Triangle t1("isosceles", 4.0, 4.0); Triangle t2("right", 8.0, 12.0);

t1.showStyle(); t1.display(); cout << "Area is " << t1.area() << "\n"; t2.showStyle(); t2.display(); cout << "Area is " << t2.area() << "\n"; return 0; }

// Triangle is derived from Shape. class Triangle : public Shape { char style[20]; // now private public: // Constructor for Triangle. Triangle(char *str, double w, double h): Shape(w,h) { strcpy(style, str); } double area() { return getWidth() * getHeight() / 2; } void showStyle() { cout << "Triangle is " << style << "\n"; } };

Page 8: C++ Programming:  chapter 7 – inheritence

PNU private, protected, public and class inheritance

8

class Shape { protected: double width; double height; public: void display() { cout << "Width and height are " << width << " and " << height << "\n"; } // accessor functons };

int main() { Triangle t1("isosceles", 4.0, 4.0); Triangle t2("right", 8.0, 12.0);

t1.showStyle(); t1.display(); cout << "Area is " << t1.area() << "\n"; t2.showStyle(); t2.display(); cout << "Area is " << t2.area() << "\n"; return 0; }

// Triangle is derived from Shape. class Triangle : public Shape { char style[20]; // now private public: // Constructor for Triangle. Triangle(char *str, double w, double h): Shape(w, h) { strcpy(style, str); } double area() { return width * height / 2; } void showStyle() { cout << "Triangle is " << style << "\n"; } };

Subclass에서만 접근 가능

Page 9: C++ Programming:  chapter 7 – inheritence

PNU

Super Class

Subclass

private protected public

private 접근 불가능 접근 불가능 ( 강제로 접근 가능하게 선언 )

public 접근 불가능 접근 가능 접근 가능

9

class base { int i; // private to basepublic: int j, k; void seti(int x) { i = x; } int geti() { return i; }};

class derived: private base {public: base::j; // make j public again - but not k base::seti(); // make seti() public base::geti(); // make geti() public int a; // public};

private, protected, public and class inheritance

Page 10: C++ Programming:  chapter 7 – inheritence

PNU Member function redefinition

10

class Shape { protected: double width; double height; public: Shape(double w, double h): width(w), height(h) {} void display() { cout << "Width and height are " << width << " and " << height << "\n"; }};

// Triangle is derived from Shape. class Triangle : public Shape { string style; // now private public: // Constructor for Triangle. Triangle(double w, double h, string s): width(w), height(h), style(s) {} void display() { cout << ”Style: “<<style<<“ Width:”<<width<<“ Height: "<<height << "\n"; } double area() { width*height/2.0; } };

int main() { Shape s(4.0, 4.0); Triangle t("right", 8.0, 12.0); Right_Triangle r(10.0);

t.display(); cout<<“Area: “<<r.area() <<“\n”; cout<<“Area: “<<r.Triangle::area()<<“\n”; return 0; }

class Regular_Triangle : public Triangle { double side; public: // Constructor for Triangle. Right_Triangle(double s): Triangle(s, sqrt(3.0)/2.0*s,“regular”), side(s) {} double area() { side*sqrt(3.0)/4.0; } };

Page 11: C++ Programming:  chapter 7 – inheritence

PNU Member function redefinition

11

class Shape { protected: double width; double height, double area;public: Shape(double w, double h, double a): width(w), height(h), area(a) {}};

// Triangle is derived from Shape. class Triangle : public Shape { protected: double s1, s2, s3;public: Triangle(double a, double b, string c): s1(a), s2(b), s3(c), Shape(s3,0.0,0.0) { height=s3*sqrt(1-((s3*s3+s1*s1)/2.0*s3*s1)*((s3*s3+s1*s1)/2.0*s3*s1)); area=s3*height/2.0; }};

class Rectangle : public Shape { public: Rectangle(double a, double b): Shape(a, b, a*b){} };

Page 12: C++ Programming:  chapter 7 – inheritence

PNU Class hierarchy and type conversion

12

FunctionA(Shape A) {…}

int main() { Shape s(4.0, 4.0); Triangle t("right", 8.0, 12.0);

FunctionA(t); // OK ??

FunctionB(s); // OK ??}

FunctionB(Triangle B) {…}

Page 13: C++ Programming:  chapter 7 – inheritence

PNU class hierarchy: example calculator

13

10*3+5 +

*

10 3

5

Node: Plus (+) Times (*) Int

class Node { protected: int code; public: enum {PLUS, TIME, INT}; Node(int c): code(c) {} int eval();};

class Plus: public Node { public: Node *left, *right; Plus(Node *l, Node *r): Node(PLUS), left(l), right(r) {}};

class Times: public Node { public: Node *left, *right; Times(Node *l, Node *r): Node(TIMES), left(l), right(r) {}};

class Int : public Node { public: unsigned int value; Int(int v): Node(INT), value(v) {}};

Page 14: C++ Programming:  chapter 7 – inheritence

PNU class hierarchy: example calculator

14

Node

Plus IntTimes

Node

Bop Int

Plus Times

Page 15: C++ Programming:  chapter 7 – inheritence

PNU class hierarchy: example calculator

15

class Times: public Bop { public: Times(Node *l, Node *r): Bop(TIMES, l, r) {}};

class Plus: public Bop { public: Plus(Node *l, Node *r): Bop(PLUS, l, r) {}};

int Node::eval() { switch(code) { case INT: return ((Int *)this)->value; case PLUS: return ((Bop *)this)->left->eval()+((Bop *)this)->right->eval(); case TIMES: return ((Bop *)this)->left->eval()*((Bop *)this)->right->eval(); }}

Node

Bop Int

Plus Times

class Bop: public Node {protected: Node *left, *right;public: Bop(int c, Node *l, Node *r): Node(c), left(l), right(r) {}};

class Node { protected: const int code; public: enum {PLUS, TIME, INT}; Node(int c): code(c) {} int eval();};

별로 자연스럽지 못한 방법

Page 16: C++ Programming:  chapter 7 – inheritence

PNU

16

int Node::eval() { switch(code) { case INT: return this->value; // wrong case PLUS: return this->left->eval()+this->right->eval(); // wrong case TIMES: return this->left->eval()*this->right->eval(); // wrong }}

무엇이 잘못되었는가 ?

int Node::eval() { switch(code) { case INT: return ((Int *)this)->value; case PLUS: return ((Bop *)this)->left->eval()+((Bop *)this)->right->eval(); case TIMES: return ((Bop *)this)->left->eval()*((Bop *)this)->right->eval(); }}

무엇이 부자연스러운가 ?

Page 17: C++ Programming:  chapter 7 – inheritence

PNU Virtual function

17

class Times: public Bop { public: Times(Node *l, Node *r): Bop(TIMES, l, r) {} int eval() {left->eval()*right->eval();}};

class Int: public Node { int value;public: Plus(Node *l, Node *r): Bop(INT, l, r) {} int eval() { return value; }};

Node

Bop Int

Plus Times

class Bop: public Node {protected: Node *left, *right;public: Bop(int c, Node *l, Node *r): Node(c), left(l), right(r) {}};

class Node { protected: const int code; public: enum {PLUS, TIME, INT}; Node(int c): code(c) {} virtual int eval() {return -1;} // -1 means “error”};

class Plus: public Bop { public: Plus(Node *l, Node *r): Bop(PLUS, l, r) {} int eval() {left->eval()+right->eval();}};

eval

eval

eval

eval

지금까지는 virtual 이 있으나 없으나 비슷함 ( 없는 경우 : function redefinition)

Page 18: C++ Programming:  chapter 7 – inheritence

PNU Virtual function

18

class Node { protected: const int code; public: enum {PLUS, TIME, INT}; Node(int c): code(c) {} virtual int eval() {return -1;} // -1 means “error”};

class Node { protected: const int code; public: enum {PLUS, TIME, INT}; Node(int c): code(c) {} int eval() {return -1;} // -1 means “error”};

#include <iostream>using namespace std;

int main() {  Node* node_ptr=new Node(INT); Int* int1_ptr=new Int(INT, 10); Int* int2_ptr=new Int(INT, 20); Plus* plus_ptr=new Bop(PLUS, int1_ptr, int2_ptr); int flag;

cout << “Input flag: ”; cin >> flag;

if (flag!=0) node_ptr=plus_ptr; else node_ptr=int1_ptr; cout<<“Evaluation result:” <<node_ptr->eval() << endl;

  return 0;}

이 문장으로는 Compile time 때 함수를 결정할 수 없음 Late Binding

다른 Function

Page 19: C++ Programming:  chapter 7 – inheritence

PNU C++ 에서 Polymorphism

객체가 무엇이냐에 따라 서로 다른 동작 ( 또는 함수 ) 이 수행되는 것 Overriding 조건 :

Pointer ( 왜 ??) Virtual Function

19

#include <iostream>using namespace std;

int main() {  Node node(INT); Int int1(INT, 10); Int int2(INT, 20); Plus plus(PLUS, &int1, &int2); int flag;

cout << “Input flag: ”; cin >> flag;

if (flag!=0) node=plus; else node=int1; cout<<“Evaluation result:” <<node.eval() << endl;

  return 0;}

#include <iostream>using namespace std;

int main() {  Node* node_ptr; Int* int1_ptr=new Int(INT, 10); Int* int2_ptr=new Int(INT, 20); Plus* plus_ptr=new Bop(PLUS, int1_ptr, int2_ptr); int flag;

cout << “Input flag: ”; cin >> flag;

if (flag!=0) node_ptr=plus_ptr; else node_ptr=int1_ptr; cout<<“Evaluation result:” <<node_ptr->eval() << endl;

  return 0;}

무슨 일이 일어날까 ?

Page 20: C++ Programming:  chapter 7 – inheritence

PNU Pure Virtual Function

Subclass 에서만 정의되는 Virtual Function Pure Virtual Function 을 가지고 있는 Class: Abstract

Class Abstract Class 에서는 객체를 생성할 수 없음

20

class Node { protected: const int code; public: enum {PLUS, TIME, INT}; Node(int c): code(c) {} virtual int eval()=0;};

class Bop: public Node {protected: Node *left, *right;public: Bop(int c, Node *l, Node *r): Node(c), left(l), right(r) {}};

#include <iostream>using namespace std;

int main() {  Node* node_ptr=new Node(INT); // wrong Bop bop(INT, NULL, NULL); // wrong   return 0;}

class Plus: public Bop { public: Plus(Node *l, Node *r): Bop(PLUS, l, r) {} int eval() {left->eval()+right->eval();}};