CS212: Object Oriented Analysis and Design

Slides:



Advertisements
Similar presentations
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
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.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
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.
Programming Pillars Introduction to Object- Oriented Programming.
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.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Object-Oriented Programming Chapter Chapter
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Computer Science Department Inheritance & Polymorphism.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
One class is an extension of another.
Class A { public : Int x; A()
Object-Oriented Programming
Polymorphism.
Inheritance and Run time Polymorphism
Inheritance AKEEL AHMED.
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Object-Orientated Programming
Object Oriented Programming
Polymorphism Lec
One class is an extension of another.
Inheritance Basics Programming with Inheritance
Abstract Classes AKEEL AHMED.
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
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.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
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
Presentation transcript:

CS212: Object Oriented Analysis and Design Run Time Polymorphism

Polymorphism Virtual Function Operator overloading Polymorphism in C++ Compile Time Function overloading Operator overloading Runtime Virtual Function The provision of a single interface to entities of different types. Methods with same name but different implementations. Operators have different implementations depending on their arguments A member function that you expect to be redefined in derived classes.

Introduction Virtual function Inheritance Access control Encapsulation An object as its own type or its base type Separates the interface from the implementation Creates new data types

Paradigm shift in programming Better C Enhanced version of C Structure  Class Variable declaration Object Based Grouping a data structure together (Encapsulation) Constructor and destructors Inheritance Object oriented Virtual function Intimately bound with the concept of type Understood only from a design viewpoint

Pointers to Derived Types A pointer of one type cannot point to an object of a different type An important exception to this rule that relates only to derived classes B D A base class pointer can point to any derived classes object Demonstration

Virtual Function Redefined by a derived class A virtual function is a member function Declared within a base class Redefined by a derived class “one interface, multiple methods”

Binding Types of Bindings Early Late Connecting a function call to a function body is called binding. Types of Bindings Early Late Dynamic binding or Runtime binding Before the program is run (by the compiler and linker) Binding occurs at runtime, based on the type of the object

Virtual functions To cause late binding to occur for a particular function Use the virtual keyword when declaring the function When a virtual function is redefined, all aspects of its prototype must be the same. Constructor functions cannot be virtual, but destructor functions can. Overriding : virtual function redefinition by a derived class. Demonstration

How C++ implements late binding Compiler takes care of the virtual function mechanism Run-time polymorphism is achieved when access is through a base-class pointer (or reference) Compiler creates a single table (called the VTABLE) for each class Compiler places the addresses of the virtual functions Pointer to the VTABLE for an object: VPTR (vpointer)

Storing type information No explicit type information stored in any of the classes. How the type is determined at run time? The type information is hidden. Demonstration

Instrument; play(), what(), adjust() An Example Instrument; play(), what(), adjust() Wind Percussion Stringed Brass Woodwind Demonstration

Visualizing virtual functions

Visualizing virtual functions Begins with the Instrument pointer Compiler can pick the VPTR of that object VPTR points to the starting address of the VTABLE Compiler knows adjust( ) function is at the location VPTR+2 “Call the function at VPTR+2”

The Virtual Attribute Is Inherited When a virtual function is inherited, its virtual nature is also inherited. Base class Virtual function Derived class 1 No matter how many times a virtual function is inherited, it remains virtual Derived class 2 Virtual function

Inheritance and the VTABLE Compiler creates a new VTABLE for your new class For non-overridden virtual functions, inserts the addresses using the base-class function addresses What happens when you inherit and add new virtual functions in the derived class? (Demonstration) RTTI: Run Time Type Identification

Hierarchical Virtual Functions Base class Virtual function Derived class 1 Derived class 2 Case 1 Base class Virtual function Derived class 1 Derived class 2 Case 2 Base class Virtual function Derived class 1 Derived class 2 Case 3

Overheads of virtual functions https://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf