Object Oriented Programming in C++ Chapter 7 Dynamic Binding.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
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.
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 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.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
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.
Java and C++, The Difference An introduction Unit - 00.
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.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10 Inheritance and Polymorphism
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.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Variations on Inheritance Object-Oriented Programming Spring
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism Encapsulation Inheritance
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.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
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.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 11: Virtual Function and Pure Virtual Function
CMSC 202 Polymorphism.
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Polymorphism.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
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.
Object-Oriented Programming (OOP) Lecture No. 25
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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.
Presentation transcript:

Object Oriented Programming in C++ Chapter 7 Dynamic Binding

class Employee { public: Employee( String nm, int ag ) ; Employee( ) ; void print( void ) const ; private: String name; int age; };

Implementation of Employee’s methods Employee ::Employee( String nm, int ag ) : age( ag ), name( nm ) { } Employee ::Employee( ): age(0), name("") { } void Employee ::print( void ) const { cout << "Name " << name << " Age " << age; }

Manager inherits from Employee class Manager : public Employee { public: Manager( String nm, int ag, int lev ); Manager( ); void print( void ) const private: int level; }; print redefined

Manager’s member functions Manager ::Manager( char* nm, int ag, int lev ) : Employee( nm, ag ), level( lev ) { } Manager :: Manager( ) : Employee( ), level( 0 ) { } void Manager :: print( void ) const { Employee::print(); cout << " Level " << level; }

Using Employee and Manager Employee emp1( “Shem ", 42 ); Employee emp2( “Ham ", 32 ); Manager mgr1( “Yefet", 50, 3 ); Manager mgr2( “Zefet ", 46, 2 ); emp1.print();// Employee’s print function emp2.print();//“““ mgr1.print();// Manager’s print function mgr2.print();//“““ Name Shem Age 42 Name Ham Age 32 Name Yefet Age 50 Level 3 Name Zefet Age 46 Level 2

Storing pointers to objects, then printing Employee* employees[4]; // array of pointers to Employee objects employees[0] = &emp1; // Store address of emp1 in first array element employees[1] = &mgr1; // store address of mgr1 in second element employees[2] = &emp2; // etc. employees[3] = &mgr2; // etc. for ( int i = 0; i < 4; i++ ) { employees[i]->print(); // call print member function of each element cout << endl; }

Oops… Name Shem Age 42 Name Yefet Age 50 Name Ham Age 32 Name Zefet Age 46 Not what we had expected The Employee version of print() has been invoked for objects of both Employee and Manager This was determined at compile-time - Static Binding by reference to the array element type (Employee*) We want the Manager version of print() to be called for Manager objects

Solution Make Employee’s print method virtual Ensures that the version of print() invoked is determined at run-time by reference to the actual object referred to by each pointer stored in the array - Dynamic Binding In Employee class declaration: virtual void print( void ) const { cout << "Name " << name << " Age " << age; } l No change required to class Manager

Output from loop with virtual print() Name Shem Age 42 Name Yefet Age 50 Level 3 Name Ham Age 32 Name Zefet Age 46 Level 2 OK now. Base class function is virtual - ensures Dynamic Binding

Virtual functions Default is static binding - faster Dynamic binding only if specifically requested

Abstract base classes classes which we never intend to instantiate cannot create objects of this class can declare pointers to this class vs concrete classes too generic to define real objects used only as a base class for inheritance required to have a derived class made by defining 1 or more virtual functions (methods) as pure if derived class does not override all pvf, it becomes an abstract class as well! A pure virtual function - virtual void print( void ) const = 0; // no parentheses

Employee as an Abstract class class Employee { public: Employee( String nm, int ag ) : age( ag ), name( nm ) { } Employee( ): age(0), name("") { } virtual void print( void ) const = 0; protected: String name; int age; };

Class Manager class Manager : public Employee { public: Manager( String nm, int ag, int lev ) : Employee( nm, ag ), level( lev ) { } Manager( ) : Employee(), level( 0 ){ } // redefinition of pure virtual function void print( void ) const { cout << "Name " << name << " Age " << age << " Level " << level; } private: int level; };

Class Worker class Worker : public Employee { public: Worker ( String nm, int ag ) : Employee( nm, ag ) { } Worker ( ) : Employee() { } // redefinition of pure virtual function void print( void ) const { cout << "Name " << name << " Age " << age; } };

2 derived classes in Employee* array Worker wkr1( “Shem ", 42 ); Worker wkr2( “Ham ", 32 ); Manager mgr1( “Yefet", 50, 3 ); Manager mgr2( “Zefet ", 46, 2 ); Employee* employees[4]; employees[0] = &wkr1; employees[1] = &mgr1; employees[2] = &wkr2; employees[3] = &mgr2; for ( int i = 0; i < 4; i++ ) { employees[i]->print(); cout << endl; } Name Shem Age 42 Name Yefet Age 50 Level 3 Name Ham Age 32 Name Zefet Age 46 Level 2