Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,

Slides:



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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Inheritance, Polymorphism, and Virtual Functions
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
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.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 4 Applications of the Derivative.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
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,
Polymorphism in Methods
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Types of Programming Languages
Inheritance Using work from:
Advanced Java Topics Chapter 9
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
“Under the Hood” of Polymorphism
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
VIRTUAL FUNCTIONS RITIKA SHARMA.
Fundaments of Game Design
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
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:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism, Virtual Functions, and Multiple Inheritance by Tony Gaddis, Judy Walters, and Godfrey Muganda

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-2 Topics 15.1 Type Compatibility in Inheritance Hierarchies 15.2 Polymorphism and Virtual Member Functions 15.3 Abstract Base Classes and Pure Virtual Functions 15.4 Multiple and Virtual Inheritance 15.5 Composition Versus Inheritance

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Type Compatibility in Inheritance Hierarchies Classes in a program may be part of an inheritance hierarchy Classes lower in the hierarchy are special cases of those above Animal CatDog Poodle

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-4 Type Compatibility in Inheritance A pointer to a derived class can be assigned to a pointer to a base class A base class pointer can point to derived class objects Animal *pA = new Cat;

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-5 Type Compatibility in Inheritance Assigning a base class pointer to a derived class pointer requires a cast Animal *pA = new Cat; Cat *pC; pC = static_cast (pA); The base class pointer must already point to a derived class object for this to work

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-7 Polymorphism Consider the Animal, Cat, Dog hierarchy where each class has its own version of the member function id( ) Animal CatDog Poodle

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-8 Polymorphism class Animal{ public: void id(){cout << "animal";} } class Cat : public Animal{ public: void id(){cout << "cat";} } class Dog : public Animal{ public: void id(){cout << "dog";} }

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-9 Polymorphism Consider the collection of different Animal objects Animal *pA[] = {new Animal, new Dog, new Cat}; and accompanying code for(int k=0; k<3; k++) pA[k]->id(); Prints: animal animal animal, ignoring the more specific versions of id() in Dog and Cat

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polymorphism Preceding code is not polymorphic: it behaves the same way even though Animal, Dog and Cat have different types and different id() member functions Polymorphic code would have printed " animal dog cat" instead of " animal animal animal"

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polymorphism The code is not polymorphic because in the expression pA[k]->id() the compiler sees only the type of the pointer pA[k], which is pointer to Animal Compiler does not see type of actual object pointed to, which may be Animal, or Dog, or Cat

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Functions 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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Functions If the member functions id() are declared virtual, the code Animal *pA[] = {new Animal, new Dog,new Cat}; for(int k=0; k<3; k++) pA[k]->id(); will print animal dog cat

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Functions How to declare a member function virtual: class Animal{ public: virtual void id(){cout << "animal";} } class Cat : public Animal{ public: virtual void id(){cout << "cat";} } class Dog : public Animal{ public: virtual void id(){cout << "dog";} }

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Binding In pA[k]->id(), Compiler must choose which version of id() to use: There are different versions in the Animal, Dog, and Cat classes Function binding is the process of determining which function definition to use for a particular function call The alternatives are static and dynamic binding

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 Static binding is very efficient

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Binding Dynamic Binding determines the function to be invoked at execution time 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 More flexible but less efficient than static binding

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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…

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Abstract Base Classes and Pure Virtual Functions Abstract classes are an organizational tool: useful in organizing inheritance hierarchies Abstract classes can be used to specify an interface that must be implemented by all subclasses

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 the pure virtual function with a concrete member function before they can be instantiated.

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Multiple and Virtual Inheritance Multiple inheritance is the derivation of a new class from more than one base class Multiple inheritance is useful in modeling objects that have the characteristics of several different classes

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Multiple Inheritance The class D is derived from two base classes B2, B2: class D : public B1, public B2 { }; B1B2 D

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Inheritance Multiple inheritance may result in the same base class appearing twice in a derived class This causes ambiguity, and is a reason why multiple inheritance is rarely used.

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Inheritance D inherits two versions of base class B B D1D2 D3

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Inheritance Solving the problem of a multiply- inherited class: –When a base class is declared virtual, the compiler makes sure only one version of the class gets into any derived class class D1: virtual public B { }; class D2: virtual public B { };

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Composition vs. Inheritance Inheritance models an 'is a' relation between classes. An object of a derived class 'is a(n)' object of the base class Example: –an UnderGrad is a Student –a Mammal is an Animal –a Poodle is a Dog

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Composition vs. Inheritance When defining a new class: Composition is appropriate when the new class needs to use an object of an existing class Inheritance is appropriate when –objects of the new class are a subset of the objects of the existing class, or –objects of the new class will be used in the same ways as the objects of the existing class

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism, Virtual Functions, and Multiple Inheritance by Tony Gaddis, Judy Walters, and Godfrey Muganda