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