23
07/06/22 Assoc. Prof. Stoyan Bonev 1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter 11

3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 1

COS220 Concepts of PLs AUBG, COS dept

Lecture 23

OOPPolymorphism

Reference: R.Sebesta, Chapter 12Lafore, Chapter 11

Page 2: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 2

Lecture Contents:

• Early binding and Late binding;– Regular member functions;–virtual member functions;– Methods accessed with pointers.

Page 3: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 3

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 class– Giving different meanings to the same thing

Page 4: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 4

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, Derived2classes

Person Professor, Student

classes

Page 5: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 5

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.

Page 6: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 6

Early binding / Late binding

virtual vs. non virtual methodsFirst 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();

Page 7: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 7

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

Page 8: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 8

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.

Page 9: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 9

Early Binding (at compile time)

Page 10: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 10

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

Page 11: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 11

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.

Page 12: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 12

Late Binding (at run time)

Page 13: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 13

Late Binding (at run time)

Pure virtual methodsA 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:" ;} };

Page 14: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 14

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

Page 15: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 15

Virtual method IsOutstanding()class Person { protected: char name[20]; public:

void GetName() { cout << "\nEnter name:"; cin >> name; } void ShowName() { cout << "\n Name is:" << name << " "; } bool virtual isOutStanding() = 0;

};

Page 16: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 16

Virtual method IsOutstanding()class Student : public Person{ private: float score; public:

void GetScore() { cout << "\n Enter student's score:"; cin >> score; } bool isOutStanding() {return (score > 98.0) ? true: false; }

};

Page 17: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 17

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; }

};

Page 18: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 18

Virtual method IsOutstanding()void main (){ Person *PersPtr[100]; Student *StuPtr; Professor *ProPtr; int n=0; char choice; do {

cout << "\n Enter student or professor (s/p)?:"; cin >> 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 << "\n\n Enter another (y/n)?:"; cin >> 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()

Page 19: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

Polymorphism in C++

Conclusion

Page 20: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

05/14/23 Assoc. Prof. Stoyan Bonev 20

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

Page 21: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

21

More on OOP polymorphism •Polymorphism in VBasic

•Polymorphism in C#

•Polymorphism in Java

Page 22: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

22

Java // file OOPPolymorphism.javaclass 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(); }}

Page 23: 3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism…

Thank YouFor

Your Attention