CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.

Slides:



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

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
LECTURE LECTURE 17 More on Inheritance, Virtual Inheritance, & Virtual Destructors, 17.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
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.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
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.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
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.
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 © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
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.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Class A { public : Int x; A()
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Inheritance, Polymorphism, and Virtual Functions
CS212: Object Oriented Analysis and Design
Inheritance II CMSC 202.
Polymorphism & Virtual Functions
Lesson 14 Copy and Assignment
Polymorphism Lec
Abstract Classes AKEEL AHMED.
Inheritance, Polymorphism, and Virtual Functions
CMSC 202 Lesson 22 Templates I.
Polymorphism 1 CMSC 202.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism CMSC 202, Version 4/02.
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Polymorphism 2 CMSC 202.
Templates I CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
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.
Computer Science II for Majors
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

CMSC 202 Lesson 19 Polymorphism 2

Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned int i = 1; i < customers.size(); ++i) { for (unsigned int j = i - 1; j >= 0; --j) { if (customers.at(j)->GetUsername() > customers.at(j+1)->GetUsername()) { Customer* temp = customers.at(j); customers.at(j) = customers.at(j+1); customers.at(j+1) = temp; }

Review Polymorphism –Ability to dynamically decide which method to call –C++ Base class pointer Derived class object ‘virtual’ keyword –Run-time Call method on pointer Runs method of the derived class

What about destructors? Imagine the following: class Animal { public: ~Animal(); }; class StarFish : public Animal { public: StarFish(); ~StarFish(); void RegrowArm(int i); void LoseArm(int i); private: Arm* arms; }; StarFish::StarFish() { arms = new Arm[5]; } StarFish::~StarFish() { delete [] arms; arms = NULL; } int main() { Animal* a = new StarFish(); delete a; a = NULL; } What happens here? Oh No! Only an Animal is destroyed!!!

Virtual Destructors Remember –Static binding means that we call the POINTER’s method –Dynamic binding means that we call the OBJECT’s method Requires the ‘virtual’ keyword Rule of thumb? –If a class has one or more virtual method – give it a virtual destructor! Expect this class to be a base class, eventually Let’s rewrite that class…

Virtual Destructors Imagine the following: class Animal { public: virtual ~Animal(); }; class StarFish : public Animal { public: StarFish(); ~StarFish(); void RegrowArm(int i); void LoseArm(int i); private: Arm* arms; }; StarFish::StarFish() { arms = new Arm[5]; } StarFish::~StarFish() { delete [] arms; arms = NULL; } int main() { Animal* a = new StarFish(); delete a; a = NULL; } What happens here? Dynamic binding – a StarFish is destroyed!!!

Designing Inheritance For Base Class –Methods Identify common operations of ALL derived classes Identify which are type-independent –These are (pure) virtual and will be overridden Identify access level for each method –Data Members Identify common data of ALL derived classes Identify access level for each data member

Aggregation Problem? class Zoo { public: Zoo(const Zoo& zoo); private: vector animals; }; Zoo::Zoo(const Zoo& zoo) { for (unsigned i = 0; i < zoo.animals.size(); ++i) { animals.push_back( ___________________________ ); } What goes here? new Animal( *zoo.animals.at(i) ) new Lion( *zoo.animals.at(i) ) new Gecko( *zoo.animals.at(i) ) new StarFish( *zoo.animals.at(i) ) new Penguin( *zoo.animals.at(i) )

Aggregation Solution Clone() –Define a virtual method named Clone() –Returns a pointer to current type –Override in derived classes –Might be pure virtual in base class Example: –virtual Animal* Clone() const = 0; –virtual Lion* Clone();

Revised Animal Hierarchy class Animal { public: virtual ~Animal(); virtual Animal* Clone() const = 0; }; class StarFish : public Animal { public: StarFish(const Starfish& s); StarFish* Clone() const; }; class Lion : public Animal { public: Lion(const Lion& l); Lion* Clone() const; }; StarFish* StarFish::Clone() const { return new StarFish( *this ); } Lion* Lion::Clone() const { return new Lion( *this ); } How does this work? Aren’t the signatures different??

Revised Zoo class Zoo { public: Zoo(const Zoo& zoo); private: vector animals; }; Zoo::Zoo(const Zoo& zoo) { for (unsigned i = 0; i < zoo.animals.size(); ++i) { animals.push_back( zoo.animals.at(i)->Clone() ); }

Inheritance in Real Life Vehicles Sports Entertainment Products Computers Music Writing Utensils Food Restaurants Data Structures Basically – anything that you can classify into a set of categories or hierarchy of categories!

Challenge Pick an object from the world around you Define an inheritance hierarchy for that item Demonstrate the use of virtual destructors in your hierarchy Demonstrate cloning in your hierarchy