1 Inheritance and Polymorphism Chapter 13. 2 Getting Started Continue the Cat Management example from previous presentation.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
1 Derived Classes and Inheritance Chapter Objectives You will be able to: Create derived classes in C#. Understand polymorphism. Write polymorphic.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
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.
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.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Chapter 10 Inheritance and Polymorphism
11 Introduction to Object Oriented Programming (Continued) Cats.
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.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Chapter -6 Polymorphism
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Overview of C++ Polymorphism
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
1 Project 2: Sorting Cats. Write a C++ console application to read a text file containing information about cats and output the information to the screen.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
1 The Standard Template Library Drozdek Section 3.7.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
A First Book of C++ Chapter 12 Extending Your Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
Polymorphism &Virtual Functions
Object-Oriented Programming
Chapter 5 Classes.
Polymorphism & Virtual Functions
ATS Application Programming: Java Programming
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance, Polymorphism, and Interfaces. Oh My
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism CMSC 202, Version 4/02.
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
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
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.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

1 Inheritance and Polymorphism Chapter 13

2 Getting Started Continue the Cat Management example from previous presentation.

Show Cats 3

Show_Cat.h #pragma once #include #include "cat.h" using namespace std; class Show_Cat : public Cat { private: std::string breed; std::string registration_id; public: Show_Cat(const std::string& name_, Date dob, double weight_, const Person* owner_, const std::string& breed_, const std::string& id); std::string Breed() const {return breed;}; std::string Registration_ID() const {return registration_id;}; void Display(ostream& os) const; ~Show_Cat(void); }; 4

Inheritance Class Show_Cats inherits the members and methods of class Cat. While there is no Weight() method in Show_Cat.h, we can still call Weight() for a Show_Cat. 5

Show_Cat.cpp void Show_Cat::Display(std::ostream& os) const { os << "Cat: " << name << endl; os << "Breed: " << breed << endl; os << "Registration ID: " << registration_id << endl; os << "Weight: " << weight << endl; } 6

Using an Inherited Method 7

Cats vs. Show Cats Let’s add a Display method to class Cat. Same output as operator<<() Same signature as in Show_Cat. In Cat.h: (inside the class definition) void Display(std::ostream& os) const; 8

Cat.cpp void Cat::Display(ostream& os) const { os << "Cat: " << name << endl; os First_Name() Last_Name() << endl; os Adr(); } 9

Using Inherited Methods If Show_Cats did not have a Display method, it would inherit that method from its base case, Cat. To demonstrate this, temporarily comment out the Display() method in class Show_Cat. In main() we still call Display() for Show_Cats. 10

Using Inherited Methods void Display_Cats(Show_Cat** Cat_Array, int Nr_Cats) { for (int i = 0; i < Nr_Cats; ++i) { Cat_Array[i]->Display(cout); cout Weight() << endl; cout << endl; } 11 In main.cpp

Using Inherited Methods 12

Restore Show_Cat Display Method Put back Display() method in class Show_Cat. Build and run again. 13

Using Show_Cat.Display() 14

Inherited Methods If a derived class defines a method with the same signature as a method in its base class, the new method overrides the base class method. Compare to overloading a method. If the derived class does not define a matching method, it inherits the base class method. 15

Back to Plain Cats In main.cpp, change Show_Cat declarations back to Cat. int Get_Cats(Cat** Cat_Array, int Max_Cats, Person** owners, int owner_count) … void Display_Cats(Cat** Cat_Array, int Nr_Cats) … int main() { Cat* cats[20]; 16

Setting a Cat* to a Show_Cat* In get_cats, continue to create Show_Cat. Cat_Array[count++] = new Show_Cat(name, dob, weight, owner, breed, id); We can set a Cat* variable to the address of a Show_Cat because a Show_Cat is a Cat. A Show_Cat object can be used anywhere a Cat object is called for. Build and run 17

Program Running 18 Output is from Cat::Display even though the cats are actually Show_Cats.

19 Virtual Methods Usually we would want to call the Display method defined in Show_Cat if a cat really is a Show_Cat. C++ provides a way to do this. We must declare the method in the base class virtual.

Virtual Methods In Cat.h, declare the Display method as virtual. virtual void Display(std::ostream& os) const; Build and run. 20

Using Virtual Display Method 21 This time we get the Display method from Show_Cat!

22 The Cats are Displayed as Show_Cats Even though the cats array is declared as an array of Cat* cats[i]->Display() called the Show_Cat Display() method. This is polymorphism. " Many forms" Only applies to methods called via pointers. Only applies to virtual methods.

23 Virtual Method Example How did the compiler know to call Show_Cat.Display() rather than Cat.Display()? Ans: It didn’t! The linkage to the Display() method was not resolved until run time. This is know as late binding.

24 Late Binding Late binding is the key to polymorphism. Virtual methods are called indirectly through a function pointer in an overhead area of the object. (not accessible to the programmer.) The specific object used for the call determines which version of the method is invoked.

25 Late Binding vs. Early Binding Before the method was declared as virtual, the call to cats[i]->Display() was resolved at compile time. The declaration of the cats array determined which version of Display() was invoked by the call cats[i]->Display(); This is early binding.

Mixing Cats Let’s make every other cat a plain cat and see what happens. In main.cpp function get_cats(): if (count % 2 == 0) { Cat_Array[count++] = new Show_Cat(name, dob, weight, owner, breed, id); } else { Cat_Array[count++] = new Cat(name, dob, weight, owner); } 26

Program Running 27

28 Summary Inheritance is a major feature of OOP. Permits us to extend existing classes without modifying the original code. Objects of a derived class can be used anywhere an object of the base class could be used. A Show_Cat is a Cat A derived class can override the definition of a method defined in its base class. New method with same signature.

29 Summary A class can declare a method as virtual. Invoked using a pointer to an object that could be either the base class or the derived class, even though the pointer is declared as a pointer to the base class. Polymorphism Caller does not need to know or care whether the object is a base class object or a derived class object. The right method will be called! End of Presentation