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.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
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.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
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.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
Chapter -6 Polymorphism
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Overview of C++ 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.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 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.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
CMSC 202 Polymorphism.
Polymorphism, Virtual Methods and Abstract Classes
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance: hiding methods
Polymorphism Lec
Inheritance Using work from:
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism 1 CMSC 202.
Polymorphism 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.
Fundaments of Game Design
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 9 Carrano Chapter 10 Small Java
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
COP 3330 Object-oriented Programming in C++
C++ Polymorphism Reference and pointer implicit type casting
Computer Science II for Majors
Presentation transcript:

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 pointed to Static binding is done at compile time

Late Binding / Dynamic Binding technique of waiting until runtime to determine which version of a member function is appropriate Can look at the actual class of the object pointed to and choose the most specific version of the function Dynamic binding is used to bind virtual functions

Polymorphism Polymorphism is another word for late binding. Enables us to write programs that process objects of classes that are part of the same class hierarchy as if they were all objects of the hierarchy’s base class.

Virtual Functions (C++ only) By default, C++ matches a function call with the correct function definition at compile time -- static binding. Can specify that the compiler match a function call with the correct function definition at run time -- dynamic binding. Declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.

Virtual Functions (C++ only) The virtual function specification tells the compiler to create a pointer to a function f(), but to not fill in the value of the pointer until the function is actually called. Declaring a function virtual will make the compiler check the type of each object to see if it defines a more specific version of the virtual function;

Virtual Functions (C++ only) the compiler chooses the appropriate definition of f(), not by the type of reference, but by the type of object that the reference refers to. Therefore, a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.

Virtual Functions (C++ only) To create a virtual function, place the keyword virtual before the function's return type in the declaration section. Once a function is declared as virtual, it remains virtual for the next derived class with or without a virtual declaration in the derived class. The virtual declaration in the derived class is not needed, but is usually included both for clarity and to ensure that any subsequently derived classes correctly inherit the function.

Virtual Functions (C++ only) A class that declares or inherits a virtual function is called a polymorphic class. You redefine a virtual member function, like any member function, in any derived class.

Polymorphism and Virtual Member Functions Polymorphic code: Code that behaves differently when it acts on objects of different types Virtual Member Function: The C++ mechanism for achieving polymorphism

Polymorphism Consider the Vehicle, Car, Truck hierarchy where each class has its own version of the member function printType( ) Vehicle Car Truck 18-Wheeler

Polymorphism class Vehicle { public: Vehicle(); Vehicle(…); void printType(); private: string type; };

Polymorphism class Car : public Vehicle { public: Car(); Car(…) void printType(); … };

Polymorphism class Truck : public Vehicle { public: Truck(); Truck(…) void printType(); … };

void Vehicle::printType() { cout << "vehicle"; } Polymorphism void Vehicle::printType() { cout << "vehicle"; }

void Car::printType() { cout << "car"; } Polymorphism void Car::printType() { cout << "car"; }

void Truck::printType() { cout << "truck"; } Polymorphism void Truck::printType() { cout << "truck"; }

Polymorphism Consider the collection of different Vehicle objects Vehicle *vehCollection[] = {new Vehicle, new Car,new Truck}; and accompanying code for(int k=0; k<3; k++) vehCollection[k]->printType(); Prints: vehicle vehicle vehicle ignoring the more specific versions of printType() in Car and Truck See inheritance4.h and pr15-02.cpp

Polymorphism The preceding code is not polymorphic: it behaves the same way even though Vehicle, Car, and Truck have different types and different printType() member functions. Static binding is used. In the expression vehCollection[k]->printType(); the compiler sees only the type of the pointer vehCollection[k], which is pointer to Vehicle; it does not see the type of the actual object pointed to, which may be Vehicle, or Car, or Truck

Polymorphism Polymorphic code would have printed vehicle car truck instead of vehicle vehicle vehicle

Virtual Functions If the member functions printType()are declared virtual, then the code Vehicle *vehCollection[] = {new Vehicle, new Car,new Truck}; for(int k=0; k<3; k++) vehCollection[k]->id(); will print vehicle car truck See inheritance5.h and pr15-03.cpp

Virtual Functions Declaring printType() as a virtual function class Vehicle { public: … virtual void printType(); }

Virtual Functions Declaring printType() as a virtual function: class Car : public Vehicle { … virtual void printType(); }

Virtual Functions Declaring printType() as a virtual function: class Truck : public Vehicle { … virtual void printType(); }

Virtual functions Now that printType() is declared virtual, vehCollection[k]->printType() will print vehicle car truck dynamic binding is used and the compiler chooses the appropriate definition of printType() by the type of object that the reference refers to.

Abstract Base Classes and Pure Virtual Functions An abstract class is a class that contains no objects that are not members of subclasses (derived classes) For example, in real life, Animal is an abstract class: there are no animals that are not dogs, or cats, or lions …

Abstract Base Classes and Pure Virtual Functions Abstract classes are an organizational tool. They are useful in organizing inheritance hierarchies Abstract classes can be used to specify an interface that must be implemented by all subclasses

Abstract Functions The member functions specified in an abstract class do not have to be implemented The implementation is left to the subclasses In C++, an abstract class is a class with at least one abstract member function

Pure Virtual Functions In C++, a member function of a class is declared to be an abstract function by making it virtual and replacing its body with = 0; class Animal{ public: virtual void id()=0; }; A virtual function with its body omitted and replaced with =0 is called a pure virtual function, or an abstract function See pr15-04.cpp

Abstract Classes An abstract class can not be instantiated An abstract class can only be inherited from; that is, you can derive classes from it Classes derived from abstract classes must override all pure virtual functions with concrete member functions before they can be instantiated.