Virtual functions concepts

Similar documents
Inheritance (with C++)

Inheritance Basics. Inheritance (with C++) Base class example: Employee. Inheritance begets hierarchies. Writing derived classes

Chapter 15. Polymorphism and Virtual Functions. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance

Inheritance. Chapter 15 & additional topics

Inheritance. Chapter 15 & additional topics

Class Sale. Represents sales of single item with no added discounts or charges. Notice reserved word "virtual" in declaration of member function bill

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Data Structures and Other Objects Using C++

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

VIRTUAL FUNCTIONS Chapter 10

C++ Programming: Polymorphism

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance, Polymorphism, and Interfaces

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

15: Polymorphism & Virtual Functions

Upcasting. Taking an object reference and treating it as a reference to its base type is called upcasting.

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Polymorphism Part 1 1

Java Object Oriented Design. CSC207 Fall 2014

More class design with C++ Starting Savitch Chap. 11

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Fast Introduction to Object Oriented Programming and C++

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Data Abstraction. Hwansoo Han

CSE 307: Principles of Programming Languages

Object-Oriented Programming

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Instantiation of Template class

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

VALLIAMMAI ENGINEERING COLLEGE

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Polymorphism. Arizona State University 1

Inheritance, Polymorphism and the Object Memory Model

CS Lecture #6. Administrative... Assignment #2 Graded NN Turned In, Average Grade: XX Assignment #3 due on Thursday

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

04-24/26 Discussion Notes

Object Oriented Paradigm

Comp 249 Programming Methodology Chapter 8 - Polymorphism

8. Polymorphism and Inheritance

Derived Classes in C++

Lecture 5: Inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS 162, Lecture 25: Exam II Review. 30 May 2018

Introduction to Object-Oriented Programming

Object Oriented Programming. Solved MCQs - Part 2

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

Inheritance and object compatibility

Module 10 Inheritance, Virtual Functions, and Polymorphism

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Increases Program Structure which results in greater reliability. Polymorphism

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Lecture 20: templates

CS105 C++ Lecture 7. More on Classes, Inheritance

Polymorphism and Inheritance

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

POLYMORPHISM Polymorphism: the type Polymorphism taking many shapes type of object

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

Polymorphism. Zimmer CSCI 330

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

CPSC 427: Object-Oriented Programming

COMP322 - Introduction to C++

Object Oriented Programming

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

What are the characteristics of Object Oriented programming language?

CS111: PROGRAMMING LANGUAGE II

Part VIII. More classes. Philip Blakely (LSC) C++ Introduction 226 / 370

Software Design and Analysis for Engineers

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism

CS11 Introduction to C++ Fall Lecture 7

OOP Fundamental Concepts

And Even More and More C++ Fundamentals of Computer Science

Inheritance, polymorphism, interfaces

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Advanced C++ Topics. Alexander Warg, 2017

Object Oriented Programming: Inheritance Polymorphism

Reusable and Extendable Code. Composition Example (1) Composition Example (2) CS183-Su'02-Lecture 8 17 June by Eric A. Durant, Ph.D.

Transcription:

Virtual functions concepts l Virtual: exists in essence though not in fact l Idea is that a virtual function can be used before it is defined And it might be defined many, many ways! l Relates to OOP concept of polymorphism Associate many meanings to one function l Implemented by dynamic binding A.k.a. late binding happens at run-time

Polymorphism example: figures l Imagine classes for several kinds of figures Rectangles, circles, and ovals (to start) All derive from one base class: Figure l All Figure objects inherit: void draw() Of course, each one implements it differently! Rectangle r; Circle c; r.draw(); // Calls Rectangle class s draw() c.draw(); // Calls Circle class s draw l Nothing new here yet

Figures example cont. center() l Consider that base class Figure has functions that apply to all figures l e.g., center(): moves figure to screen center Erases existing drawing, then re-draws the figure So Figure::center() uses draw() to re-draw l But which draw() function will be used? We re implementing base class center() function, so we have to use the base class draw() function. Right? l Actually, it turns out the answer depends on how draw() is handled in the base class

Poor solution: base works hard l Figure class tries to implement draw to work for all (known) figures First devise a way to identify a figure s type Then Figure::draw() uses conditional logic: if ( /* the Figure is a Rectangle */ ) Rectangle::draw(); else if ( /* the Figure is a Circle */ ) Circle::draw();... l But what if a new kind of figure comes along? e.g., how to handle a derived class Triangle?

Better solution: virtual function l Base class declares that the function is virtual: virtual void draw() const; l Remember it means draw() exists in essence l Such a declaration tells compiler I don t know how this function is implemented, so wait until it is used in a program, and then get its implementation from the object instance. l The instance will exist in fact (eventually) Therefore, so will the implementation at that time! l Function binding happens late dynamically

Another virtual function example (Sale, DiscountSale, Display 15.11) l Record-keeping system for auto parts store Track sales, compute daily gross, other stats All based on data from individual bills of sale l Problem: lots of different types of bills l Idea start with a very general Sale class that has a virtual bill() function: virtual double bill() const; l Rest of idea many different types of sales will be added later, and each type will have its own version of the bill() function

Sale functions: savings and op < double Sale::savings(const Sale &other) const { return (bill() other.bill()); } bool operator < (const Sale &first, const Sale &second) { return (first.bill() < second.bill()); } l Notice both functions use member function bill()!

A class derived from Sale class DiscountSale : public Sale { public: DiscountSale(); DiscountSale(double price, double discount); double getdiscount() const; void setdiscount(double newdiscount); double bill() const; // implicitly virtual private: double discount; // inherits price };

DiscountSale s bill() function l First note it is automatically virtual Inherited trait, applies to any descendants Also note rude not to declare it explicitly l Of course, definition never says virtual: double DiscountSale::bill() const { double fraction = discount/100; return (1 fraction)*getprice(); } Must use access method as price is private

The power of virtual is actual! l e.g., base class Sale written long before derived class DiscountSale l Sale had members savings and < before there was any idea of class DiscountSale l Yet consider what the following code does DiscountSale d1, d2; d1.savings(d2); // calls Sale s savings function l In turn, class Sale s savings function uses class DiscountSale s bill function. Wow!

Clarifying some terminology l Recall that overloading redefining l Now a new term overriding means redefining a virtual function l Polymorphism is an OOP concept Overriding gives many meanings to one name l Dynamic binding is what makes it all work l Thus, as Savitch puts it, polymorphism, late binding, and virtual functions are really all the same topic.

Why not all virtual functions? l Philosophy issue: pure OOP vs. efficiency All functions are virtual by default in another popular programming language (Java) there must take steps to make functions non-virtual C++ default is non-virtual programmer must explicitly declare (except when inherited trait) l Virtual functions have more overhead More storage for class virtual function table Slower a look-up step; less optimization

Simpler polymorphism demo (~mikec/cs32/demos/figures) l Base: Figure has virtual void print() print() is used in printat(lines) l Derived: Rectangle just overrides print() l Which print() is used in the following code? Figure *ptr = new Rectangle, &ref = *new Rectangle('Q', 5, 10, 4); ptr->printat(1); ref.printat(1); l What if print() was not declared virtual? l What if line 2 above just had ref, not &ref? To know why, see slicing a few slides from now

Pure virtual and abstract classes l Actually class Figure s print() function is useless It should have been a pure virtual function: virtual void draw() const = 0; Says not defined in this class means any derived class must define its own version, or be abstract itself l A class with one or more pure virtual functions is an abstract class so it can only be a base class An actual instance would be an incomplete object So any instance must be a derived class instance

A sorting hierarchy See /demos/sorting

Types when inheritance is involved l Consider: void func (Sale &x) { } or similarly: void func (Sale *xp) { } What type of object is x (or *xp), really? Is it a Sale? Or is it a DiscountSale, or even a CrazyDiscountSale? l Just Sale members are available But might be virtual, and Sale might even be abstract & and * variables allow polymorphism to occur l Contrast: void func (Sale y) { } What type of object is y? It s a Sale. Period. Derived parts are sliced off by Sale s copy ctor Also in this case, Sale cannot be an abstract class

Type compatibility example class Pet { public: // pls excuse bad info hiding }; string name; virtual void print(); class Dog : public Pet { public: string breed; virtual void print(); }; l Consider: Dog d; Pet p; d.name = "Tiny"; d.breed = "Mutt"; p = d; // slicing here All okay a Dog is a Pet l Reverse is not okay A Pet might be a Bird, or l And p.breed? Nonsense! l Also see slicing.cpp at ~mikec/cs32/demos/

Destructors should be virtual l Especially if class has virtual functions l Derived classes might allocate resources via a base class reference or pointer: Base *ptrbase = new Derived;... // a redefined function allocates resources delete ptrbase; l If dtor not virtual, derived dtor is not run! l If dtor is virtual okay: run derived dtor, immediately followed by base dtor

Casting and inherited types l Consider again: Dog d; Pet p; l Upcasting (descendent to ancestor) is legal: p = d; // implicitly casting up p = static_cast<pet>(d); // like (Pet)d But objects sliced if not pointer or reference l Other way ( downcasting ) is a different story: d = static_cast<dog>(p); // ILLEGAL Can only do by pointer and dynamic cast : Pet *pptr = new Dog; // we know it s a Dog Dog *dptr = dynamic_cast<dog*>(pptr) But can be dangerous, and is rarely done

Multiple inheritance and virtual l Idea: a ClockRadio is a Radio and an AlarmClock But what if class Radio and class AlarmClock are both derived from another class, say Appliance? Doesn t each derived object contain an Appliance portion? So wouldn t a Clockradio have two copies of that portion, and how can such a scheme possibly work properly? l Answer: it can work, but only by using virtual inheritance! class Radio : virtual public Appliance; class AlarmClock : virtual public Appliance; class ClockRadio : public Radio, public AlarmClock; Now a Clockradio has just one Appliance portion, not two l See demo code in ~mikec/cs32/demos/multi-inherit l But note: hierarchy is still messed up, and still lots of chances for ambiguity best to avoid multi-inheritance!

How do virtual functions work? l Not exactly magic, but safe to consider it so l virtual tells compiler to wait for instructions until the function is used in a program l So the compiler creates a virtual function table for the class, with pointers to all virtual functions l In turn, every object of such a class will be made to store a pointer to its own class s virtual function table try /demos/sizeofvirtual.cpp l At runtime: follow the pointers to find the code!