Lecture 6: Polymorphism

Slides:



Advertisements
Similar presentations
1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept.
Advertisements

Inheritance and Composition (aka containment) Textbook Chapter –
1 Another Way to Define A Class - Inheritance. 2 Inheritance Concept Rectangle Triangle Polygon class Polygon { private: int width, length; public: void.
1 Chapter 6 Object-Oriented Software Development.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
Chapter 12: Adding Functionality to Your Classes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
CMSC 202 Inheritance.
1 Chapter 14 Object-Oriented Software Development.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
Chapter 10 Inheritance and Polymorphism
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
10/18/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #02:
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
1 Another way to define a class Inheritance..!!. 2 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining.
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Computer Science Department Inheritance & Polymorphism.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Concept Polygon Rectangle Triangle class Rectangle{
CMSC 202 Polymorphism.
Inheritance Saras M. Srivastava LEARN BY EXAMPLES PGT – Comp. Sc.
Inheritance CMSC 202, Version 4/02.
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
Polymorphism.
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Review: Two Programming Paradigms
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Programming Techniques Course
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 14 Object-Oriented Software Development
Another way to define a class
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.
Presentation transcript:

Lecture 6: Polymorphism - The fourth pillar of OOP -

Object-Oriented Concept Encapsulation Abstract Data Type (ADT), Object Inheritance Derived object Polymorphism Each object knows what it is

Polymorphism – An Introduction Definition noun, the quality or state of being able to assume different forms - Webster An essential feature of an OO Language It builds upon Inheritance

Before We Proceed… Inheritance – Basic Concepts Class Hierarchy Code Reuse, Easy to maintain Type of inheritance : public, protected, private Function overriding

class Time Specification // SPECIFICATION FILE ( time.h) class Time { public : void Set ( int h, int m, int s ) ; void Increment ( ) ; void Write ( ) const ; Time (int initH, int initM, int initS ) ; Time ( ) ; protected : int hrs ; int mins ; int secs ; } ;

Derived Class ExtTime // SPECIFICATION FILE ( exttime.h) #include “time.h” enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ; class ExtTime : public Time { public : void Set ( int h, int m, int s, ZoneType timeZone ) ; void Write ( ) const; //overridden ExtTime (int initH, int initM, int initS, ZoneType initZone ) ; ExtTime (); private : ZoneType zone ; // added data member } ;

Class Interface Diagram ExtTime class Set Set Protected data: hrs mins secs Increment Increment Write Write ExtTime Time ExtTime Time Private data: zone

Why Polymorphism?--Review: Time and ExtTime Example by Inheritance void Print (Time someTime ) //pass an object by value { cout << “Time is “ ; someTime.Write ( ) ; cout << endl ; } CLIENT CODE Time startTime ( 8, 30, 0 ) ; ExtTime endTime (10, 45, 0, CST) ; Print ( startTime ) ; Print ( endTime ) ; // Time :: write() OUTPUT Time is 08:30:00 Time is 10:45:00

Static Binding When the type of a formal parameter is a parent class, the argument used can be: the same type as the formal parameter, or, any derived class type. Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter When pass-by-value is used, static binding occurs Static binding: compile time Dynamic binding: run time 3. Animal a = new Animal(); a.Roar(); // The compiler can resolve this method call statically. Dynamic binding means that references are resolved at run time. public void MakeSomeNoise(object a) { // Things happen... ((Animal) a).Roar(); // You won't know if this works until runtime! }

Can We Do Better? // Time :: write() CLIENT CODE OUTPUT void Print (Time someTime ) //pass an object by value { cout << “Time is “ ; someTime.Write ( ) ; cout << endl ; } CLIENT CODE Time startTime ( 8, 30, 0 ) ; ExtTime endTime (10, 45, 0, CST) ; Print ( startTime ) ; Print ( endTime ) ; // Time :: write() OUTPUT Time is 08:30:00 Time is 10:45:00

Polymorphism – An Introduction Definition noun, the quality or state of being able to assume different forms - Webster An essential feature of an OO Language It builds upon Inheritance Allows run-time interpretation of object type for a given class hierarchy Also Known as “Late Binding” Implemented in C++ using virtual functions 4. pass-by-pointers a) int add(int x, int y) {} Int multiply(int x, int y) (I) int main() { int nX; cout << "Enter a number: "; cin >> nX; Int result; If(nx%2 == 0) result = add(nx, nx); } Else Result = multiply(nx, nx); (II) Int (*func)(int, int); //created a function pointer Func = add; Func = multiply; Int result = func(nx, nx); 5. Less efficient, cause indirect call, but it’s more flexible

Dynamic Binding Is the run-time determination of which function to call for a particular object of a derived class based on the type of the argument Declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding Dynamic binding requires pass-by-reference

Virtual Member Function // SPECIFICATION FILE ( time.h ) class Time { public : . . . virtual void Write ( ) ; // for dynamic binding virtual ~Time(); // destructor private : int hrs ; int mins ; int secs ; } ;

This is the way we like to see… void Print (Time * someTime ) { cout << “Time is “ ; someTime->Write ( ) ; cout << endl ; } OUTPUT Time is 08:30:00 Time is 10:45:00 CST CLIENT CODE Time startTime( 8, 30, 0 ) ; ExtTime endTime(10, 45, 0, CST) ; Time *timeptr; timeptr = &startTime; Print ( timeptr ) ; timeptr = &endTime; Time::write() ExtTime::write()

Virtual Functions Virtual Functions overcome the problem of run time object determination Keyword virtual instructs the compiler to use late binding and delay the object interpretation How ? Define a virtual function in the base class. The word virtual appears only in the base class If a base class declares a virtual function, it must implement that function, even if the body is empty Virtual function in base class stays virtual in all the derived classes It can be overridden in the derived classes But, a derived class is not required to re-implement a virtual function. If it does not, the base class version is used it knows which function to call based on the actual type of the object

Polymorphism Summary When you use virtual functions, compiler store additional information about the types of object available and created Polymorphism is supported at this additional overhead Important : virtual functions work only with pointers/references Not with objects even if the function is virtual If a class declares any virtual methods, the destructor of the class should be declared as virtual as well.

Abstract Classes & Pure Virtual Functions Some classes exist logically but not physically. Example : Shape Shape s; // Legal but silly..!! : “Shapeless shape” Shape makes sense only as a base of some classes derived from it. Serves as a “category” Hence instantiation of such a class must be prevented class Shape //Abstract { public : //Pure virtual Function virtual void draw() = 0; } A class with one or more pure virtual functions is an Abstract Class Objects of abstract class can’t be created Shape s; // error : variable of an abstract class

Example Shape Circle Triangle virtual void draw() public void draw()

Hence derived class also becomes abstract A pure virtual function not defined in the derived class remains a pure virtual function. Hence derived class also becomes abstract class Circle : public Shape { //No draw() - Abstract public : void print(){ cout << “I am a circle” << endl; } class Rectangle : public Shape { public : void draw(){ // Override Shape::draw() cout << “Drawing Rectangle” << endl; } Rectangle r; // Valid Circle c; // error : variable of an abstract class

Pure Virtual Functions: Summary Pure virtual functions are useful because they make explicit the abstractness of a class Tell both the user and the compiler how it was intended to be used Note : It is a good idea to keep the common code as close as possible to the root of you hierarchy

Summary – Cont’d It is still possible to provide definition of a pure virtual function in the base class The class still remains abstract and functions must be redefined in the derived classes, but a common piece of code can be kept there to facilitate reuse In this case, they can not be declared inline class Shape { //Abstract public : virtual void draw() = 0; }; // OK, not defined inline void Shape::draw(){ cout << “Shape" << endl; } class Rectangle : public Shape { public : void draw(){ Shape::draw(); //Reuse cout <<“Rectangle”<< endl; } 9/11/2019

Take Home Message Polymorphism is built upon class inheritance It allows different versions of a function to be called in the same manner, with some overhead Polymorphism is implemented with virtual functions, and requires pass-by-reference