Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Inheritance, Polymorphism, and Virtual Functions
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,
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.
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.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Learners Support Publications Pointers, Virtual Functions and 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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
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.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Polymorphism Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Definition polymorphismthe ability to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
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.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
A First Book of C++ Chapter 12 Extending Your Classes.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
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.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Note: Some of the slides are repeated from Introduction to C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C++ Exceptions.
CMSC 202 Polymorphism.
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism & Virtual Functions
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and 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.
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
CISC/CMPE320 - Prof. McLeod
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
C++ Introduction - 3.
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:

Inheritance - 3

Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as virtual are ones that the base expects its derived classes may redefine. But it is not mandatory for a derived class to redefine a virtual function. But it is not mandatory for a derived class to redefine a virtual function. Functions NOT defined as virtual are ones that the base expects its derived classes may NOT redefine. Functions NOT defined as virtual are ones that the base expects its derived classes may NOT redefine. But a derived class could redefine a non virtual function. But a derived class could redefine a non virtual function.

Virtual functions … Invoking virtual functions through a base class pointer (or reference): Invoking virtual functions through a base class pointer (or reference): ALWAYS invokes the function defined by the actual type of the object to which the pointer (or reference) points to. ALWAYS invokes the function defined by the actual type of the object to which the pointer (or reference) points to. The type of the pointer itself is not so significant. The type of the pointer itself is not so significant. If the base class pointer is pointing to a base object it will invoke base class method If the base class pointer is pointing to a base object it will invoke base class method If the base class pointer is pointing to a derived class object (and the derived class redefines the virtual function), it will invoke the derived class method. If the base class pointer is pointing to a derived class object (and the derived class redefines the virtual function), it will invoke the derived class method. See shapes2.cpp See shapes2.cpp

Inheritance Hierarchy Classes related by inheritance are said to form an inheritance hierarchy. Classes related by inheritance are said to form an inheritance hierarchy. There are no limits (at least theoretically) on number of levels in the hierarchy. There are no limits (at least theoretically) on number of levels in the hierarchy. Redefinition of virtual and non virtual functions can occur at any level in the hierarchy. Redefinition of virtual and non virtual functions can occur at any level in the hierarchy.

Inheritance Hierarchy … class Base { /*... */ }; class Base { /*... */ }; class D1: public Base { /*... */ }; class D1: public Base { /*... */ }; class D2: public D1 { /*... */ }; class D2: public D1 { /*... */ }; The most derived type inherits the members of its base, which in turn inherits the members of its base and so on up the inheritance chain. The most derived type inherits the members of its base, which in turn inherits the members of its base and so on up the inheritance chain. Effectively, the most derived object contains a subobject for each of its immediate-base and indirect-base classes. Effectively, the most derived object contains a subobject for each of its immediate-base and indirect-base classes.

Virtual functions … For a virtual function invocation via a base class pointer or reference, the compiler DOES NOT do static binding. For a virtual function invocation via a base class pointer or reference, the compiler DOES NOT do static binding. It instead outputs code that at run-time: It instead outputs code that at run-time: Checks the type of the object that the pointer is pointing to Checks the type of the object that the pointer is pointing to Calls the virtual override (redefinition) that is defined by the object. Calls the virtual override (redefinition) that is defined by the object. Note that the final object could be many class levels away from the base (say 10) and all or some of the classes could be redefining the virtual function. Note that the final object could be many class levels away from the base (say 10) and all or some of the classes could be redefining the virtual function.

Dynamic Binding Since the binding of the virtual function is done at run-time for virtual functions, this type of binding is known as Dynamic Binding Since the binding of the virtual function is done at run-time for virtual functions, this type of binding is known as Dynamic Binding As we already studied, for non virtual functions the binding is done at compile time itself and is called static binding As we already studied, for non virtual functions the binding is done at compile time itself and is called static binding

Polymorphism double printShapeAreas(shape *sp[]) { double printShapeAreas(shape *sp[]) { …. …. cout getarea() getarea() << endl; Which getarea() does the statement call??? Which getarea() does the statement call??? shape?, rect?, circle? shape?, rect?, circle? Known only at runtime Known only at runtime In the loop, the same statement will at times invoke getarea() of rect and at times invoke getarea() of shape. In the loop, the same statement will at times invoke getarea() of rect and at times invoke getarea() of shape. Same statement, different results!!! Same statement, different results!!! Or same statement, different forms -> known as Polymorphism Or same statement, different forms -> known as Polymorphism

Dynamic Binding/Polymorphism By default, in C++, function calls DO NOT use dynamic binding By default, in C++, function calls DO NOT use dynamic binding To trigger dynamic binding two conditions must be met: To trigger dynamic binding two conditions must be met: First, only member functions that are specified as virtual can be dynamically bound (by default, member functions are not virtual; nonvirtual functions are not dynamically bound) First, only member functions that are specified as virtual can be dynamically bound (by default, member functions are not virtual; nonvirtual functions are not dynamically bound) Second, the call must be made through a reference or a pointer to a base-class type. Second, the call must be made through a reference or a pointer to a base-class type.

Dynamic Binding/Polymorphism … The crucial point about references and pointers to base-class types is that: The crucial point about references and pointers to base-class types is that: The static type: the type of the reference or pointer, which is knowable at compile time The static type: the type of the reference or pointer, which is knowable at compile timestatic typestatic type and the dynamic type: the type of the object to which the pointer or reference is bound, which is knowable only at run time and the dynamic type: the type of the object to which the pointer or reference is bound, which is knowable only at run timedynamic typedynamic type may differ. may differ.

Dynamic Binding/Polymorphism … Binding a base-type reference or pointer to a derived object has no effect on the underlying object. Binding a base-type reference or pointer to a derived object has no effect on the underlying object. The object itself is unchanged and remains a derived object. The object itself is unchanged and remains a derived object. The fact that the actual type of the object might differ from the static type of the reference or pointer addressing that object is the key to dynamic binding in C++. The fact that the actual type of the object might differ from the static type of the reference or pointer addressing that object is the key to dynamic binding in C++. When a virtual function is called through a reference or pointer, the compiler generates code to decide at run time which function to call. When a virtual function is called through a reference or pointer, the compiler generates code to decide at run time which function to call. The function that is called is the one that corresponds to the dynamic type. The function that is called is the one that corresponds to the dynamic type. See code of topic ‘Calls to virtual Functions May be resolved at run time’ in Section 15.2 See code of topic ‘Calls to virtual Functions May be resolved at run time’ in Section 15.2

Pure Virtual functions Shape class objects do not make sense as shape is an abstract concept and not a real geometric figure Shape class objects do not make sense as shape is an abstract concept and not a real geometric figure Only objects derived from shape like rect and circle are meaningful. They are concrete geometric figures (not abstract). Only objects derived from shape like rect and circle are meaningful. They are concrete geometric figures (not abstract). Ideally we should prevent users from creating a shape object. Ideally we should prevent users from creating a shape object. That can be achieved using pure virtual functions. That can be achieved using pure virtual functions. See shapes3.cpp See shapes3.cpp

Pure Virtual functions … virtual double getarea() const = 0; // Pure virtual virtual double getarea() const = 0; // Pure virtual Makes getarea() a pure virtual function. Makes getarea() a pure virtual function. And makes shape an abstract class. And makes shape an abstract class. Objects cannot be created from an abstract class. Objects cannot be created from an abstract class. shape s;// Compiler will flag as error shape s;// Compiler will flag as error

Assignment 8B The same as given earlier (employee, salesman) but now using CalculateSalary() as a virtual function. The same as given earlier (employee, salesman) but now using CalculateSalary() as a virtual function.

More realistic shapes example Shapes3.cpp has limitation of fixed number of shapes. Shapes3.cpp has limitation of fixed number of shapes. Consider program like Microsoft Paint. Consider program like Microsoft Paint. It may use a class hierarchy to keep track of various shapes drawn on bitmap. It may use a class hierarchy to keep track of various shapes drawn on bitmap. The various shapes will have to be kept in some collection. The various shapes will have to be kept in some collection. But cannot use fixed size array. But cannot use fixed size array. So, what’s the solution?? So, what’s the solution?? See vecshapes1.cpp See vecshapes1.cpp

Book Source Code Copyright Note The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, are of example code given in and/or along with the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, based on the permission related statements in the source code, I have considered that permission has been granted to use them in these slides. The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, are of example code given in and/or along with the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, based on the permission related statements in the source code, I have considered that permission has been granted to use them in these slides.