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

Slides:



Advertisements
Similar presentations
Inheritance Inheritance Reserved word protected Reserved word super
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance, Polymorphism, and Virtual Functions
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Programming
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Classes, Interfaces and Packages
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
7. Inheritance and Polymorphism
Object-Oriented Programming
Polymorphism.
Inheritance and Run time Polymorphism
Polymorphism & Virtual Functions
Lecture 23 Polymorphism Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

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

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

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

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(); …... };

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

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.

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.

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(); …... };

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) }

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.

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.

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.

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.

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

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.

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; };

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”

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 };

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.