Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
© 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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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.
Chapter 12: Adding Functionality to Your Classes.
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.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 8 More Object Concepts
(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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance in the Java programming language J. W. Rider.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Overview of C++ Polymorphism
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Using work from:
Java Programming Language
Advanced Java Topics Chapter 9
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Chapter 8: Class Relationships
Overview of C++ Polymorphism
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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Inheritance, Polymorphism and Virtual Functions

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance Inheritance is a way of creating a new class by starting with an existing class and adding new members The new class can replace or extend the functionality of the existing class Inheritance models the 'is-a' relationship between classes 11-2

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance - Terminology The existing class is called the base class –Alternates: parent class, superclass The new class is called the derived class –Alternates: child class, subclass 11-3

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance Syntax and Notation // Existing class class Base { }; // Derived class class Derived : public Base { }; Inheritance Class Diagram 11-4 Base Class Derived Class

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance of Members class Parent { int a; void bf(); }; class Child : public Parent { int c; void df(); }; Objects of Parent have members int a; void bf(); Objects of Child have members int a; void bf(); int c; void df(); 11-5

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Protected Members and Class Access protected member access specification: A class member labeled protected is accessible to member functions of derived classes as well as to member functions of the same class Like private, except accessible to members functions of derived classes 11-6

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Base Class Access Specification Base class access specification: determines how private, protected, and public members of base class can be accessed by derived classes 11-7

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Base Class Access C++ supports three inheritance modes, also called base class access modes: - public inheritance class Child : public Parent { }; - protected inheritance class Child : protected Parent{ }; - private inheritance class Child : private Parent{ }; 11-8

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Base Class Access vs. Member Access Specification Base class access not same as member access specification: –Base class access: determine access for inherited members –Member access specification: determine access for members defined in the class 11-9

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Access Specification Specified using the keywords private, protected, public class MyClass { private: int a; protected: int b; void fun(); public: void fun2(); }; 11-10

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Base Class Access Specification class Child : public Parent { protected: int a; public: Child(); }; member access base access

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Base Class Access Specifiers 1)public – object of derived class can be treated as object of base class (not vice- versa) 2)protected – more restrictive than public, but allows derived classes to know some of the details of parents 3)private – prevents objects of derived class from being treated as objects of base class

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Effect of Base Access private: x protected: y public: z private: x protected: y public: z private: x protected: y public: z Base class members x inaccessible private: y private: z x inaccessible protected: y protected: z x inaccessible protected: y public: z How base class members appear in derived class private base class protected base class public base class

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Constructors,Destructors and Inheritance By inheriting every member of the base class, a derived class object contains a base class object The derived class constructor can specify which base class constructor should be used to initialize the base class object 11-14

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Order of Execution When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class 11-15

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Order of Execution // Student – base class // UnderGrad – derived class // Both have constructors, destructors int main() { UnderGrad u1;... return 0; }// end main Execute Student constructor, then execute UnderGrad constructor Execute UnderGrad destructor, then execute Student destructor

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading Can also be done with inline constructors Must be done if base class has no default constructor 11-17

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Base Class Constructor class Parent { int x, y; public: Parent(int,int); }; class Child : public Parent { int z public: Child(int a): Parent(a,a*a) {z = a;} }; 11-18

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overriding Base Class Functions Overriding: function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class Not the same as overloading – with overloading, the parameter lists must be different 11-19

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Access to Overridden Function When a function is overridden, all objects of derived class use the overriding function. If necessary to access the overridden version of the function, it can be done using the scope resolution operator with the name of the base class and the name of the function: Student::getName(); 11-20

Copyright © 2011 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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Type Compatibility in Inheritance A pointer to a derived class can be assigned to a pointer to a base class. Another way to say this is: A base class pointer can point to derived class objects Animal *pA = new Cat; 15-22

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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using Type Casts with Base Class Pointers C++ uses the declared type of a pointer to determine access to the members of the pointed-to object If an object of a derived class is pointed to by a base class pointer, all members of the derived class may not be accessible Type cast the base class pointer to the derived class (via static_cast ) in order to access members that are specific to the derived class 15-24

Copyright © 2011 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 15-25

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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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";} } 15-27

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

Copyright © 2011 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" 15-29

Copyright © 2011 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 15-30

Copyright © 2011 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 15-31

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Virtual Functions If the member functions id() are declared virtual, then 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 15-32

Copyright © 2011 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";} } 15-33

Copyright © 2011 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 15-34

Copyright © 2011 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 15-35

Copyright © 2011 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 15-36

Copyright © 2011 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… 15-37

Copyright © 2011 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 15-38

Copyright © 2011 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 15-39

Copyright © 2011 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 15-40

Copyright © 2011 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 © 2011 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 15-42

Copyright © 2011 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 15-43