problem: given a vector of pointers to shape objects, print the...

22
Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. 1

Upload: others

Post on 16-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape.

1

Page 2: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

Problem: Given a vector of pointers to Shape objects, sort the shape objects by areas in decreasing order.

Std++sort: genericity (泛型)

2

Page 3: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

3

Object-Oriented Programming C++

Inheritance and Polymorphism

CSIE Department, NTUT

Woei-Kae Chen

Page 4: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

4

Introduction to inheritance

Inheritance Derived class

inherits data members and member functions from base class

Single inheritance inherits from one base class

Multiple inheritance inherits from multiple base classes

Types of inheritance public private protected

Base

Derived

Alternative

to composition

“Is a”

relationship

Page 5: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

5

Inheritance Examples 1

Base class Derived classes

Student GraduateStudent

UndergraduateStudent

Shape Circle

Triangle

Rectangle

Loan CarLoan

HomeImprovementLoan

MortgageLoan

Employee FacultyMember

StaffMember

Account CheckingAccount

SavingsAccount

Page 6: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

6

Single inheritance

CommunityMember

Employee Student

Administrator Teacher

AdministratorTeacher

Staff Faculty

Alumnus

Single inheritance

Single inheritance

Multiple inheritance

Base class

(或super class)

Derived class

(或subclass)

Inheritance Examples 2

Page 7: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

7

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

Inheritance Examples 3

Page 8: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

8

“is-a” vs. “has-a” relationship

“is-a” Inheritance Derived class object treated as base class

object Example: Car is a vehicle

“has-a” Composition Object contains one or more objects of other

classes as members Example: Car has a steering wheel

Page 9: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

9

UML Example (C++): Inheritance

class X {

...

};

class Y : public X {...

...

};

X

Y UML:

Inheritance

UML:

Class Shape

Circle

Page 10: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

10

UML Example (C++): Composition

class Employee {

...

...

Date d; // Composition

};

X Y

Employee Date

Page 11: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

11

Introduction to polymorphism

Problem:

A list of unknown shapes?

draw()

...()

Shape

draw()

...()

Circle

draw()

...()

Rectangle

draw()

...()

Triangle

Page 12: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

12

Declaring a list of unknown shapes

Shape s[100]; // Bad

Circle s[100]; // Bad

Shape s[100]; // Bad

Shape *s[100]; // OK, 但怎麼呼叫draw()?

draw()

...()

Shape

draw()

...()

Circle

draw()

...()

Rectangle

draw()

...()

Triangle

1. 混合type的shape

怎麼儲存在array?

2. 怎麼呼叫draw()?

s[0] = new Circle;

s[1] = new Triangle;

...

for (i=0;i<100;i++)

s[i]->draw();// ???

Solution:

設成virtual

function

Does NOT

work

Page 13: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

13

Effect of Virtual Functions

Example (non-virtual) class X {

public:

void f() {

cout << ’x’;

}

};

class Y : public X {

public:

void f() {cout<<’y’;}

};

...

X a; a.f(); // x

Y b; b.f(); // y

X *p=&a;

p->f(); // x

X *p=&b;

p->f(); // x

Example (virtual) class X {

public:

virtual void f(){

cout << ’x’;

}

};

class Y : public X {

public:

void f() {cout<<’y’;}

};

...

X a; a.f(); // x

Y b; b.f(); // y

X *p=&a;

p->f(); // x

X *p=&b;

p->f(); // y

virtual只對pointer或reference有效(dynamic binding)

Page 14: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

14

Virtual Functions for the shape problem

Shape *s;

s = new Circle; s->draw(); // 呼叫circle的draw()

s = new Triangle; s->draw(); // 呼叫Triangle的draw()

...

Shape *s[100];

... // 設定s[0..99]分別指向哪一種物件

for (int i=0;i<100;i++)

s[i]->draw(); // 依據實際的type呼叫draw()

draw()

...()

Shape

draw()

...()

Circle

draw()

...()

Rectangle

draw()

...()

Triangle

設成virtual

function

Java ?

Page 15: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

15

Virtual Functions & Polymorphism

Polymorphism

Ability for objects of different classes to respond differently to the same function call

Implemented through virtual functions C++允許使用base class pointer(或reference)呼叫derived

class的member function

C++ chooses the correct overridden function in object

Dynamic binding Function determined during execution time

ShapePtr->Draw();

Static binding

Function determined during compile-time ShapeObject.Draw();

Pointer可能指向任何derivedObject

已知derivedObject

Page 16: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

16

Dynamic Binding Static Binding Dynamic binding (run-time) 用base class的pointer或reference呼叫virtual function

Static binding (compile time) 其他(用object呼叫virtual/non-virtual function, 用pointer或

reference呼叫non-virtual function

class X {

public:

void f1() {

cout << ”xf1”;

}

virtual void f2() {

cout << ”xf2”;

}

};

class Y : public X {

public:

void f1(){cout<<”yf1”;}

void f2(){cout<<”yf2”;}

};

X a;

a.f1(); // xf1 static

a.f2(); // xf2 static

Y b;

b.f1(); // yf1 static

b.f2(); // yf2 static

X *p=&a;

p->f1(); // xf1 static

p->f2(); // xf2 dynamic

X *pb=&b;

pb->f1(); // xf1 static

pb->f2(); // yf2 dynamic

Page 17: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

17

Abstract and Concrete Classes

Abstract classes To provide a base class for other classes

No objects can be instantiated

Declare one or more pure virtual functions

Example virtual void draw() = 0;

Concrete classes Objects can be instantiated

No unimplemented virtual functions Implement all unimplemented virtual functions

Page 18: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

18

Abstract and Concrete Classes

draw() setColor() ...()

Shape

draw() ...()

Rectangle

C++ example class Shape { public: virtual void draw() = 0; void setColor(int c) { color = c; } private: int color; }; class Rectangle : public Shape { public: void draw() {...}; void getArea() {...}; virtual void f() {...}; };

Abstract class

Concrete class

Not pure

virtual

virtual is inherited

Page 19: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

19

Abstract and Concrete Classes UML example

draw()

setColor()

...()

Shape

draw()

...()

Circle

draw()

...()

Rectangle

draw()

...()

Triangle

斜體字

pure virtual function

斜體字abstract

class

正體字concrete class

正體字

有implementation

Page 20: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

20

Virtual Destructors

Problem: How to delete an object with a base class pointer?

Solution: declare a virtual (base-class) destructor

class X {

public:

virtual ~X(); // virtual destructor

...

private:

...

};

class Y : public X {

...

};

X *pa = new Y;

...

// use *pa

...

delete pa;

Page 21: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

21

How does compiler handle virtual functions?

virtual functions

overheads?

memory

speed

How it works?

dynamic binding

vtable

Point

Circle

Cylinder

Shape

Abstract

class

Page 22: Problem: Given a vector of pointers to Shape objects, print the …yccheng/oop2017s/Polymorphism... · 2017. 6. 5. · 15 Virtual Functions & Polymorphism Polymorphism Ability for

22

vir

tual fu

nctio

ns

每個class都有一個vtable

vtable

每個object都帶著

vtable的pointer

array

bp->PSN(); fu

nctio

ns

area()

volume()

PSN()

print()

Shape

PSN()

print()

x

y

Point

area()

PSN()

print()

radius

Circle

area()

volume()

PSN()

print()

height

Cylinder