Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.

Similar presentations


Presentation on theme: "Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more."— Presentation transcript:

1 Object-Oriented Programming: Polymorphism 1

2 OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more extensible and maintainable. To declare and use virtual functions to effect polymorphism. The distinction between abstract and concrete classes. To declare pure virtual functions to create abstract classes. How C++ implements virtual functions and dynamic binding "under the hood." How to use virtual destructors to ensure that all appropriate destructors run on an object 2

3 Pointers to base class 3

4 Introduction Polymorphism enables us to "program in the general" rather than "program in the specific.“ In particular, polymorphism enables us to write programs that process objects of classes that are part of the same class hierarchy as if they are all objects of the hierarchy's base class. 4

5 Introduction (cont’d) With polymorphism, we can design and implement systems that are easily extensible new classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes generically. 5

6 Introduction (cont’d) it is recommended that you have a proper understanding of pointers and class inheritance. 6

7 Virtual members A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual: class Base-class { protected: public: virtual type-in-derived-class function-in-derived-class() { return (0); } }; 7

8 Virtual members cont’d cout << object-to-derived-class.function-in- derived-class() << endl; Rewriting cout Virtual-function-in- derived-or-base-class() << endl; 8

9 9 Example

10 Note The member function area() has been declared as virtual in the base class because it will be redefined in each derived class. If the virtual keyword is removed from the declaration of area() within base class, and then the program is implemented, the result will be 0 for the three polygons instead of 20, 10 and 0. A class that declared or inheritd a virtual function is called a polymorphic class. 10

11 Abstract Classes” الأصناف المجردة أو المثالية ” and Pure virtual Functions A class is made abstract by declaring one or more of its virtual functions to be "pure." A pure virtual function is specified by placing "= 0" in its declaration, as in: Example: virtual void draw() const = 0; // pure virtual function The "=0" is known as a pure specifier. Pure virtual functions do not provide implementations. 11

12 Abstract Classes and Pure virtual Functions (cont’d) The difference between a virtual function and a pure virtual function is that a virtual function has an implementation and gives the derived class the option of overriding the function; by contrast, a pure virtual function does not provide an implementation and requires the derived class to override the function. Pure virtual functions are used when it does not make sense for the base class to have an implementation of a function, but the programmer wants all concrete derived classes to implement the function. 12

13 Abstract base classes cont’d // abstract class Cpolygon class Cpolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area () =0; }; This type of function is called a pure virtual function, and all classes that contain at least one pure virtual function are abstract base classes 13

14 Abstract base classes cont’d But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like: CPolygon poly; would not be valid for the abstract base class we have just declared, because tries to instantiate an object. Nevertheless, the following pointers: CPolygon * ppoly1; CPolygon * ppoly2; would be perfectly valid. 14 In main function

15 15 Example

16 Self study Case Study: Payroll System Using Polymorphism Virtual Destructors 16

17 E-Books & Links C++ How to Program, By H. M. Deitel - Deitel & Associates, Inc., P. J. Deitel - Deitel & Associates. http://www.cplusplus.com/doc/tutorial/poly morphism/ http://www.cplusplus.com/doc/tutorial/poly morphism/ A Complete Guide to Programming in C++, Ulla Kirch-Prinz, Peter Prinz, JONES AND BARTLETT PUBLISHERS. 17


Download ppt "Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more."

Similar presentations


Ads by Google