3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter.

Slides:



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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
C/c++ 4 Yeting Ge.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
6/10/2015Assoc. Prof. Stoyan Bonev1 Assoc. Prof. Stoyan Bonev, PhD Computer Science dept American University in Bulgaria
7/2/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter 11.
Inheritance, Polymorphism, and Virtual Functions
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.
CSC3170 Introduction to Database Systems
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
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.
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.
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.
1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)
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 with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
10/21/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01b Title: C++ as O-O Prog Lang (Review) Reference: COS240 Syllabus.
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.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Chapter 10 Inheritance and Polymorphism
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Reference: COS240 Syllabus
COMPUTER 2430 Object Oriented Programming and Data Structures I
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism & Virtual Functions
ATS Application Programming: Java Programming
Polymorphism Lec
Object Oriented Analysis and Design
Virtual Functions Department of CSE, BUET Chapter 10.
CMSC 202 Templates.
Inheritance, Polymorphism, and Virtual Functions
Sampath Kumar S Assistant Professor, SECE
Polymorphism CMSC 202, Version 4/02.
Object Oriented Programming
Overview of C++ Polymorphism
Sampath Kumar S Assistant Professor, SECE
Java Programming: From the Ground Up
Chapter 11 Class Inheritance
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
Inheritance and Polymorphism
Presentation transcript:

3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter 11

3/20/2016Assoc. Prof. Stoyan Bonev2 Lecture Contents: Early binding and Late binding; –Regular member functions; –virtual member functions; –Methods accessed with pointers.

3/20/2016Assoc. Prof. Stoyan Bonev3 The 3 main OOP characteristics Data Encapsulation and Data Hiding Inheritance Polymorphism –Generally, the ability to appear in many forms –More specifically, in OOP it is the ability to redefine methods for derived classes –ability to process objects differently depending on their data type or classdata typeclass –Giving different meanings to the same thing

3/20/2016Assoc. Prof. Stoyan Bonev4 What is polymorphism? The concept of polymorphism (giving different meanings to the same thing). Early binding and Late binding. Regular /normal/ and virtual methods accessed via pointers. Examples: Base Derived1, Derived2 classes Person Professor, Student classes

3/20/2016Assoc. Prof. Stoyan Bonev5 Member Functions accessed with pointers Introduction to virtual functions: Virtual means existing in appearance (effect) but not in reality. Early binding operates on regular /normal/ member functions (methods) accessed with pointers. Late binding operates on virtual member functions (methods) accessed with pointers.

3/20/2016Assoc. Prof. Stoyan Bonev6 Early binding / Late binding virtual vs. non virtual methods First example on Polymorphism: method Show() class Base {... }; class Derived1 : public Base {... }; class Derived2 : public Base {... }; Derived1 drv1;Derived2 drv2;Base *ptr; ptr = &drv1;ptr->Show(); ptr = &drv2;(*ptr).Show();

3/20/2016Assoc. Prof. Stoyan Bonev7 Early Binding (at compile time) Normal, regular, non virtual methods class Base {public: void Show(){ cout << "\n Base:" ;}}; class Derived1 : public Base {public: void Show(){ cout << "\n Derived1:" ;} }; class Derived2 : public Base {public: void Show(){ cout << "\n Derived2:" ;} }; Major factor/condition: the type of the ptr pointer – object of Base class Derived1 drv1;Derived2 drv2;Base *ptr; ptr = &drv1;ptr->Show(); ptr = &drv2;(*ptr).Show(); OOP3aEarlyBinding.cpp

3/20/2016Assoc. Prof. Stoyan Bonev8 Early Binding (at compile time) The compiler ignores the contents of the pointer ptr and selects the member function that matches the type of the pointer. See figure on next slide.

3/20/2016Assoc. Prof. Stoyan Bonev9 Early Binding (at compile time)

3/20/2016Assoc. Prof. Stoyan Bonev10 Late Binding (at run time) Virtual methods class Base {public: virtual void Show(){ cout << "\n Base:" ; } }; class Derived1 : public Base {public: void Show(){ cout << "\n Derived1:" ;} }; class Derived2 : public Base {public: void Show(){ cout << "\n Derived2:" ;} }; Major factor/condition: contents of the ptr pointer – obj of derived class Derived1 drv1;Derived2 drv2;Base *ptr; ptr = &drv1;ptr->Show(); ptr = &drv2;(*ptr).Show(); OOP3aLateBinding.cpp

3/20/2016Assoc. Prof. Stoyan Bonev11 Late Binding (at run time) The compiler selects the function based on the contents of the pointer ptr, not on the type of the pointer. See figure on next slide.

3/20/2016Assoc. Prof. Stoyan Bonev12 Late Binding (at run time)

3/20/2016Assoc. Prof. Stoyan Bonev13 Late Binding (at run time) Pure virtual methods A virtual function with no body that is never executed. class Base { public: virtual void Show() = 0 ;}; class Derived1 : public Base { public: void Show(){ cout << "\n Derived1:" ;} }; class Derived2 : public Base { public: void Show(){ cout << "\n Derived2:" ;} };

3/20/2016Assoc. Prof. Stoyan Bonev14 Early binding / Late binding virtual vs. non virtual methods Second example on Polymorphism: method isOutstanding() class Person {... }; class Professor: public Person {... }; class Student: public Person {... }; OOP3bIsOutStanding.cpp

3/20/2016Assoc. Prof. Stoyan Bonev15 Virtual method IsOutstanding() class Person { protected: char name[20]; public: void GetName() { cout > name; } void ShowName() { cout << "\n Name is:" << name << " "; } bool virtual isOutStanding() = 0; };

3/20/2016Assoc. Prof. Stoyan Bonev16 Virtual method IsOutstanding() class Student : public Person { private: float score; public: void GetScore() { cout > score; } bool isOutStanding() { return (score > 98.0) ? true: false; } };

3/20/2016Assoc. Prof. Stoyan Bonev17 Virtual method IsOutstanding() class Professor : public Person { private: int NumPubs; public: void GetNumPubs() { cout << "\n Enter number of professor's publications:"; cin >> NumPubs; } bool isOutStanding() { return (NumPubs > 100) ? true: false; } };

3/20/2016Assoc. Prof. Stoyan Bonev18 Virtual method IsOutstanding() void main () { Person *PersPtr[100]; Student *StuPtr; Professor *ProPtr; int n=0; char choice; do{ cout > choice; if (choice == 's') { StuPtr = new Student; StuPtr->GetName(); StuPtr->GetScore(); PersPtr[n++] = StuPtr; } else { ProPtr = new Professor; ProPtr->GetName(); ProPtr->GetNumPubs(); PersPtr[n++] = ProPtr; } cout > choice; } while (choice == 'y'); // end of do for (int j=0; j<n; j++) { PersPtr[j]->ShowName(); if (PersPtr[j]->isOutStanding() == true) cout << " --outstanding person"; } // end of for }// end of main()

Polymorphism in C++ Conclusion

3/20/2016Assoc. Prof. Stoyan Bonev20 Polymorphism in C++ classified Dynamic polymorphism – present lecture –Based on late binding and virtual methods Static or ad-hoc polymorphism – lecture on the subroutine concept –Based on overloaded functions concept Parametric (generic) polymorphism – lecture on generic programming –Based oh template reserved word

21 More on OOP polymorphism Polymorphism in VBasic Polymorphism in C# Polymorphism in Java

22 Java // file OOPPolymorphism.java class Base { public void Show() { System.out.println( "\n Base:");} }; class Derived1 extends Base { public void Show() { System.out.println( "\n Derived1:");} }; class Derived2 extends Base { public void Show() { System.out.println( "\n Derived2:");} }; public class OOPPolymorhismJava { public static void main(String[] args) { Base ptr = new Base(); ptr.Show(); ptr = new Derived1(); ptr.Show(); ptr = new Derived2(); ptr.Show(); }

Thank You For Your Attention