A Review of the OOP Polymorphism Concept

12 downloads 1411 Views 446KB Size Report
A Review of the OOP Polymorphism Concept. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1 ...
A Review of the OOP Polymorphism Concept

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

1

O li Outline • Overview • Type Casting and Function Ambiguity • Virtual Functions,, Pure Virtual Functions,, and Abstract Class • Non Type Casting Programming, and S l bilit P Scalability Problems bl • Class Composition Framework • Summary S mm

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

2

P l Polymorphism: hi Overview O i • A An iimportant t t conceptt iin object bj t oriented programming (OOP) • A polymorphic l hi f function ti

– acts differently under different context. context – has different implementation under different context.

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

3

P l Polymorphism: hi Inheritance I h i • Receptionist R ti i t and dh how th they greett customers t

– Friendly: “Good morning. How can I help you today?” – Moody: y “What do you y want?” – Rude: “What do you want? I’m busy. Come back later”

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

4

P l Polymorphism: hi Ex E 1

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

5

P l Polymorphism: hi Example E l

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

6

Polymorphism: Ex 2 • Modify class MoodyReceptionist

Why?: Receptionist says nothing.

INHERITANCE

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

7

O li Outline • Overview • Type Casting and Function Ambiguity • Virtual Functions,, Pure Virtual Functions,, and Abstract Class • Non Type Casting Programming, and S l bilit P Scalability Problems bl • Class Composition Framework • Summary S mm

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

8

Polymorphism: Ex 3 • Ex1:

• Three variables, variables Three classes • No type casting!

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

9

Polymorphism: Ex 3 • Based on Ex1: Let implement type casting main() { FriendlyReceptionist *f_pt; MoodyReceptionist *m m_pt, pt, *r r_pt; pt; f_pt = new FriendlyReceptionist(); m_pt = new MoodyReceptionist(); r_pt = new RudeReceptionist(); cout>./receptionist >> /receptionist ------------ Friendly Receptionist --------Say: Good morning. How can I help you today? ------------ Moody Receptionist i i Say: What do you want?

---------

------------ Rude Receptionist ------------Say: What do you want?

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

17

Vi Virtual l Functions: F i Ex4 E 4 • Ex E 3: 3 >>./receptionist ------------ Friendly Receptionist --------Say: Good d morning. i How can I h help l you today? d ------------ Moody Receptionist Say: What do you want?

---------

------------ Rude Receptionist ------------Say: What do you want?

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

18

Vi Virtual l Functions: F i Ex4 E 4 • Based on Ex 3,, modify y class Receptionist p class Receptionist { public: virtual void greet() {coutgreet(); coutgreet(); cout