CSC241 Object-Oriented Programming (OOP) Lecture No. 17.

Slides:



Advertisements
Similar presentations
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
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,
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
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.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Cpt S 122 – Data Structures Polymorphism
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.
Polymorphism &Virtual Functions
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Polymorphism Polymorphism occurs with objects instantiated from a set of classes related by inheritance to the same ancestor class. –Each class provides.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
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.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Overview of C++ Polymorphism
Variations on Inheritance Object-Oriented Programming Spring
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Virtual Functions & Polymorphism. Geometric Object Example Function also available in Circle & Rectagle Circle & Rectangle could use toString() but chose.
Computer Science Department Inheritance & Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
CMSC 202 Polymorphism.
7. Inheritance and Polymorphism
Inheritance and Big O And your first reading assignment
Class A { public : Int x; A()
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Object-Oriented Programming
Polymorphism.
CS212: Object Oriented Analysis and Design
Inheritance & Polymorphism
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 20- Virtual Functions and Polymorphism
Polymorphism 1 CMSC 202.
Inheritance: Polymorphism and Virtual Functions
Programming II Polymorphism A.AlOsaimi.
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.
Chapter 20 - C++ Virtual Functions and Polymorphism
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheritance: Polymorphism and Virtual Functions
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
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.
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 17

Abstract Class Shape LineCircleTriangle draw calcArea draw calcArea draw calcArea draw calcArea

Abstract Class  Implements an abstract concept  Cannot be instantiated  Used for inheriting interface and/or implementation

Concrete Class  Implements a concrete concept  Can be instantiated  May inherit from an abstract class or another concrete class

Abstract Classes in C++  In C++, we can make a class abstract by making its function(s) pure virtual  Conversely, a class with no pure virtual function is a concrete class

Pure Virtual Functions function  A pure virtual represents an abstract behavior and therefore may not have its implementation (body)  A function is declared pure virtual by following its header with “= 0” virtual void draw() = 0;

… Pure Virtual Functions  A class having pure virtual function(s) becomes abstract class Shape { … public: virtual void draw() = 0; }; … Shape s;//Error!

… Pure Virtual Functions  A derived class of an abstract class remains abstract until it provides implementation for all pure virtual functions

Shape Hierarchy Shape LineCircleQuadrilateral draw = 0 draw Rectangle draw

… Pure Virtual Functions class Quadrilateral : public Shape { … // No overriding draw() method }; … Quadrilateral q; // Error!

… Pure Virtual Functions class Rectangle :public Quadrilateral{ … public: // void draw() virtual void draw() { … // function body } }; … Rectangle r;// OK

Virtual Destructors class Shape { … public: ~Shape() { cout << "Shape destructor called\n"; } };

…Virtual Destructors class Quadrilateral : public Shape { … public: ~Quadrilateral() { cout << "Quadrilateral destructor called\n"; }

…Virtual Destructors class Rectangle : public Quadrilateral { … public: ~Rectangle() { cout << "Rectangle destructor called\n"; }

…Virtual Destructors  When delete operator is applied to a base class pointer, base class destructor is called regardless of the object type

…Virtual Destructors int main() { Shape* pShape = new Rectangle(); delete pShape; return 0; }  Output Shape destructor called

Result Shape Part Quad Part Rect Part pShape Quad Part Rect Part Before After pShape

Virtual Destructors  Make the base class destructor virtual class Shape { … public: virtual ~Shape() { cout << "Shape destructor called\n"; }

…Virtual Destructors class Quadrilateral : public Shape { … public: virtual ~Quadrilateral() { cout << "Quadrilateral destructor called\n"; }

…Virtual Destructors class Rectangle : public Quadrilateral { … public: virtual ~Rectangle() { cout << "Rectangle destructor called\n"; }

…Virtual Destructors  Now base class destructor will run after the derived class destructor

…Virtual Destructors int main() { Shape* pShape = new Rectangle(); delete pShape; return 0; }  Output Rectangle destructor called Quadilateral destructor called Shape destructor called

Result Shape Part Quad Part Rect Part pShape Before After pShape

Virtual Functions – Usage  Inherit interface and implementation  Just inherit interface (Pure Virtual)

Inherit interface and implementation Shape LineCircleTriangle draw = 0 calcArea draw calcArea draw calcArea

…Inherit interface and implementation class Shape { … virtual void draw() = 0; virtual float calcArea() { return 0; }

…Inherit interface and implementation  Each derived class of Shape inherits default implementation of calcArea()  Some may override this, such as Circle and Triangle  Others may not, such as Point and Line

…Inherit interface and implementation  Each derived class of Shape inherits interface (prototype) of draw()  Each concrete derived class has to provide body of draw() by overriding it

V Table  Compiler builds a virtual function table (vTable) for each class having virtual functions  A vTable contains a pointer for each virtual function

Example – V Table int main() { Point p1(10, 10), p2(30, 30); Shape* pShape; pShape = new Line(p1, p2); pShape->draw(); pShape->calcArea(); }

Example – V Table calcArea draw … Shape vTable draw… Line vTable calcArea Line object 0 pShape Shape … point1 = p1 point2 = p2

Dynamic Dispatch  For non-virtual functions, compiler just generates code to call the function  In case of virtual functions, compiler generates code to  access the object  access the associated vTable  call the appropriate function

Conclusion  Polymorphism adds  Memory overhead due to vTables  Processing overhead due to extra pointer manipulation  However, this overhead is acceptable for many of the applications  Moral: “Think about performance requirements before making a function virtual”