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



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

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Chapter 1 Fundamentals of Java Programming

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

History OOP languages Year Language 1967 Simula Smalltalk


Java Interview Questions and Answers

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

Java (12 Weeks) Introduction to Java Programming Language

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

CEC225 COURSE COMPACT

Advanced Data Structures

CIS 190: C/C++ Programming. Polymorphism

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Fundamentals of Java Programming

Introduction to Object-Oriented Programming

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

CS506 Web Design and Development Solved Online Quiz No. 01

The C Programming Language course syllabus associate level

Ch 7-1. Object-Oriented Programming and Classes

CS193j, Stanford Handout #10 OOP 3

C++ INTERVIEW QUESTIONS

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

Embedded Software Development with MPS

Chapter 5 Aspect Oriented Programming

CSE 1020 Introduction to Computer Science I A sample nal exam

Part I. Multiple Choice Questions (2 points each):

Java Programming Language

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

The Separation of Interface and Implementation in C++

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

13 Classes & Objects with Constructors/Destructors

Dennis Olsson. Tuesday 31 July

Classes Dennis Olsson

Class 16: Function Parameters and Polymorphism

Chapter 13 - Inheritance

CS 111 Classes I 1. Software Organization View to this point:

Object-Oriented Software Specification in Programming Language Design and Implementation

Syllabus Introduction to C++ Programming and Numerical Analysis Spring 2016

Glossary of Object Oriented Terms

An Internet Course in Software Development with C++ for Engineering Students

Course MS10975A Introduction to Programming. Length: 5 Days

Object-Oriented Programming

C Programming Language

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Objective C and iphone App

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Web Services for Management Perl Library VMware ESX Server 3.5, VMware ESX Server 3i version 3.5, and VMware VirtualCenter 2.5

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Course Title: Software Development

6.170 Tutorial 3 - Ruby Basics

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

An Incomplete C++ Primer. University of Wyoming MA 5310

Konzepte objektorientierter Programmierung

Patient Satisfaction Survey Results

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

Monitoring of Tritium release at PTC.

Algorithms, Flowcharts & Program Design. ComPro

Software Reuse. Software Engineering Course Given by: Arnon Netzer. To use parts of one product in another product with a different function.

Trends in Software Intensive Systems Development JACEK SZYMANSKI INFORMATION SYSTEMS CONSULTANCY

Object Oriented Design

OpenCL Static C++ Kernel Language Extension

Programmation 2. Introduction à la programmation Java

Syllabus for CS 134 Java Programming

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

The Interface Concept

Object-Oriented Programming in Java

PostgreSQL Functions By Example

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

WHITE PAPER A COMPARISON BETWEEN RELATIONAL AND OBJECT ORIENTED DATABASES FOR OBJECT ORIENTED APPLICATION DEVELOPMENT JONATHAN ROBIE DIRK BARTELS

Object Oriented Software Design II

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

C++FA 5.1 PRACTICE MID-TERM EXAM

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

.NET Overview. David Smith. Today s s Topics. Why am I here? A tool. Microsoft s s Vision for.net

MODULE 17 POLYMORPHISM

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Lecture 12: Entity Relationship Modelling

Web Application Development for the SOA Age Thinking in XML

El Dorado Union High School District Educational Services

Roles in Building Web Applications using Java

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Java CPD (I) Frans Coenen Department of Computer Science


How to Design APIs for Cryptographic Protocols. Lior Malka. Crypto Group, University of Maryland, USA

Subject Name: Object Oriented Programming in C++ Subject Code:

Objects-First vs. Structures-First Approaches to 00 Programming Education: A Replication Study

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:

Agile Business Suite: a 4GL environment for.net developers DEVELOPMENT, MAINTENANCE AND DEPLOYMENT OF LARGE, COMPLEX BACK-OFFICE APPLICATIONS

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Transcription:

A Review of the OOP Polymorphism Concept Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1

Outline Overview Type Casting and Function Ambiguity Virtual Functions, Pure Virtual Functions, and Abstract Class Non Type Casting Programming, and Scalability Problems Class Composition Framework Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 2

Polymorphism: Overview An important t concept in object oriented programming (OOP) A polymorphic function acts differently under different context. has different implementation under different context. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 3

Polymorphism: Inheritance Receptionist i t and how they greet customers Friendly: Good morning. How can I help you today? Moody: What do you 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

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

Polymorphism: Example 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

Outline Overview Type Casting and Function Ambiguity Virtual Functions, Pure Virtual Functions, and Abstract Class Non Type Casting Programming, and Scalability Problems Class Composition Framework Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 8

Ex1: Polymorphism: Ex 3 Three 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_pt, *r_pt; f_pt = new FriendlyReceptionist(); m_pt = new MoodyReceptionist(); r_pt = new RudeReceptionist(); } cout<<"\n------------ Friendly Receptionist ----\n"; f_pt->greet(); cout<<"\n------------ Moody Receptionist ----\n"; m_pt->greet(); cout<<"\n------------ Rude Receptionist ----\n"; r_pt->greet(); cout<<"-----------------------------------------\n"; Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 10

Polymorphism: Ex 3 After running: >>./receptionist ------------ Friendly Receptionist --------- Say: Good morning. How can I help you today? ------------ Moody Receptionist i --------- Say: What do you want? ------------ Rude Receptionist ------------- Q:? Say: What do you want? Why? type casting Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 11

Polymorphism: Ex 3 *r_pt is of class MoodyReceptionist MoodyReceptionist i t *m_pt, *r_pt; Two steps process when invoking r_pt = new RudeReceptionist(); 2 1 1. Create a RudeReceptionist object 2. Cast the created object to be a MoodyReceptionist object Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 12

Polymorphism: Ex 3 The object *r_pt was doing what Moody Receptionist does What if we want the object *r_pt what RudeReceptionist do? 1. Declare *r_pt as RudeReceptionist Scalability l problem!! 2. USE VIRTUAL FUNCTION Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 13

Outline Overview Type Casting and Function Ambiguity Virtual Functions, Pure Virtual Functions, and Abstract Class Non Type Casting Programming, and Scalability Problems Class Composition Framework Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 14

Virtual Functions Carry inheritance through type casting. Bind the implementation to the construction type, not the declaration type E.g., MoodyReceptionist *r_pt; r_pt = new RudeReceptionist(); Which implementation of a virtual function greet() will be used for r_pt->greet()? RudeReceptionist Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 15

Virtual Functions Why this is a good idea? Declare a very general pointer: Receptionist *a,*b; Create the object on the fly as you wish a = new RudeReceptionist(); b = new FriendlyReceptionist(); At declaration,,you do not need to think what type of receptionist you want. Decide later at the construction. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 16

Polymorphism: Ex 3 After running: >>./receptionist ------------ Friendly Receptionist --------- Say: Good morning. How can I help you today? ------------ Moody Receptionist 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

Virtual Functions: Ex4 Ex 3: >>./receptionist ------------ Friendly Receptionist --------- Say: Good morning. How can I help you today? ------------ 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

Virtual Functions: Ex4 Based on Ex 3, modify class Receptionist class Receptionist { public: virtual void greet() {cout<<"say:\n";}; }; At run time, Note: Virtuality is inheritable. You only need to do it once at the base class. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 19

Pure Virtual Functions Make virtuality mandatory Ex5: From Ex4, modify class Receptionist class Receptionist { }; public: virtual void greet()=0; Obtain the same results. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 20

Pure Virtual Functions What s difference? Implementation is now manadatory. If no implementation in the derived class, Virtual: Use that of the base class Pure virtual: The class is non-instantiable!!; An attempt compilation error Summary Base Class Derive Class Virtual Implementation Provide Optional Declaration Virtual Optional Pure Implementation None Mandatory virtual Declaration Virtual, =0 Mandatory Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 21

Abstract Class A class with at least one pure virtual function. Incomplete; Non-instantiable. Only have what to do Derive class providing how to do is complete and instantiable. The use of polymorphism has 3 main components: 1. A pure virtual function 2. An abstract class 3. An instantiable class Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 22

Pure Virtual Functions Last catch: Related declaration If the base class is abstract, you cannot leave the derived class unimplemented. Otherwise compilation error for object instantiation. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 23

Outline Overview Type Casting and Function Ambiguity Virtual Functions, Pure Virtual Functions, and Abstract Class Non Type Casting Programming, and Scalability Problems Class Composition Framework Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 24

Type Casting v.s. Non-Type Casting The ambiguity occurs due to type casting: MoodyReceptionist *r_pt; r_pt = new RudeReceptionist(); ecept Can we not use type casting? Yes Then, what s problem? Type casting programming is more elegant and scalable!! Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 25

Non-Type Casting Programming Let make the receptionist concept more interesting: Create a company, Employ a receptionist, and Serve customers About the company: Base class Company: Derived class MoodyCompany: Employ a moody receptionist. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 26

Non-Type Casting Programming: Ex6 class Company { public: void serve() { }; greet(); cout<<"\nserving the customer... \n"; }; void greet () {}; class MoodyCompany : public Company { public: MoodyCompany(){employee_ = new MoodyReceptionist;}; void greet(){employee_->greet();}; private: MoodyReceptionist* employee_; }; Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 27

Non-Type Casting Programming: Ex6 int main() { MoodyCompany my_company; my_company.serve(); return 0; } greet(); Q: Why saying this? A: The employee belongs to class MoodyReceptionist >>./company Say: What do you want? Serving the customer cout<< Serving the customer Here, we do not use virtuality. Do we need one? What if we do not use it? Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 28

A Scalablility Problem Q: What do we do if we want have the company to greet customer nicely? A: Put a friendly receptionist in the company Q: How do we do that in C++? A: class FriendlyCompany : public Company { public: FriendlyCompany(){employee_ = new FriendlyReceptionist;}; void greet(){employee_->greet();}; private: FriendlyReceptionist* employee_; }; Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 29

Q: What s Whats problem? A Scalability Problem A: We do not reuse the existing codes! Q: How many company class do we need (excluding the base class) for 3 types of receptionist? A: ( 3 classes s ) Q: What if we have 3 types of receptionist, 5 types of engineers, and 10 types of accountants. How many classes? A: ( 3x5x10 = 150 classes ) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 30

Outline Overview Type Casting and Function Ambiguity Virtual Functions, Pure Virtual Functions, and Abstract Class Non Type Casting Programming, and Scalability Problems Class Composition Framework Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 31

Class Composition Framework A solution to the scalability problem Consider the previous example: Companies with 3 receptionists, 5 engineers, 10 accountants Class composition defines One company Class Company Put people in company without deriving the company class Use function hire( ) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 32

Class Composition Framework: Ex7 class Company { public: void serve() { recp_->greet(); cout<<"\nserving the customer... \n"; }; void hire(receptionist* r) { recp_ = r; }; void hire(engineer* e) { }; private: }; engr_ = e; int main() { Receptionist* recp_; Engineer* engr_; } MoodyReceptionist *m_pt= new MoodyReceptionist(); GoodEngineer *e_pt = new Company my_company; my_company.hire(m_pt); my_company.hire(e_pt); my_company.serve(); return 0; GoodEngineer(); Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 33

Class Composition Framework Come back to our problem. We have company with 3 receptionist, 5 engineers, 10 accountants Q: How many company classes do we need (excluding employee s classes)? A: No Type Casting: (150 classes ) A: Class composition: ( 1 class ) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 34

Class Composition Framework: Main Components 1. An abstract class: Receptionist 2. An derived class: MoodyReceptionist 3. An abstract user class: Company 4. A user class: main() Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 35

Class Composition Framework class Company { public: void serve() { }; private: }; recp_->greet(); From Ex7, cout<<"\nserving the customer... \n"; }; void hire(receptionist* r) { recp_ = r; }; void hire(engineer* e) { engr_ = e; i i () { Receptionist* recp_; Engineer* engr_; FriendlyReceptionist Make change in the user class only. What do we have to do to have the company greet customers nicely? Hire a new reception!!; How? int main() { MoodyReceptionist *m_pt= new MoodyReceptionist(); GoodEngineer *e_pt = new Company my_company; my_company.hire(m_pt); my_company.hire(e_pt); my_company.serve(); return 0; Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. } GoodEngineer(); 36

Outline Overview Type Casting and Function Ambiguity Virtual Functions, Pure Virtual Functions, and Abstract Class Non Type Casting Programming, and Scalability Problems Class Composition Framework Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 37

Polymorphism: Summary An important OOP concept Act differently under different context Example--Receptionist: Friendly, Moody, Rude Type casting problem Regular function ( declaration ) type Virtual function ( construction ) type Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 38

Summary Pure virtual function: ( implementation is mandatory ) Abstract class Definition: ( At least one p.v. function ) Usage: ( Non-instantiable ) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 39

Summary Scalability of non-type casting programming Class composition framework Abstract class Derived class Abstract user class User class Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 40