Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.

Similar presentations


Presentation on theme: "Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes."— Presentation transcript:

1 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes

2 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 2 Virtual functions and polymorphism are mechanism that produce better programming design. Systems become easily extensible without making too many modification to the source code New classes can be added to the software with little or no modification to the generic part of the program Virtual function and polymorphism can increase the reliability of the software The less is the amount of the modified code, the less is the possibility of making mistake. Thus, the higher is the reliability of the software

3 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 3 Overriding the Functions Before we describe the virtual function and polymorphism, we need to define the concept of function overriding. A derived class can override a method (function) that has inherited from the base class. A function is said to be overridden, if it is re-implemented in the derived class. Consider the following class hierarchy: Shape CircleSquare

4 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 4 A simple interface for these three classes can be: class Shape { public: Shape(); int visible(); Color get_color(); void draw();... }; class Circle : public Shape { public: Circle(); void draw(); …... }; class Square : public Shape { public: Square(); void draw(); …... };

5 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 5 Note that the draw() function of the Shape class is overridden both in the Square class and Circle class. Now consider the following driver main program: void main() { Shape sh, *shptr; Circle c; Squaresq; sh.draw();// calls the draw function in Shape class c.draw();// calls the draw function in Circle class sq.draw();// calls the draw function in Square class shptr = &c; shptr->draw();// calls the draw function in Shape class shptr = &sq; shptr->draw();// calls the draw function in Shape class } shptr->draw() is the same as (*shptr).draw

6 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 6 In the statement shptr = &c although shptr is pointing to the address of an object of Circle class, the statement shptr->draw() still calls the draw function of the base class Shape. This is an example of non-polymorphic behavior.

7 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 7 Polymorphism The ability for objects of different classes related by inheritance to respond differently to the same message (i.e. member function call) In the previous example, the draw function of the base class Shape had a non-polymorphic behavior. Question: How can we change the draw function in the Shape class to have a polymorphic behavior? In another word, how can we change the draw() function such that the statement shptr->draw() calls the draw function of the Square/Circle class when shptr is pointing to an object of the Square/Circle class.

8 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 8 Answer: By making the draw function in Shape class a Virtual function as shown in the following example class Shape { public: Shape(); int visible(); Color get_color(); virtual void draw();... }; class Circle : public Shape { public: Circle(); virtual void draw(); …... }; class Square : public Shape { public: Square(); virtual void draw(); …... };

9 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 9 Now consider the following driver main program: void main() { Shape sh, *shptr; Circle c; Squaresq; sh.draw(); // calls the draw function in Shape class (static binding) c.draw(); // calls the draw function in Circle class (static binding) sq.draw(); // calls the draw function in Square class (static binding) shptr = &c; shptr->draw(); // calls the draw function in Circle class (dynamic binding) shptr = &sq; shptr->draw(); // calls the draw function in Square class (dynamic binding) }

10 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 10 Thus depending on the state(value) of the shptr, the system will decide dynamically which draw function to call. This is an example of Polymorphic behavior In C++, by default, method calls are resolved at compile time. The class method to call is the one specified by the variable’s type. This is known as static binding. Dynamic binding is when the actual method to call is not resolved at compile time, but at run time when the true type of the instantiated object is known. In C++, use the key word virtual in front of the method in the class declaration. It tells the compiler to use dynamic binding for calls to the virtual method.

11 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 11 Constructors may not be made virtual. The destructor can be made virtual, it is recommended that you make destructors virtual in base classes. Once a method is declared virtual in the base class, it remains virtual for all derived classes even if the derived classes do not explicitly declare the method virtual. There is no need to put the word “virtual” in front of the method in the subclass. Though for clarity it’s recommended that you do.

12 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 12 With virtual functions and polymorphism, the programmer can deal in generalities and let the execution-time environment concern itself with the specifies. The programmer can command a wide variety of objects to behave in manners appropriate to those objects without even knowing the types of those objects command. Polymorphism promotes extensibility: Software written to invoke polymorphic behavior is written independently of the types of the objects to which messages are sent. Thus, new types of objects that can respond to existing messages can be added into such a system without much modifying the Except for the client code that instantiates new objects, underlying programs need not be recompiled.

13 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 13 There is a cost to dynamic binding. Each class with a virtual method keeps track of a virtual table. The virtual table contains the one entry for the class type and the other is the address of the virtual method for that class. For every call to a virtual function, there is a table look up to determine the function to call. The cost is slight speed loss, but the benefits are huge.

14 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 14 Virtual Destructor If an object is destroyed explicitly by applying the delete operator to a base class pointer to the object to the object, the base class destructor is called on the object But by declaring base-class destructor virtual, all derived class destructors become virtual even though they do not have the same name as the base class destructor. Thus if an object of a class in the hierarchy is destroyed explicitly by applying the delete operator to a base class pointer that points to a derived class object, the destructor for the appropriate derived class is called

15 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 15 Abstract base classes Sometimes a class is defined for the sole purpose of keeping track of common members of the derived classes, common methods that will not be specialized, and to specify common behavior which the base class cannot implement, but specialized classes must implement. For example, it is silly to have the draw function for the “Shape” class because an object of the class shape does not know what shape to draw.

16 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 16 Yet we need the “Shape” class to keep track of a graphic shape’s (x,y) origin, color and other common shape attributes and behavior. class Shape { public: Shape(); // special classes do not need to specialize the following two methods int visible(); Color get_color(); // each sub-class has its own unique way of drawing itself virtual void draw();... protected: intvisible; intoX;// origin x,y intoY; };

17 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 17 Abstract class is used to specify an interface that a derived class must implement, but you can’t create an object of an abstract class. For example, by making the “Shape” class abstract, a programmer cannot create a general “Shape” by accident. i.e. : It is an error to create an object of type Shape However, you may create pointers to objects of abstract class You may also create objects of the subclasses of an abstract class. To make a class abstract, first make a method, any method other the constructor, virtual and append =0 after the full method signature, but before an inline implementation. This makes your method “pure virtual”

18 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 18 class Shape { public: Shape(); int visible(); // special classes do not need to specialize this behavior Color get_color(); virtual void draw()=0; // a sub-class must implement the // method, or it too is abstract... }; class Square : public Shape { public: Square(); virtual void draw();// provide specialized behavior for general …...// interface, remove the =0 syntax };

19 Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 19 Once one or more methods are declared pure virtual with the “=0” syntax, the class becomes abstract. A method implementation need not be provided for the pure virtual methods. However, specialized classes must implement these methods. If a subclass does not remove the “=0” from the pure virtual method, it remains pure virtual and the subclass also become abstract. If the ‘=0´is removed, the method must be implemented or you get a linker error.


Download ppt "Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes."

Similar presentations


Ads by Google