 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
 2008 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System UML.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Cpt S 122 – Data Structures Polymorphism
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
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming Polymorphism Session 07 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Polymorphism Polymorphism occurs with objects instantiated from a set of classes related by inheritance to the same ancestor class. –Each class provides.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Object Oriented Programming
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
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.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
 2006 Pearson Education, Inc. All rights reserved Templates.
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Lecture 11: Virtual Function and Pure Virtual Function
Polymorphism & Virtual Functions
CS250 Introduction to Computer Science II
Object-Oriented Programming (OOP) Lecture No. 28
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 20- Virtual Functions and Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Chapter 20 - C++ Virtual Functions and Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Chapter 11 Class Inheritance
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:

 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual Functions 20.4Abstract Base Classes and Concrete Classes

 2000 Prentice Hall, Inc. All rights reserved Introduction virtual functions and polymorphism –Design and implement systems that are more easily extensible –Programs written to generically process objects of all existing classes in a hierarchy

 2000 Prentice Hall, Inc. All rights reserved Type Fields and switch Statements switch statement –Take an action on a object based on its type –A switch structure could determine which print function to call based on which type in a hierarchy of shapes Problems with switch –Programmer may forget to test all possible cases in a switch. Tracking this down can be time consuming and error prone virtual functions and polymorphic programming can eliminate the need for switch

 2000 Prentice Hall, Inc. All rights reserved Virtual Functions virtual functions –Used instead of switch statements –Declaration: Keyword virtual before function prototype in base class virtual void draw() const; –A base-class pointer to a derived class object will call the correct draw function –If a derived class does not define a virtual function it is inherited from the base class

 2000 Prentice Hall, Inc. All rights reserved Virtual Functions (II) ShapePtr->Draw(); –Compiler implements dynamic binding –Function determined during execution time ShapeObject.Draw(); –Compiler implements static binding –Function determined during compile-time

 2000 Prentice Hall, Inc. All rights reserved Abstract and Concrete Classes Abstract classes –Sole purpose is to provide a base class for other classes –No objects of an abstract base class can be instantiated Too generic to define real objects, i.e. TwoDimensionalShape Can have pointers and references –Concrete classes - classes that can instantiate objects Provide specifics to make real objects, i.e. Square, Circle

 2000 Prentice Hall, Inc. All rights reserved Abstract and Concrete Classes (II) Making abstract classes –Declare one or more virtual functions as “pure” by initializing the function to zero virtual double earnings() const = 0; Pure virtual function