330 likes | 557 Views
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.
E N D
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 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
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
7.3- Restriction of overriding Functions Perhaps, you want to see “Son” on the screen.
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
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
Pointer of base class can point to an subclass object 7.6- Type Conformity Down Type casting OK
Type conformity…. Error: Can not convert “Point2*” to “Poỉnt3*” Opposite Type casting NO OK
Type conformity…. Explicit Type casting OK
Type conformity… Two classes have no relation. Explicit type casting OK
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()
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.
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’
Subclass of a concrete class may be an abstract class. Error: Cannot create instance of abstract class ‘B’ Abstract subclass…
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.
7.9- Class Object elements and Class Vector for arbitrary elements
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;
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.
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.