CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
Polymorphism, Virtual Methods and Abstract Classes.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
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.
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.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
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.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
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.
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.
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.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Computer Science Department Inheritance & Polymorphism.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Polymorphism, Virtual Methods and Abstract Classes
CS 3370 – C++ Object-oriented Programming
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Big O And your first reading assignment
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Inheritance, Polymorphism and the Object Memory Model
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance and Polymorphism:
Polymorphism Polymorphism
Pointers Dr. Bhargavi Goswami Department of Computer Science
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
CISC/CMPE320 - Prof. McLeod
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 11 Class Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
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:

CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions

Recap of lecture 16 Polymorphism Pointer to derived type Virtual function VTABLE, VPTR Inheritance and VTABLE

Outline of Lecture 17 Abstract base class Object slicing Overloading, overriding Constructor and destructors Multiple dispatching Downcasting

Abstract base class Base class to present only an interface for its derived classes No object is created of the base class This is accomplished by making that class abstract pure virtual function. It uses the virtual keyword and is followed by = 0 virtual type func-name(parameter-list) = 0;

Pure virtual definitions It’s possible to provide a definition for a pure virtual function The compiler will not allow objects of that abstract base class Pure virtual functions must still be defined in derived classes in order to create objects Demonstration

Object slicing Passing the addresses of objects and passing objects by value In case polymorphism there is a distinct difference Upcast to an object instead of a pointer or reference The object is “sliced” Demonstration

Overloading vs. Overriding Redefining an overloaded function in the base class Virtual functions: the behaviour is a little different. Compiler will not allow you to change the return type Demonstration

Virtual functions & constructors VPTR must be initialized to point to the proper VTABLE The constructor has the job of bringing an object into existence The constructor’s job to set up the VPTR Compiler inserts code into the beginning of the constructor Constructor will include the proper VPTR initialization code There are several implications

Implication 1: Efficiency Inline functions It initialize the VPTR, it must also check the value of this Calls base class constructor Code size can grow without any benefits in speed

Implication 2: Order of constructor calls The order of constructor calls and the way virtual calls are made within constructors Base-class constructors are always called in the constructor for an inherited class Compiler enforces a constructor call for every portion of a derived class Calling virtual function from the constructor

Virtual function & destructors Constructor assembles the object parts, while destructor disassembles it The disassembling process happens in the reverse order of that of the assembly A derived object can be manipulated using base class pointer The problem occurs when a pointer of this type is to be deleted for an object that has been created on the heap with new Demonstration

Operator overloading Operator functions can be made virtual Implementing virtual operators often becomes confusing Operating on two objects, both with unknown types Multiple dispatching Demonstration

Downcasting Downcasting: to move down a hierarchy There are usually several possibilities that you could cast to Dilemma is figuring out a way to safely downcast Type-safe downcast operation: dynamic_cast dynamic_cast uses information stored in the VTABLE to determine the actual type Demonstration

Summary Polymorphism  C++  Virtual Function  “Different forms” Polymorphism is a feature that cannot be viewed in isolation If it isn’t late binding, it isn’t polymorphism

Thank you Next Lecture: Exception Handling