Anatomy of Polymorphism

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
ITF11006.NET Inheritance. Classes and Inheritance Constructors and Inheritance Modifiers Interfaces Operators.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
:PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Abstract classes and Interfaces. Abstract classes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 10 Inheritance and Polymorphism
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.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
CS250 Introduction to Computer Science II
Anatomy of a class Part II
Polymorphism 11-Nov-18.
Polymorphism 15-Nov-18.
Polymorphism CSCE 121 J. Michael Moore.
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Input Validation CSCE 121 J. Michael Moore
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Polymorphism - Greek for “many forms”
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Function Overloading CSCE 121 J. Michael Moore
Anatomy of Inheritance
Inheritance in Graphics
CISC/CMPE320 - Prof. McLeod
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
Event Driven Programming Anatomy – Handle
Polymorphism & Pointers
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Inheritance and Polymorphism
Anatomy of Polymorphism
Polymorphism & Pointers
COP 3330 Object-oriented Programming in C++
CS410 – Software Engineering Lecture #8: Advanced C++ III
Anatomy of Inheritance
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Anatomy of Polymorphism CSCE 121 J. Michael Moore Based on slides created by Carlos Soto.

A motivating example class Automobile { // ... public: double getRange() { // compute from gas } }; class Hybrid: public Automobile { // ... public: double getRange() { // compute from gas // and battery } };

Function overriding class Automobile { // ... public: virtual double getRange() { // compute from gas } };

Function overriding: virtual functions class Automobile { public: virtual double getRange() {/*...*/} }; class Hybrid: public Automobile { double getRange() {/*...*/}

Beware! Do not confuse overloading with overriding! The new function has a different signature! What we already know! The new function has the exact same signature!

Using overridden functions Automobile myauto; cout << myauto.getRange(); Hybrid myhybrid; cout << myhybrid.getRange(); Automobile* anotherauto = &myhybrid; cout << anotherauto->getRange(); Calling the base class / default version. Calling the overridden version. (Defined in derived class.) Note: this is overriding since the function signatures are the same!

Pure virtual functions Shape is an “abstract base class” Because it has a “pure virtual function” Objects of this type cannot be created / instantiated class Shape { // ... public: virtual double getArea() = 0; }; Indicates a “pure virtual function” For a derived class to be instantiated, this function MUST be defined.

Function hiding class Base { // ... public: void foo(int a) { } }; class Child: public Base { // ... public: void foo(int a) { } }; Not overriding. Notice: No “virtual” Hiding! Avoid!!!

Function hiding (which you should avoid) class Base { // ... public: void foo(char c) { } }; class Child: public Base { // ... public: void foo(int a) { } }; Note the signatures differ… Still hiding!

Accessing Hidden Things class Base { // ... public: void foo(int a) { } }; class Child: public Base { int main() { Base b; b.foo(7); Child c; c.foo(8); c.Base::foo(8); } Base version. Child version. Base version. Avoid!!!

Polymorphism Allows a single function call to automatically call the version of that function for the correct class type Even if you don’t know at compile-time what the type will be Sometimes called dynamic/runtime polymorphism