Object-Oriented Programming in C++ Lecture 7 Polymorphism.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Inheritance and Polymorphism CS351 – Programming Paradigms.
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Pointer Data Type and Pointer Variables
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.
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.
Polymorphism &Virtual Functions
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
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.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Object-Oriented Programming in C++ More examples of Association.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Overview of C++ Polymorphism
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
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,
Class A { public : Int x; A()
Polymorphism.
CS212: Object Oriented Analysis and Design
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance: Polymorphism and Virtual Functions
Fundaments of Game Design
Overview of C++ Polymorphism
Inheritance and Polymorphism
Lecture 10 Concepts of Programming Languages
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.
Presentation transcript:

Object-Oriented Programming in C++ Lecture 7 Polymorphism

Introduction Last lecture we reviewed the concept of inheritance discussed how to implement inheritance in C++ This lecture we will introduce polymorphism explain virtual and pure virtual functions learn how to use polymorphism in C++ programs

LandVehicl e +move() Polymorphism many forms Greek –"poly" – many –"morph" form the same method can be called on different objects they may respond to it in different ways all Vehicles have a move method Cars and Truck drive Airplanes fly Vehicle +move() Airplane +move() CarTruck

Polymorphism example see handout for implementation of Vehicle class hierarchy #include "Vehicle.h" int main(){ Vehicle v ("Transporter 54"); Airplane a("Tornado 2431", 14); LandVehicle lv("My wheels"); Car c("Ford Anglia 22"); Truck t("Red pickup"); v.move(); a.move(); lv.move(); c.move(); t.move(); }

Output Vehicle constructor Airplane constructor Vehicle constructor Land vehicle constructor Vechicle constructor Land vehicle constructor Car constructor Vechicle constructor Land vehicle constructor Truck constructor Vehicle Transporter 54 moving Airplane Tornado 2431 flying Land Vehicle My wheels driving Land Vehicle Ford Anglia 22 driving Land Vehicle Red pickup driving

Which version of move is called in the previous example, the version of move to be called was determined at compile time –depends on the object type –for Airplane a, the Airplane version of move –for Car c, the Vehicle version of move what about a situation where the type is not known at compile time?

#include "Vehicle.h" void moveVehicle(Vehicle * v) { v->move(); } int main(){ Vehicle v ("Transporter 54"); Airplane a("Tornado 2431", 14); LandVehicle lv("My wheels"); Car c("Ford Anglia 22"); Truck t("Red pickup"); moveVehicle(&v); moveVehicle(&a); moveVehicle(&lv); moveVehicle(&c); moveVehicle(&t); }

Output …. Vehicle Transporter 54 moving Vehicle Tornado 2431 moving Vehicle My wheels moving Vehicle Ford Anglia 22 moving Vehicle Red pickup moving the moveVehicle method takes a pointer to any Vehicle object which could be any subtype of Vehicle however, the move method to call is determined at compile time –Vehicle version all vehicles move the same way

Polymorphic behaviour to get polymorphic behaviour, we would like the version of move() to be determined at run-time if moveVehicle is sent an Airplane object, it should get it to fly do this by using the virtual keyword in the first (base class) declaration of the polymorphic method class Vehicle { protected: string name; public: // other members virtual void move() { cout << "Vehicle " << name << " moving" << endl; } }; now it works

Polymorphic output Vehicle Transporter 54 moving Airplane Tornado 2431 flying Land Vehicle My wheels driving Land Vehicle Ford Anglia 22 driving Land Vehicle Red pickup driving

Polymorphism polymorphism allows us to use a pointer to a derived type object wherever a pointer to base type is expected Car c("Ford Anglia 22"); Vehicle * v2 = &c; v2->move(); Vehicle & v3 = c; v3.move(); only works for pointer and reference types they store an address – same size for all objects Land Vehicle Ford Anglia 22 driving

This won't work Airplane a("Tornado 2431", 14); Vehicle v2 = a; v2.move(); trying to fit an airplane into a space meant for any vehicle can call the move() method, but we've lost the wingspan member variable

Virtual destructors constructors cannot be virtual –the correct one is always called anyway destructors can be virtual you should specify a virtual destructor for any class which is overridden so that the object is cleaned up correctly otherwise you might not deallocate all the memory allocated to a subclass object

Pure virtual functions sometimes it is not sensible to implement a base class virtual function how does a Vehicle move? a pure virtual function does not have an implementation virtual void move() =0; the subclasses must implement it a class with a pure virtual function cannot be instantiated –if we made it virtual, how would it move? such a class is abstract

Comparison to Java in Java, all methods are virtual by default the keyword abstract is used to define pure virtual functions a class containing an abstract method must itself be abstract C++ is more flexible but can be confusing –why have virtual and non-virtual functions?

Virtual functions using a virtual function has an overhead the correct method cannot be bound to a method call at compile time it is determined at run-time by looking up the correct method address in a table this takes up time and space if polymorphism is not being used, this is expensive.

Summary In this lecture we have: introduced polymorphism explained virtual and pure virtual functions learned how to use polymorphism in C++ programs