1 / 30

Chapter 8: Dynamic Binding And Abstract classes

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.

maire
Download Presentation

Chapter 8: Dynamic Binding And Abstract classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8:Dynamic BindingAndAbstract classes

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

  3. 7.1- Static binding Data is associated with corresponding code at compile-time 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(); } 5 4 65520 c 3 main() [180,65520] [120,65520] 800 Circle print() 180 Circle Constructor 120

  4. 7.2- Dynamic binding Data is associated with corresponding code at run-time 65540 pc 1200 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(); } 5 4 1200 3 main() [180,[65540]] [120,[65540]] main() [180,1200] [120,1200] 800 Circle print() Circle print() 180 Circle Constructor Circle Constructor 120

  5. 7.3- Restriction of overriding Functions Perhaps, you want to see “Son” on the screen.

  6. 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 ReturnType or ReturnType virtual are accepted

  7. main() [600, dataX] [500,dataX] [400,dataX] 800 600 Nephew::print() 500 Son::print() 400 Father::print() [print, 600] […, …] VFT- Nephew [print, 500] […, …] VFT- Son [print, 400] […, …] VFT- Father 7.5- Virtual Function Table

  8. Pointer of base class can point to an subclass object 7.6- Type Conformity Down Type casting OK

  9. Type conformity…. Error: Can not convert “Point2*” to “Poỉnt3*” Opposite Type casting NO OK

  10. Type conformity…. Explicit Type casting OK

  11. Type conformity… Two classes have no relation. Explicit type casting  OK

  12. 7.7- Abstract class • Result of so-high generation class Shape void print() double area() double perimeter() Pure virtual methods How will we implement these methods? class Circle int x,y,r; void print() double area() double perimeter() class Rectangle int x1,y1,x2,y2; void print() double area() double perimeter() class Triangle int x1,y1,x2,y2,x3,y3; void print() double area() double perimeter()

  13. 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.

  14. Abstract class….

  15. Abstract subclass • 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 Error: Cannot create instance of abstract class ‘B’

  16. Subclass of a concrete class may be an abstract class. Error: Cannot create instance of abstract class ‘B’ Abstract subclass…

  17. 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.

  18. Class Shape and Circle

  19. Class Rectangle

  20. Class ShapeList

  21. Class ShapeList…, main(), Result

  22. 7.9- Class Object elements and Class Vector for arbitrary elements

  23. 7.9- …

  24. 7.9- Class Student:public Object

  25. 7.9- Class Circle:public Object

  26. 7.9- Main

  27. 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;

  28. 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.

  29. 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.

  30. Thanks

More Related