Introduction to inheritance

Size: px
Start display at page:

Download "Introduction to inheritance"

Transcription

1 C++ Inheritance and Polymorphism CSIE Department, NTUT Woei-Kae Chen 1 Introduction to inheritance Inheritance Derived class inherits data members and member functions from base class Single inheritance inherits from one base class Multiple inheritance inherits from multiple base classes Types of inheritance public private protected Is a relationship Alternative to composition Base Derived 2 1

2 Inheritance Examples 1 Base class Student Shape Loan Employee Account Derived classes GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount 3 Inheritance Examples 2 Base class ( 或 super class) CommunityMember Employee Student Alumnus Single inheritance Faculty Staff Single inheritance Administrator Teacher Single inheritance Derived class ( 或 subclass) AdministratorTeacher Multiple inheritance 4 2

3 Inheritance Examples 3 Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron 5 Inheritance: is-a relationship is-a Inheritance A derived class object can be treated as a base class object Examples a faculty is an employee a circle is a shape a car is a vehicle Never use inheritance, if the derived object is not a base object. 6 3

4 UML Example (C++): Inheritance X Shape UML: Class UML: Inheritance Y Circle class X { class Y : public X { 7 Objects and Pointers B D B bo, *bp; D do, *dp; D Instance Pointer Instance (do) Pointer (dp) Size depends on the class D Size is fixed (e.g., 32 bits) dp 8 4

5 Casting Inherited Objects and Pointers Up Down B D B bo, *bp; D do, *dp; Upcasting Downcasting Object Allowed safe bo = do; do = bo Not allowed Pointer Allowed safe bp = dp; Casting allowed dp = (D *) bp; (Next Page) Not safe or C++ static_cast 9 Use operator= to enable upcasting Upcasting/Downcasting Objects base class object derived class object class B { class D : public B { int b; int d; B bo; D do; bo = do; bo // ok derived class object base class object do = bo; // error do = (D) bo; // error do = static_cast<d> (bo); // error b memory model do b d Use operator= to enable downcasting 10 5

6 Upcasting Pointers pointer to base class object pointer to derived class object class B { class D : public B { void bf(); void df(); b B int b; int d; do d D B *bp; D do,*dp=&do; bp bp = dp; // ok: upcast dp bp = &do; // ok: upcast bp->bf(); // ok bp->df(); // error 11 Downcasting Pointers pointer to derived class object pointer to base class object class B { class D : public B { void bf(); void df(); int b; int d; b B do d D B bo,*bp; D do, *dp; bp = &do; // ok: upcast bp dp = bp; // error bp dp = (D *) bp; // ok: downcast dp->bf(); // ok dp = (D *) &bo; // legal: but, buggy 12 6

7 Introduction to polymorphism How do you attack an enemy? (next page) Select a number of soldiers Right click the enemy WC1 WC2 Introduction to polymorphism Select a number of soldiers We need a list that can store objects of different types (swordman, archery, cavalry, ) discussed later Soldier Swordsman Archery Cavalry 7

8 投影片 14 WC1 Woei-Kae Chen, 2014/9/22 WC2 Woei-Kae Chen, 2014/9/22

9 WC1 WC2 Introduction to polymorphism Right click the enemy We need to request different objects (swordman, archery, ) to do the same thing (attack the enemy) in different ways (using different weapons) What s wrong? // Attack the enemy for each soldier s in list { if (s.type == SWORDMAN) s.attack_with_sword(); else if (s.type == ARCHERY) s.attack_with_arrow(); Introduction to polymorphism 亞拉岡 16 8

10 投影片 15 WC1 Woei-Kae Chen, 2014/9/22 WC2 Woei-Kae Chen, 2014/9/22

11 Introduction to polymorphism 殺 17 Introduction to polymorphism 半獸人 18 9

12 Introduction to polymorphism 亞拉岡 半獸人 19 Introduction to polymorphism 士兵. 揮劍弓箭手. 射箭 20 10

13 Introduction to polymorphism // Attack the enemy for each soldier s in list { if (s.type == SWORDMAN) s.attack_with_sword(); else if (s.type == ARCHERY) s.attack_with_arrow(); // 將軍的 code for each soldier s in list { s. 攻擊 (); // 將軍的 code for each soldier s in list { if (s 是士兵 ) s. 揮劍 (); else if (s 是弓箭手 ) s. 射箭 (); else if (s 是大砲 ) s. 發射 (); Not a good design: 1. The soldiers should be smart 2. The General has too many responsibilities 3. When a new kind of soldiers is created, the general s code must be modified Introduction to polymorphism Another example How do you enlarge a number of graphical objects (in Powerpoint)? 22 11

14 Introduction to polymorphism Processing a list of unknown shapes 23 Declaring a list of unknown shapes Shape s[100]; // Bad Circle s[100]; // Bad Triangle s[100]; // Bad Shape *s[100]; // OK, 但怎麼呼叫? Solution: 設成 virtual function Shape 1. 混合 type 的 shape 怎麼儲存在 array? 2. 怎麼呼叫? s[0] = new Circle(); s[1] = new Triangle(); for (i=0;i<100;i++) s[i]->;//??? Circle Rectangle Triangle Does NOT work 24 12

15 Effect of Virtual Functions Example (non-virtual) class X { public: void f() { cout << x ; class Y : public X { public: void f() {cout<< y ; X a; a.f(); // x Y b; b.f(); // y X *p=&a; p->f(); X *p=&b; p->f(); // x // x Example (virtual) class X { public: virtual void f(){ cout << x ; class Y : public X { public: void f() {cout<< y ; X a; a.f(); // x Y b; b.f(); // y X *p=&a; p->f(); X *p=&b; p->f(); // x // y virtual 只對 pointer 或 reference 有效 (dynamic binding) 25 Virtual Functions for the shape problem 設成 virtual function Shape Java? Circle Rectangle Triangle Shape *s; s = new Circle(); s->; // 呼叫 circle 的 s = new Triangle(); s->; // 呼叫 Triangle 的 Shape *s[100]; // 設定 s[0..99] 分別指向哪一種物件 for (int i=0;i<100;i++) s[i]->; // 依據實際的 type 呼叫 26 13

16 Virtual Functions & Polymorphism Polymorphism Ability for objects of different classes to respond differently to the same function call Implemented through virtual functions C++ 允許使用 base class pointer( 或 reference) 呼叫 derived class 的 member function C++ chooses the correct overridden function in object Dynamic binding Function determined during execution time ShapePtr->Draw(); Static binding 已知 derivedobject Function determined during compile-time ShapeObject.Draw(); Pointer 可能指向任何 derivedobject 27 Dynamic Binding Static Binding Dynamic binding (run-time) 用 base class 的 pointer 或 reference 呼叫 virtual function Static binding (compile time) 其他 ( 用 object 呼叫 virtual/non-virtual function, 用 pointer 或 reference 呼叫 non-virtual function class X { public: void f1() { cout << xf1 ; virtual void f2() { cout << xf2 ; class Y : public X { public: void f1(){cout<< yf1 ; void f2(){cout<< yf2 ; X a; a.f1(); // xf1 static a.f2(); // xf2 static Y b; b.f1(); // yf1 static b.f2(); // yf2 static X *p=&a; p->f1(); // xf1 static p->f2(); // xf2 dynamic X *pb=&b; pb->f1(); // xf1 static pb->f2(); // yf2 dynamic 28 14

17 Abstract and Concrete Classes Abstract classes To provide a base class for other classes No objects can be instantiated Declare one or more pure virtual functions Example virtual void = 0; Concrete classes Objects can be instantiated No unimplemented virtual functions Implement all unimplemented virtual functions 29 Abstract and Concrete Classes C++ example class Shape { public: virtual void = 0; void setcolor(int c) { color = c; private: int color; virtual is inherited Abstract class Concrete class class Rectangle : public Shape { public: void { void getarea() { virtual void f() { Shape setcolor() Rectangle Not pure virtual 30 15

18 Abstract and Concrete Classes UML example 斜體字 pure virtual function 正體字有 implementation Shape setcolor() 斜體字 abstract class 正體字 concrete class Circle Rectangle Triangle 31 Virtual Destructors Problem: How to delete an object with a base class pointer? Example: 32 16

19 Solution: Virtual Destructors declare a virtual (base-class) destructor class X { public: virtual ~X(); // virtual destructor private: class Y : public X { X *pa = new Y; // use *pa delete pa; 33 WC1 WC2 Introduction to polymorphism Which code runs faster? // 將軍的 code for each soldier s in list { if (s.type == SWORDMAN) s.attack_with_sword(); else if (s.type == ARCHERY) s.attack_with_arrow(); Switch? // 將軍的 code for each soldier s in list { s. 攻擊 (); 17

20 投影片 34 WC1 Woei-Kae Chen, 2014/9/22 WC2 Woei-Kae Chen, 2014/9/22

21 How does compiler handle virtual functions? virtual functions Overheads? memory speed How it works? dynamic binding vtable Abstract class Shape Point Circle Cylinder 35 vtable virtual functions functions 每個 class 都有一個 vtable 每個 object 都帶著 vtable 的 pointer array Memory overhead? Speed overhead? bp->psn(); 36 18

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape.

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. 1 Problem: Given a vector of pointers to Shape objects, sort the shape objects by areas in decreasing order. Std++sort:

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy CISC181 Introduction to Computer Science Dr. McCoy Lecture 27 December 8, 2009 Object Oriented Programming Classes categorize entities that occur in applications. Class teacher captures commonalities of

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. All Rights Reserved. 1 Inheritance is a form of software reuse in which you create a class that absorbs an existing class s data and behaviors

More information

JAVA Programming Language Homework V: Overall Review

JAVA Programming Language Homework V: Overall Review JAVA Programming Language Homework V: Overall Review ID: Name: 1. Given the following Java code: [5 points] 1. public class SimpleCalc { 2. public int value; 3. public void calculate(){ value = value +

More information

Chapter 9 - Object-Oriented Programming: Inheritance

Chapter 9 - Object-Oriented Programming: Inheritance Chapter 9 - Object-Oriented Programming: Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship between Superclasses and Subclasses 9.5 Case Study: Three-Level

More information

Inheritance Introduction. 9.1 Introduction 361

Inheritance Introduction. 9.1 Introduction 361 www.thestudycampus.com Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship Between Superclasses and Subclasses 9.4.1 Creating and Using a CommissionEmployee

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 10: I n h e r i t a n c e Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued COMP322 - Introduction to C++ Lecture 09 - Inheritance continued Dan Pomerantz School of Computer Science 11 March 2012 Recall from last time Inheritance describes the creation of derived classes from

More information

Java 程式設計入門. 講師 : 陳昭源 CSIE, NTU September 28, 2005

Java 程式設計入門. 講師 : 陳昭源 CSIE, NTU September 28, 2005 Java 程式設計入門 講師 : 陳昭源 CSIE, NTU September 28, 2005 Outline Floating Point & Decimal Numbers Inheritance Polymorphism Design Principles Abstract Exercise Interface September 28, 2005 Page 2 Floating Point

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

Polymorphism. Arizona State University 1

Polymorphism. Arizona State University 1 Polymorphism CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 15 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

C++ Inheritance and Encapsulation

C++ Inheritance and Encapsulation C++ Inheritance and Encapsulation Private and Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance 1 Private Members Private members

More information

Lecture 5: Inheritance

Lecture 5: Inheritance McGill University Computer Science Department COMP 322 : Introduction to C++ Winter 2009 Lecture 5: Inheritance Sami Zhioua March 11 th, 2009 1 Inheritance Inheritance is a form of software reusability

More information

CHAPTER 6 Class-Advanced Concepts - Inheritance

CHAPTER 6 Class-Advanced Concepts - Inheritance CHAPTER 6 Class-Advanced Concepts - Inheritance Page 1 Introduction: The idea of deriving a new class from the existing class. Provides the idea of code reusability. Existing class is called as base class

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Chapter 19 - C++ Inheritance

Chapter 19 - C++ Inheritance Chapter 19 - C++ Inheritance 19.1 Introduction 19.2 Inheritance: Base Classes and Derived Classes 19.3 Protected Members 19.4 Casting Base-Class Pointers to Derived-Class Pointers 19.5 Using Member Functions

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

Java 程式設計基礎班 (7) 莊坤達台大電信所網路資料庫研究室. Java I/O. Class 7 1. Class 7 2

Java 程式設計基礎班 (7) 莊坤達台大電信所網路資料庫研究室. Java I/O.   Class 7 1. Class 7 2 Java 程式設計基礎班 (7) 莊坤達台大電信所網路資料庫研究室 Email: doug@arbor.ee.ntu.edu.tw Class 7 1 回顧 Java I/O Class 7 2 Java Data Structure 動態資料結構 Grow and shrink at execution time Several types Linked lists Stacks Queues Binary

More information

Programming Languages: OO Paradigm, Polymorhism and Class Members

Programming Languages: OO Paradigm, Polymorhism and Class Members Programming Languages: OO Paradigm, Polymorhism and Class Members Onur Tolga Şehitoğlu Computer Engineering,METU 1 May 2009 Outline 1 2 Abstract Classes Inheritance inclusion polymorphism Binding is still

More information

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck C++ Crash Kurs Polymorphism Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck http://www.itm.uni-luebeck.de/people/pfisterer C++ Polymorphism Major abstractions of C++ Data abstraction

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig Fig. 9.11

Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig Fig. 9.11 CHAPTER 9 INHERITANCE 1 Illustrations List (Main Page) Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig. 9.10 Fig. 9.11 Some simple inheritance examples. An inheritance

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

Chapter 20 - C++ Virtual Functions and Polymorphism

Chapter 20 - C++ Virtual Functions and Polymorphism Chapter 20 - C++ Virtual Functions and Polymorphism Outline 20.1 Introduction 20.2 Type Fields and switch Statements 20.3 Virtual Functions 20.4 Abstract Base Classes and Concrete Classes 20.5 Polymorphism

More information

15: Polymorphism & Virtual Functions

15: Polymorphism & Virtual Functions 15: Polymorphism & Virtual Functions 김동원 2003.02.19 Overview virtual function & constructors Destructors and virtual destructors Operator overloading Downcasting Thinking in C++ Page 1 virtual functions

More information

Chapter 19 C++ Inheritance

Chapter 19 C++ Inheritance Chapter 19 C++ Inheritance Angela Chih-Wei i Tang Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 19.11 Introduction ti 19.2 Inheritance: Base Classes

More information

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

Object Oriented Programming

Object Oriented Programming OOP Object Oriented Programming Object 2 Object 1 Object 3 For : COP 3330. Object oriented Programming (Using C++) Object 4 http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Objects: State (fields),

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 08 - Inheritance continued School of Computer Science McGill University March 8, 2011 Last Time Single Inheritance Polymorphism: Static Binding vs Dynamic

More information

Upcasting. Taking an object reference and treating it as a reference to its base type is called upcasting.

Upcasting. Taking an object reference and treating it as a reference to its base type is called upcasting. 7. Polymorphism 1 Upcasting Taking an object reference and treating it as a reference to its base type is called upcasting. class Instrument { public void play () { public class Wind extends Instrument

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Inheritance A form of software reuse in which a new class is created by absorbing an existing class s members and enriching them with

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August Polymorphism, Dynamic Binding and Interface 2 4 pm Thursday 7/31/2008 @JD2211 1 Announcement Next week is off The class will continue on Tuesday, 12 th August 2 Agenda Review Inheritance Abstract Array

More information

C++ Programming: Polymorphism

C++ Programming: Polymorphism C++ Programming: Polymorphism 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Run-time binding in C++ Abstract base classes Run-time type identification 2 Function

More information

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University Polymorphism Miri Ben-Nissan (Kopel) 1 Shape Triangle Rectangle Circle int main( ) Shape* p = GetShape( ); p->draw( ); Shape* GetShape( ) choose randomly which shape to send back For example: Shape* p

More information

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN CSSE 220 Day 15 Inheritance Check out DiscountSubclasses from SVN Discount Subclasses Work in pairs First look at my solution and understand how it works Then draw a UML diagram of it DiscountSubclasses

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(b): Abstract classes & Polymorphism Lecture Contents 2 Abstract base classes Concrete classes Polymorphic processing Dr. Amal Khalifa,

More information

CST242 Object-Oriented Programming Page 1

CST242 Object-Oriented Programming Page 1 CST4 Object-Oriented Programming Page 4 5 6 67 67 7 8 89 89 9 0 0 0 Objected-Oriented Programming: Objected-Oriented CST4 Programming: CST4 ) Programmers should create systems that ) are easily extensible

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. All Rights Reserved. 1 Standard Behavior: When the compiler is working out which member function to call, it selects according to the type of

More information

Oxford isolution. 下載及安裝指南 Download and Installation Guide

Oxford isolution. 下載及安裝指南 Download and Installation Guide Oxford isolution 下載及安裝指南 Download and Installation Guide 系統要求 個人電腦 Microsoft Windows 10 (Mobile 除外 ) Microsoft Windows 8 (RT 除外 ) 或 Microsoft Windows 7 (SP1 或更新版本 ) ( 網上下載 : http://eresources.oupchina.com.hk/oxfordisolution/download/index.html)

More information

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Lecture 36: Cloning Last time: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Today: 1. Project #7 assigned 2. equals reconsidered 3. Copying and cloning 4. Composition 11/27/2006

More information

25. Generic Programming

25. Generic Programming 25. Generic Programming Java Fall 2009 Instructor: Dr. Masoud Yaghini Generic Programming Outline Polymorphism and Generic Programming Casting Objects and the instanceof Operator The protected Data and

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Inheritance CSC9Y4. Inheritance

Inheritance CSC9Y4. Inheritance Inheritance CSC9Y4 1 Inheritance We start with a superclass from which a subclass can be derived. New attributes and methods can be defined in a subclass and existing methods re-defined. How do we know

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab

More information

虛擬機 - 惡意程式攻防的新戰場. 講師簡介王大寶, 小時候大家叫他王小寶, 長大後就稱王大寶, 目前隸屬一神祕單位. 雖然佯稱興趣在看書與聽音樂, 但是其實晚上都在打 Game. 長期於系統最底層打滾, 熟悉 ASM,C/C++,

虛擬機 - 惡意程式攻防的新戰場. 講師簡介王大寶, 小時候大家叫他王小寶, 長大後就稱王大寶, 目前隸屬一神祕單位. 雖然佯稱興趣在看書與聽音樂, 但是其實晚上都在打 Game. 長期於系統最底層打滾, 熟悉 ASM,C/C++, 王大寶, PK 虛擬機 - 惡意程式攻防的新戰場 講師簡介王大寶, 小時候大家叫他王小寶, 長大後就稱王大寶, 目前隸屬一神祕單位. 雖然佯稱興趣在看書與聽音樂, 但是其實晚上都在打 Game. 長期於系統最底層打滾, 熟悉 ASM,C/C++, 對於資安毫無任何興趣, 也無經驗, 純粹是被某壞人騙上台, 可以說是不可多得的素人講師!! 議程大綱 : 現今的 CPU 都支援虛擬化專用指令集, 讓 VM

More information

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright Polymorphism Object Orientated Programming in Java Benjamin Kenwright Quizzes/Labs Every single person should have done Quiz 00 Introduction Quiz 01 - Java Basics Every single person should have at least

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

Chapter 4 (Part IV) The Processor: Datapath and Control (Parallelism and ILP)

Chapter 4 (Part IV) The Processor: Datapath and Control (Parallelism and ILP) Chapter 4 (Part IV) The Processor: Datapath and Control (Parallelism and ILP) 陳瑞奇 (J.C. Chen) 亞洲大學資訊工程學系 Adapted from class notes by Prof. M.J. Irwin, PSU and Prof. D. Patterson, UCB 4.10 Instruction-Level

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship The "is-a" Relationship Object classification

More information

Inheritance and object compatibility

Inheritance and object compatibility Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can

More information

Cpt S 122 Data Structures. Inheritance

Cpt S 122 Data Structures. Inheritance Cpt S 122 Data Structures Inheritance Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Base Classes & Derived Classes Relationship between

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship Object classification is typically hierarchical.

More information

Inheritance and aggregation

Inheritance and aggregation Advanced Object Oriented Programming Inheritance and aggregation Seokhee Jeon Department of Computer Engineering Kyung Hee University jeon@khu.ac.kr 1 1 Inheritance? Extend a class to create a new class

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 4: OO Principles - Polymorphism http://courses.cs.cornell.edu/cs2110/2018su Lecture 3 Recap 2 Good design principles.

More information

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers Resource Management Resource Management With a Unique Pointer Dynamic memory is an example of a resource that is allocated and must be released. void f() { Point* p = new Point(10, 20); // use p delete

More information

Midterm Exam 5 April 20, 2015

Midterm Exam 5 April 20, 2015 Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d.

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone else has built and debugged. In composition

More information

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University (12-1) OOP: Polymorphism in C++ D & D Chapter 12 Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University Key Concepts Polymorphism virtual functions Virtual function tables

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Subtyping (Dynamic Polymorphism)

Subtyping (Dynamic Polymorphism) Fall 2018 Subtyping (Dynamic Polymorphism) Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References PFPL - Chapter 24 Structural Subtyping - Chapter 27 Inheritance TAPL (pdf) - Chapter

More information

Inheritance and Overloading. Week 11

Inheritance and Overloading. Week 11 Inheritance and Overloading Week 11 1 Inheritance Objects are often defined in terms of hierarchical classes with a base class and one or more levels of classes that inherit from the classes that are above

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 4&5: Inheritance Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword @override keyword

More information

Citrix CloudGateway. aggregate control. all apps and data to any device, anywhere

Citrix CloudGateway. aggregate control. all apps and data to any device, anywhere Citrix CloudGateway aggregate control all apps and data to any device, anywhere Agenda 1. What s Cloud Gateway? 2. Cloud Gateway Overview 3. How it works? What s Cloud Gateway? It s all about the apps

More information

桌上電腦及筆記本電腦安裝 Acrobat Reader 應用程式

桌上電腦及筆記本電腦安裝 Acrobat Reader 應用程式 On a desktop or notebook computer Installing Acrobat Reader to read the course materials The Course Guide, study units and other course materials are provided in PDF format, but to read them you need a

More information

Inheritance, Polymorphism, and Interfaces

Inheritance, Polymorphism, and Interfaces Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)

More information

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08 Francesco Nidito Programmazione Avanzata AA 2007/08 Outline 1 2 3 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 10 , type systems and type checking A type type is an abstraction

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08 Francesco Nidito Programmazione Avanzata AA 2007/08 Outline 1 2 3 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 10 , type systems and type checking A type type is an abstraction

More information

Polymorphism and Interfaces. CGS 3416 Spring 2018

Polymorphism and Interfaces. CGS 3416 Spring 2018 Polymorphism and Interfaces CGS 3416 Spring 2018 Polymorphism and Dynamic Binding If a piece of code is designed to work with an object of type X, it will also work with an object of a class type that

More information

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Operator Overloading, Inheritance Lecture 6 February 10, 2005 Fundamentals of Operator Overloading 2 Use operators with objects

More information

BBM 102 Introduction to Programming II Spring Inheritance

BBM 102 Introduction to Programming II Spring Inheritance BBM 102 Introduction to Programming II Spring 2018 Inheritance 1 Today Inheritance Notion of subclasses and superclasses protected members UML Class Diagrams for inheritance 2 Inheritance A form of software

More information

Chapter 2 (Part 2) Instructions: Language of the Computer

Chapter 2 (Part 2) Instructions: Language of the Computer Chapter 2 (Part 2) Instructions: Language of the Computer 陳瑞奇 (J.C. Chen) 亞洲大學資訊工程學系 Adapted from class notes by Prof. C.T. King, NTHU, Prof. M.J. Irwin, PSU and Prof. D. Patterson, UCB 1 2.6 Logical Operations

More information

8. Polymorphism and Inheritance

8. Polymorphism and Inheritance 8. Polymorphism and Inheritance Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe polymorphism and inheritance in general Define interfaces

More information

臺北巿立大學 104 學年度研究所碩士班入學考試試題

臺北巿立大學 104 學年度研究所碩士班入學考試試題 臺北巿立大學 104 學年度研究所碩士班入學考試試題 班別 : 資訊科學系碩士班 ( 資訊科學組 ) 科目 : 計算機概論 ( 含程式設計 ) 考試時間 :90 分鐘 08:30-10:00 總分 :100 分 注意 : 不必抄題, 作答時請將試題題號及答案依照順序寫在答卷上 ; 限用藍色或黑色筆作答, 使用其他顏色或鉛筆作答者, 所考科目以零分計算 ( 於本試題紙上作答者, 不予計分 ) 一 單選題

More information

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with

More information

2009 OB Workshop: Structural Equation Modeling. Changya Hu, Ph.D. NCCU 2009/07/ /07/03

2009 OB Workshop: Structural Equation Modeling. Changya Hu, Ph.D. NCCU 2009/07/ /07/03 Amos Introduction 2009 OB Workshop: Structural Equation Modeling Changya Hu, Ph.D. NCCU 2009/07/02- 2 Contents Amos Basic Functions Observed Variable Path Analysis Confirmatory Factor Analysis Full Model

More information

Chapter 15. Polymorphism and Virtual Functions. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 15. Polymorphism and Virtual Functions. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 15 Polymorphism and Virtual Functions Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function Basics Late binding Implementing virtual functions When to

More information

COEN244: Polymorphism

COEN244: Polymorphism COEN244: Polymorphism Aishy Amer Electrical & Computer Engineering Polymorphism means variable behavior / ability to appear in many forms Outline Casting between objects Using pointers to access objects

More information

Object-Oriented Programming

Object-Oriented Programming iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 40 Overview 1 2 3 4 5 2 / 40 Primary OOP features ion: separating an object s specification from its implementation. Encapsulation: grouping related

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Figure 1 Microsoft Visio

Figure 1 Microsoft Visio Pattern-Oriented Software Design (Fall 2013) Homework #1 (Due: 09/25/2013) 1. Introduction Entity relation (ER) diagrams are graphical representations of data models of relation databases. In the Unified

More information

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS JAN 28,2011 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 FINALTERM EXAMINATION 14 Feb, 2011 CS304- Object Oriented

More information

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int) Inheritance Object OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int) PrintStream write(int) write(byte[]) Early binding Consider

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information