Programming II Polymorphism A.AlOsaimi.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
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.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
Overview of C++ Polymorphism
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
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.
A First Book of C++ Chapter 12 Extending Your Classes.
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,
CMSC 202 Polymorphism.
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming
Polymorphism.
Polymorphism & Virtual Functions
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Object Oriented Programming
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
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.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 9 Carrano Chapter 10 Small Java
Overview of C++ Polymorphism
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Lecture 6: Polymorphism
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.
Computer Science II for Majors
Presentation transcript:

Programming II Polymorphism A.AlOsaimi

Poly = Many Morphism = Forms Polymorphism A.AlOsaimi

Overview Method Binding: Substitution Principle Static Binding A more in depth discussion Method Binding: Static Binding Dynamic Binding (Polymorphism) Syntax on virtual method A.AlOsaimi

Interacting with Objects in C++ There are 3 ways to refer to an object in C++: A.AlOsaimi

Subclass Substitution Principle C++ allows superclass pointers to point to sublass objects. Object pointer and reference of class type A: Can refer to objects of type A Can also refer to objects of subclass of A 4 400.40 0.04 A.AlOsaimi

Subclass Substitution Principle Pointers to subClasses Let we have – class B_Class{ … }; class D_Class: public B_Class{ … }; Then we can write – B_Class *p1; // pointer to object of type B_Class D_Class d_obj; // object of type D_Class Both statement are valid: p1 = &d_obj; B_Class *p2 = new D_Class; Using a base class pointer (pointing to a derived class object) we can access only those members of the derived object that were inherited from the base.

Polymorphism: Basic Idea Since we know: A superclass pointer/reference can refer to an object of subclass A method implementation can be overridden in the subclass, resulting in multiple versions Question: If we invoke a method using pointer/reference, which version of the method should be invoked? C++ provides two possibilities: Static Binding Dynamic Binding: Also known as polymorphism A.AlOsaimi

Static Binding Use the class type of the pointer/reference to determine which version of method to call: This information is known during compilation time A.AlOsaimi

Dynamic Binding (Polymorphism) Use the actual class type of the object to determine which version of method to call: This information is known only during run time A.AlOsaimi

Dynamic Binding: Syntax To enable dynamic binding of a method in C++, add the "virtual" keyword before the method declaration Once a method is declared as virtual: it will remain so in all descendant classes no need to restate the "virtual" keyword A.AlOsaimi

Static VS Dynamic Binding: illustration Static Binding: The class type R of pointer or reference is used to determine the method to call Dynamic Binding: The class type S of object is used to determine the method to call A.AlOsaimi

Polymorphism: Example A.AlOsaimi

Polymorphism: Advantage Make code reuse easier: If a code is written to use a virtual method of a class A, the code can work with all future subclass of A with no modification Furthermore, new/extended behavior of subclass of A can be incorporated by overriding the virtual method implementation For example: Code that uses the virtual method print() in BankAcct can work with all subclasses of BankAcct even when the print() is overridden A.AlOsaimi

Common Mistake A common error is to assume the actual type of the object is used to determine validity of method in vocation The data type of the pointer/reference is used to determine validity of method invocation The baPtr pointer has the type of BankAcct BankAcct does not have payInterest() method  error! A.AlOsaimi

Common Mistake While it is permissible for a superclass pointer to point to a subclass object, the reverse is not true. BankAcct b; SavingAcct *ps = &b; // compiler error

Polymorphism: Summary When we see the statement: refR.methodM(); OR ptrR->methodM(); At compile time: If the class of refR/ptrR does not have a method methodM() Compilation Error If methodM() is not a virtual method  static binding methodM() in class of refR/ptrR is called At run time: If methodM() is a virtual method dynamic binding methodM() in class of the actual object is called A.AlOsaimi

Pure virtual methods Pure virtual method: class Name { public: virtual returntype name(parameters) = 0; ... }; Pure virtual method: One that is declared but not implemented. A class with one or more pure virtual functions is an Abstract Class. Objects of abstract class can’t be created must be implemented by subclasses (else they will be abstract)

Abstract Classes 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; }; Shape s; // error : variable of an abstract class

Example Shape Circle Triangle + draw(): void  virtual + draw(): void

Abstract Classes 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

Example 1 #include <iostream> using namespace std; void main(){ class figure{ figure *p; protected: triangle t; double x,y; circle c; public: p= &t; void set_dim(double I, double J ) p->set_dim(10.0,5.0); { x = I;y = J;} p-> show_area(); virtual void show_area(){ p= &c; cout <<"No area computation efined"<<endl; p-> set_dim(10.0, 0); cout<<"for this class"<<endl;} }; } class triangle: public figure{ void show_area() {cout<<"show in triangle \tarea="<< x* 0.5 * y<<endl;} class circle: public figure{ {cout<< "show in circle \t area="<<x *x<<endl;} A.AlOsaimi

Example 2 Class figure{ }; Class triangle: public figure{ protected: double x,y; public: void set_dim(double I, double j=0) {x=I; Y= j; } Virtual void show_area()=0 ;// pure function }; Class triangle: public figure{ Public: Void show_area(){cout<< x* 0.5 * y;}}; Class circle: public figure{ Public: // no definiton of show_area() will case error }; Int main(){ Figure *p; Triangle t; Circle c; // illegal – can’t create P= &t; P-> set_dim(10.0,5.0); P-> show_area(); p= &c; P-> set_dim(10.0); Return0;}

Virtual Destructors Constructors cannot be virtual, but destructors can be virtual. It ensures that the derived class destructor is called when a base class pointer is used while deleting a dynamically created derived class object.

Virtual Destructors (contd.) Using non-virtual destructor #include <iostream> using namespace std; class base { public: ~base() { cout << "destructing base"<<endl; }}; class derived : public base { ~derived() {cout << "destructing derived"<<endl; }}; void main() { base *p = new derived; delete p; }

Using non-virtual destructor

Virtual Destructors (contd.) Using virtual destructor #include <iostream> using namespace std; class base { public: virtual ~base() { cout << "destructing base"<<endl; }}; class derived : public base { ~derived() {cout << "destructing derived"<<endl; }}; void main() { base *p = new derived; delete p; }

Virtual Destructors (contd.) Using virtual destructor

References http://www.comp.nus.edu.sg/~cs2103/AY1314S2/files/Implementing%20polymorphism.pdf A.AlOsaimi