CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.

Slides:



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

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Dynamic Polymorphism Lecture-11. Method Polymorphism In a monomorphic language there is always a one to one relationship between a function name and it’s.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance, Polymorphism, and Virtual Functions
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.
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 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10 1.
Chapter 10 Inheritance and Polymorphism
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.
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.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Chapter -6 Polymorphism
Overview of C++ Polymorphism
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
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.
Polymorphism Lecture - 9.
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.
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]
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.
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance II CMSC 202.
Polymorphism & Virtual Functions
Polymorphism Lec
Inheritance, Polymorphism, and Virtual Functions
CMSC 202 Lesson 22 Templates I.
Polymorphism 1 CMSC 202.
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.
Inheritance: Polymorphism and Virtual Functions
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
C++ Polymorphism Reference and pointer implicit type casting
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 18 Polymorphism 1

Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere class AlarmClock { public: void RingAlarm(); private: Time alarmTime; }; void AlarmClock::RingAlarm() { if (alarmTime == GetCurrentTime()) cout << “Alarm!” << endl; } Assume methods and classes used are defined elsewhere. class ManualAlarm : AlarmClock { public: void RingAlarm(); }; void AlarmClock::RingAlarm() { if (alarmTime == GetCurrentTime()) RingBell(); }

Polymorphism in Inheritance “Many-shapes”  Allows a method to take on many type- dependent forms  Ability to manipulate objects in a type independent way  Only supported through pointers of base-type  Particular method is not decided until run-time

Pointers in Inheritance Base pointer can point to derived object  Derived object IS a base object  Cannot call derived-class methods via base pointer Derived pointer cannot point to base object

Binding Determination of which method in hierarchy to call Static Binding  Compiler determines binding Dynamic Binding  Run-time system determines binding  Must use keyword ‘virtual’ to indicate dynamic A “virtual” method…

Static Binding in Action class Animal { public: void Eat() { cout << “Food” << endl; } }; class Lion : public Animal { public: void Eat() { cout << “Meat” << endl; } }; int main() { Animal animal; Lion lion; animal.Eat(); lion.Eat(); Animal *animalPtr = new Animal(); animalPtr->Eat(); Animal *animalPtr = new Lion(); animalPtr->Eat(); return 0; } Food Meat Food

Dynamic Binding in Action class Animal { public: virtual void Eat(); }; void Animal::Eat() { cout << “Food” << endl; } class Lion : public Animal { public: virtual void Eat(); }; void Lion::Eat() { cout << “Meat” << endl; } int main() { Animal animal; Lion lion; animal.Eat(); lion.Eat(); Animal *animalPtr = new Animal(); animalPtr->Eat(); Animal *animalPtr = new Lion(); animalPtr->Eat(); return 0; } Food Meat Food Meat

What’s so great about it? Polymorphism  Collections of base-type pointers to derived objects If dynamic binding is used Calls derived method!  Example Animal *zoo[ 3 ]; zoo[ 0 ] = new Animal( “John” ); zoo[ 1 ] = new Giraffe( “Fred” ); zoo[ 2 ] = new Lion( “Susie” ); for (int i = 0; i < 3; ++i) { zoo[ i ]->SetName( “Jack” ); zoo[ i ]->Eat(); } class Animal { public: void SetName( string name) { m_name = name; } virtual void Eat() { cout << “Food” << endl; } private: string m_name; }; class Lion : public Animal { public: virtual void Eat() { cout << “Meat” << endl; } }; Base-class method Derived-class method

Pure Virtual Methods Base class does not define ANY implementation for a method Forces derived classes to override Compiler error if not Syntax (in class header): virtual retType method() = 0; class Animal { public: void SetName( string name) { m_name = name; } virtual void Eat() = 0; private: string m_name; }; class Lion : public Animal { public: virtual void Eat() { cout << “Meat” << endl; } };

Abstract Class Definition  Any class that has one or more pure virtual methods

Polymorphic Functions Non-member functions can be polymorphic  Pass a pointer or reference to a base-class object  Method calls are dynamically bound  Why is this cool? Old code calling new code when new derived classes are defined! void FeedAnimal( Animal *animal ) { animal->Eat(); }

Practice Modify the warmup so that the AlarmClock class does not implement RingAlarm Add an ElectricClock class that has a buzzer instead of a bell Create a collection of AlarmClocks and use polymorphism to ring their alarms

Challenge Define an Appliance class Define a Microwave class that inherits from Appliance  Microwaves have a button-based interface  Temperature on scale 1-10  Cooks for any number of minutes and/or seconds Define a Stove class that inherits from Appliance  Stoves have a knob-based interface  Temperate on scale  Cooks for any number of minutes Implement a dynamically bound hierarchy of methods that perform the following:  SetTemperature  SetTimer