Polymorphism and Virtual Functions

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
Polymorphism and Virtual Functions. class Ball : public Sphere.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Virtual Functions Fall 2008 Dr. David A. Gaitros
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Polymorphism and Virtual Functions
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.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
Slide 1 Polymorphism and Virtual Functions. Slide 2 Learning Objectives  Virtual Function Basics  Late binding  Implementing virtual functions  When.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
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 -
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 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.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Function and Polymorphism
CMSC 202 Polymorphism.
Polymorphism and Virtual Functions
Chapter 15 Inheritance. Chapter 15 Inheritance.
Polymorphism, Virtual Methods and Abstract Classes
Advanced Program Design with C++
Class A { public : Int x; A()
Polymorphism.
Polymorphism & Virtual Functions
Advanced C++ Topics Chapter 8.
CS250 Introduction to Computer Science II
Polymorphism Lec
Figure 8.1 Inheritance: Relationships among timepieces.
Learning Objectives Inheritance Virtual Function.
Polymorphism and access control
Advanced Java Topics Chapter 9
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism 1 CMSC 202.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Polymorphism - Greek for “many forms”
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
COP 3330 Object-oriented Programming in C++
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.
Computer Science II for Majors
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Polymorphism and Virtual Functions

class Ball : public Sphere

Problems in Function Redefinition int main() { Sphere mySphere(); Ball myBall(); Sphere *spherePtr; spherePtr = &mySphere; spherePtr->displayStatistics(); … Sphere’s version of displayStatistics() … spherePtr = &myBall; spherePtr->displayStatistics(); } Still Sphere’s version of displayStatistics()

Virtual Functions Virtual functions are the answer Tells compiler: “Wait until used in program” “Then get implementation from object instance” Called late binding or dynamic binding Virtual functions implement late binding Class that define virtual functions are extensible

Polymorphism Polymorphism refers to the ability to associate many meanings to one function by means of the late-binding mechanism. Thus, polymorphism, late binging, and virtual functions are really all the same topic.

Use Virtual Functions class Sphere { public: virtual void displayStatistics(){ cout << “It is Sphere\n”; } }; class Ball { void displayStatistics(){ cout << “It is Ball\n”; int main() { Ball myBall(); Sphere *spherePtr; spherePtr = &myBall; spherePtr-> displayStatistics(); } OUTPUT: It is Ball

Virtual: How? To write C++ programs: Assume it happens by ‘magic’! But explanation involves late binding Virtual functions implement late binding Tells compiler to ‘wait’ until function is used in program Decide which definition to use based on calling object Very important OOP principle

Overriding Virtual function definition changed in a derived class So: We say it’s been ‘overridden’ So: Virtual functions changed: overridden Non-virtual functions changed: redefined

Virtual Functions: Why Not All? Clear advantages to virtual functions as we’ve seen One major disadvantage: overhead! Uses more storage Late binding is ‘on the fly’, so programs run slower So if virtual functions not needed, should not be used

Subtle Example getArea is not virtual; myBall.displayStatistics() calls Sphere::getArea

Subtle Example getArea is virtual; (a) mySphere.displayStatistics() calls Sphere::getArea;

Subtle Example (b) myBall.displayStatistics() calls Ball::getArea

Not All Definitions are Useful Base class might not have ‘meaningful’ definition for some of it’s members! class Employee { public: Employee(string tName, sring tSsn); … void printCheck() const; }; Employee::printCheck() { cout << “ERROR: Undifferentiated employee\n” exit(0); }

Pure Virtual Functions class Employee { public: Employee(); Employee(string tName, sring tSsn); string getName() const; string getSsn() const; double getNetPay() const; void setName(string newName); void setSsn(string newSsn); void setNetPay(double newNetPay); virtual void printCheck() = 0; private: string name; string ssn; double netPay; }; pure virtual function

Abstract Base Classes Pure virtual functions require no definition Forces all derived classes to define ‘their own’ version Class with one or more pure virtual functions is: abstract base class Can only be used as base class No objects can ever be created from it Since it doesn’t have complete ‘definitions’ of all it’s members! If derived class fails to define all pure’s: It’s an abstract base class too

Multiple Inheritance class Base { public: virtual void print()=0; }; class DerivedOne : public Base { public: void print() {cout<<“DerivedOne”;} }; class DerivedTwo : public Base { public: void print() {cout<<“DerivedTwo”;} };

Multiple Inheritance Base DerivedOne DerivedTwo Multiple class Multiple : public DerivedOne,public DerivedTwo { public: void print() {DerivedTwo::print();} }; Base DerivedOne DerivedTwo Multiple

Multiple Inheritance int main(){ Multiple both; DerivedOne one; DerivedTwo two; Base *array[3]; array[0] = &both; array[1] = &one; array[2] = &two; for (int i=0; i<3; i++) array[i]->print(); return 0; } Error C2594: ‘=‘: ambiguous converstion from ‘class Multiple *’ to ‘class Base *’

Virtual Base Class class Base { public: virtual void print()=0; }; class DerivedOne : virtual public Base { public: void print() {cout<<“DerivedOne”;} }; class DerivedTwo : virtual public Base { public: void print() {cout<<“DerivedTwo”;} };

Virtual Base Class DerivedOne DerivedTwo Base Multiple class Multiple : public DerivedOne,public DerivedTwo { public: void print() {DerivedTwo::print();} }; DerivedOne DerivedTwo Base Multiple

Virtual Base Class int main(){ Multiple both; DerivedOne one; DerivedTwo two; Base *array[3]; array[0] = &both; array[1] = &one; array[2] = &two; for (int i=0; i<3; i++) array[i]->print(); return 0; } Derived Two Derived One