Virtual Functions Fall 2008 Dr. David A. Gaitros

Slides:



Advertisements
Similar presentations
Chapter 15 Polymorphism and Virtual Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
Polymorphism and Virtual Functions. class Ball : public Sphere.
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.
Inheritance and Polymorphism CS351 – Programming Paradigms.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
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.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Polymorphism and Virtual Functions
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 © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
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.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Copyright 2006 Pearson Addison-Wesley, 2008, 2009, 2013 Joey Paquet 6-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
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.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
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.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Function and Polymorphism
Polymorphism and Virtual Functions
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Chapter 15 Inheritance. Chapter 15 Inheritance.
Class A { public : Int x; A()
Polymorphism & Virtual Functions
Polymorphism and Virtual Functions
Polymorphism, Virtual Methods and Abstract Classes
C++ Classes C++ Interlude 1.
Polymorphism Lec
Learning Objectives Inheritance Virtual Function.
Inheritance, Polymorphism, and Virtual Functions
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
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:

Virtual Functions Fall 2008 Dr. David A. Gaitros

Virtual Functions Most of the time the compiler “binds” a function call to it’s definition after which no changes are allowed. This means that in order to accommodate all scenarios, you have to write your software from the start to account for them. Changes to requirements required that you modify the code. with Virtual functions we can accommodate changes in a more convenient fashion.

Virtual Functions Example taken from the Savitch Book: class Sale { public: Sale(); Sale(double thePrice); double getPrice() const; virtual double bill() const; double savings(const Sale& other) const; private: double price; }; Note the word “virtual” before the declaration of double bill() const; This means that at a later date, a class that inherits the class Sale can redefine this member function.

Virtual Functions Here is what the original member implementation looks like: double Sale::bill() const { return price; }

Here is what a new class that inherits the class looks like that redefines the member function bill. namespace SavitSale { class DiscountSale: public Sale { public: DiscountSale(); double getDiscount()const; void setDiscount (double d); double bill() const; private: double discount; }; } // end SavitSale

Virtual Functions A virtual function is indicated by including the modifier virtual in the member function declaration. If a function is virtual and a new definition of the function is given in a derived class, then for any object of the derived class, that object will always use the new definition. Even if it is invoked in the inherited function. This is called late binding.

Virtual Functions Virtual function in base class: – "Automatically" virtual in derived class Derived class declaration (in interface) – Not required to have "virtual" keyword – But typically included anyway, for readability The “virtual” term tells the compiler to wait to define it until it is called.

Virtual functions 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 (Much slower) So if virtual functions not needed, should not be used. If you know all of the functionality, go ahead and program it.

Virtual Destructors Recall: destructors needed to de- allocate dynamically allocated data Consider: Base *pBase = new Derived; … delete pBase; – Would call base class destructor even though pointing to Derived class object! – Making destructor virtual fixes this! Good policy for all destructors to be virtual in production programming

Inner Workings of Virtual Functions Don’t need to know how to use it! – Principle of information hiding Virtual function table – Compiler creates it – Has pointers for each virtual member function – Points to location of correct code for that function Objects of such classes also have pointer – Points to virtual function table

Abstract Classes Pure virtual functions are defined as those functions that have no exact definition at the time of compile. Example ( Myers’ Employee) #ifndef _EMPLOYEE_H #define _EMPLOYEE_H class Employee { public: virtual void PrintCheck()=0; protected: float netPay; Employee(); Employee(char* n, char* a, char* ssn); char name[30]; char address[80]; char socSecNumber[12]; };

Abstract Classes Note the “=0” in the PrintCheck function declaration. This tells compiler that there will be no implementation seen at this time. Any class with at least one pure virtual Function is known as an “Abstract Class”. Abstract classes can be used to build pointers to a class which you may want to define at a later time. Templates are coming!