30
1/30 Chapter 8: Dynamic Binding And Abstract classes

Chapter 8: Dynamic Binding And Abstract classes

  • Upload
    maire

  • View
    34

  • Download
    3

Embed Size (px)

DESCRIPTION

Chapter 8: Dynamic Binding And Abstract classes. Objectives. What is static binding? What is Dynamic binding? Restriction of overriding methods Virtual method Virtual function table Type conformity Abstract classes A demonstration. 7.1- Static binding. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 8: Dynamic Binding And Abstract classes

1/30

Chapter 8:Dynamic Binding

AndAbstract classes

Page 2: Chapter 8: Dynamic Binding And Abstract classes

2/30

Objectives

• What is static binding?• What is Dynamic binding?• Restriction of overriding methods• Virtual method• Virtual function table• Type conformity• Abstract classes• A demonstration

Page 3: Chapter 8: Dynamic Binding And Abstract classes

3/30

7.1- Static binding

5

CircleConstructor

class Circle{ int x,y,r; public: Circle (int xx, int yy, int rr) { x=xx; y=yy; r=rr; } void print() { cout <<x<<y<<r; }};void main(){ Circle c(3,4,5); c.print(); }

43

Circleprint()

main()[180,65520][120,65520]

800

120

180

65520 c

Data is associated with corresponding code at compile-time

Page 4: Chapter 8: Dynamic Binding And Abstract classes

4/30

7.2- Dynamic binding

pc

CircleConstructor

class Circle{ int x,y,r; public: Circle (int xx, int yy, int rr) { x=xx; y=yy; r=rr; } void print() { cout <<x<<y<<r; }};void main(){ Circle *pc; pc= new Circle(3,4,5); pc->print(); }

Circleprint()

main()[180,[65540]][120,[65540]]

800

120

180

65540

Data is associated with corresponding code at run-time

5

CircleConstructor

43

Circleprint()

main()[180,1200][120,1200]

1200

1200

Page 5: Chapter 8: Dynamic Binding And Abstract classes

5/30

7.3- Restriction of overriding Functions

Perhaps, you want to see “Son” on the screen.

Page 6: Chapter 8: Dynamic Binding And Abstract classes

6/30

7.4- Virtual Functions- Polymorphism

Polymorphism ability occurs only when you use a pointer

to an object and used-methods of classes

are virtual methods

virtual ReturnTypeor

ReturnType virtualare accepted

Page 7: Chapter 8: Dynamic Binding And Abstract classes

7/30

7.5- Virtual Function Table

Nephew::print()

Son::print()

Father::print()

[print, 600][…, …]

600

500

400

VFT- Nephew

VFT- Son

VFT- Father

[print, 500][…, …]

[print, 400][…, …]

main()[600, dataX][500,dataX][400,dataX]

800

Page 8: Chapter 8: Dynamic Binding And Abstract classes

8/30

7.6- Type ConformityPointer of base class

can point to an subclass

object

DownType casting

OK

Page 9: Chapter 8: Dynamic Binding And Abstract classes

9/30

Type conformity….

Error: Can not convert“Point2*” to “Poỉnt3*”

OppositeType casting

NO OK

Page 10: Chapter 8: Dynamic Binding And Abstract classes

10/30

Type conformity….

ExplicitType casting

OK

Page 11: Chapter 8: Dynamic Binding And Abstract classes

11/30

Type conformity…

Two classes have no relation.

Explicit type casting OK

Page 12: Chapter 8: Dynamic Binding And Abstract classes

12/30

7.7- Abstract class

• Result of so-high generation

class Circleint x,y,r;void print()double area()double perimeter()

class Rectangleint x1,y1,x2,y2;void print()double area()double perimeter()

class Triangleint x1,y1,x2,y2,x3,y3;void print()double area()double perimeter()

class Shapevoid print()double area()double perimeter()

How will we implementthese methods?

Pure virtualmethods

Page 13: Chapter 8: Dynamic Binding And Abstract classes

13/30

Abstract class…

• Abstract class must have at least one pure virtual method

• Pure virtual method is a method with no body.• Syntax of pure virtual method:

virtual DataType Method (…) = 0;• You cannot create an object of abstract class but

you can declare a pointer of abstract class.• This pointer must be point to an object of a

concrete class.

Page 14: Chapter 8: Dynamic Binding And Abstract classes

14/30

Abstract class….

Page 15: Chapter 8: Dynamic Binding And Abstract classes

15/30

Abstract subclass

Error: Cannot create instanceof abstract class ‘B’

• A is an abstract class • B is public subclass of A • In B, the inherited method MA() is not overriden yet B is abstract class

Page 16: Chapter 8: Dynamic Binding And Abstract classes

16/30

Abstract subclass…

Subclass of a concrete classmay be an abstract class.

Error: Cannot create instanceof abstract class ‘B’

Page 17: Chapter 8: Dynamic Binding And Abstract classes

17/30

7.8-A Demonstration

• The following program depicts using abstract class.

• People generate all concrete classes as Circle, Rectangle,… into Shape class.

• User will input some shape details

• Program will print out details of all shape

• Values of area and perimeter of each shape will be printed also.

Page 18: Chapter 8: Dynamic Binding And Abstract classes

18/30

Class Shape and Circle

Page 19: Chapter 8: Dynamic Binding And Abstract classes

19/30

Class Rectangle

Page 20: Chapter 8: Dynamic Binding And Abstract classes

20/30

Class ShapeList

Page 21: Chapter 8: Dynamic Binding And Abstract classes

21/30

Class ShapeList…, main(), Result

Page 22: Chapter 8: Dynamic Binding And Abstract classes

22/30

7.9- Class Object elements and Class Vector for arbitrary elements

Page 23: Chapter 8: Dynamic Binding And Abstract classes

23/30

7.9- …

Page 24: Chapter 8: Dynamic Binding And Abstract classes

24/30

7.9- Class Student:public Object

Page 25: Chapter 8: Dynamic Binding And Abstract classes

25/30

7.9- Class Circle:public Object

Page 26: Chapter 8: Dynamic Binding And Abstract classes

26/30

7.9- Main

Page 27: Chapter 8: Dynamic Binding And Abstract classes

27/30

Summary

• Virtual Method is a way to make polymorphism.• Syntax for virtual method: virtual ReturnType Method (parameters) ReturnType virtual Method (parameters)• Compiler will determine the right method will be

called using a virtual function table for every class which contains virtual methods.

• Pure virtual method is a virtual method but it has no code.

• Syntax for pure virtual method: virtual ReturnType Method (parameters)=0;

Page 28: Chapter 8: Dynamic Binding And Abstract classes

28/30

Summary

• Abstract class is a result of so-high generation.

• Abstract class must have at least one pure virtual method.

• You can not create an object of abstract class but you can declare a pointer to it then, it points to an object of a concrete subclass.

Page 29: Chapter 8: Dynamic Binding And Abstract classes

29/30

Exercises

• Using the class Object, implement classes: Father, Mother, Son, Daughter.

• Write a program will – Input a list of members in a family. Store them

into a Vector object.– Print out members of the family.

Page 30: Chapter 8: Dynamic Binding And Abstract classes

30/30

Thanks