Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 15 Polymorphism and Virtual Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Object Oriented Programming
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
Inheritance and Polymorphism CS351 – Programming Paradigms.
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,
Chapter 12: Adding Functionality to Your Classes.
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.
Polymorphism and Virtual Functions
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
Slide 1 Polymorphism and Virtual Functions. Slide 2 Learning Objectives  Virtual Function Basics  Late binding  Implementing virtual functions  When.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Day Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Surface Area of Cylinders Unit 5, Lesson 3. What is a Cylinder? Definition: –A three dimensional figure with 2 circular bases. Formula for Surface Area:
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Software Development
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Virtual Function and Polymorphism
CMSC 202 Polymorphism.
Polymorphism and Virtual Functions
Object-Oriented Analysis and Design
Advanced Program Design with C++
Inheritance and Polymorphism
Polymorphism.
Classes in C++ C++ originally called "C with classes":
Designing for Inheritance
Learning Objectives Inheritance Virtual Function.
Classes in C++ C++ originally called "C with classes":
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Inheritance and Polymorphism
Polymorphism CMSC 202, Version 4/02.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Compound Area Compound area is where a shape can be made up of other shapes. The area of a compound shape can be found by calculating the area of the shapes.
COP 3330 Object-oriented Programming in C++
Computer Science II for Majors
Presentation transcript:

Polymorphism, Abstraction and Virtual Functions

In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that virtual functions support: abstraction and polymorphism.

A virtual function is a function in a base class that forms part of the interface for a set of derived classes. It is declared virtual in the base class and may or may not have a definition in that class. It will have definitions in one or more of the derived classes. The purpose of a virtual function is to have one name, one prototype, and more than one definition so that the function’s behavior can be appropriate for each of the derived classes

A pure virtual function is a function that has no definition in the base class. An abstract class is a class with one or more pure virtual functions. A polymorphic class is a base class that supports a declared set of public virtual functions, together with two or more derived classes that define methods for those functions. Data members and non-virtual functions may be defined both in the base class and the derived classes, as appropriate. We say that the derived classes implement the polymorphic interface class.

Virtual functions. We create a virtual function when the same task must be done for all objects of all types in the hierarchy, but the method for doing this task depends on the particular representation of an object. For example, suppose the function calculatePay() must be defined for all employees, but the formula for the calculation is different for union members and professional staff. We want a function with one name that is implemented by three or more defining methods

Manager M Accountant A Clerk C,D For any given function call, we want the appropriate function to be called. For example, suppose we have declared the four objects given above.

Then if we call A.calculatePay() or M.calculatePay() we want the calculation to be made using the formula for professional staff. If we call C.calculatePay() or D.calculatePay(), we want the union formula used.

Polymorphic Classes

The purposes of polymorphism. To define an extensible set of representations for a class. – One or two may be defined at first, more added later. – It is a simple way to make an application extensible; you can build part of it now, part later, and be confident that the parts will work together smoothly

The purposes of polymorphism To allow a data structure to contain a mixture of items of different but related subtypes, such as the linked list of employees. – The data objects in this list are the Employees; the shape of each object indicates which subtype of Employee it belongs to.

The purposes of polymorphism To support run-time variability of types within a restricted set of related types, and the accompanying run-time dispatch (binding) of methods. The most specific appropriate method is dispatched. This lets us create different varieties of objects depending on input that is entered at run time.

Polymorphism Associating many meanings to one function. Multiple derived classes may define a function differently. An appropriate version is chosen based on the specific type of the object. E.g., Monthly salary is calculated differently for a student worker and a faculty member. In C++, virtual functions provide this capability

Figures Example Best explained by example Classes for several kinds of figures: – Rectangles, circles, ovals, etc. – Each figure an object of different class Rectangle data: height, width, center point  Circle data: center point, radius All derive from one parent class: Figure

Require function: draw() Different instructions for each figure.

Class Diagram

Figures Example: draw() Each class needs different draw() function. Can be called ‘draw’ in each class, so: Rectangle r; Circle c; r.draw(); c.draw(); Nothing new here yet….