CSCI 3155: Principles of Programming Languages Exercise sheet #12 26 June 2007

Similar documents
Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Objects, Subclassing, Subtyping, and Inheritance

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Chapter 1: Object-Oriented Programming Using C++

Method Resolution Approaches. Dynamic Dispatch

Type Checking in COOL (II) Lecture 10

Inheritance Considered Harmful

PROGRAMMING LANGUAGE 2

COMP 110/L Lecture 19. Kyle Dewey

COMP322 - Introduction to C++

CS558 Programming Languages Winter 2013 Lecture 8

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Tutorial notes on. Object relational structural patterns

[ L5P1] Object-Oriented Programming: Advanced Concepts

Inheritance, and Polymorphism.

6.170 Recitation #5: Subtypes and Inheritance

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Imperative Programming 2: Inheritance 1

Inheritance, Polymorphism, and Interfaces

CS164: Midterm Exam 2

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

Java Object Oriented Design. CSC207 Fall 2014

Exam in TDDB84: Design Patterns,

Polymorphism CSCI 201 Principles of Software Development

Featherweight Java (FJ)

CSE 307: Principles of Programming Languages

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

CSE Lab 7 Assignment

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Inheritance and object compatibility

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Self-review Questions

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Midterm Exam 5 April 20, 2015

Creating Java Programs with Greenfoot

Object-Oriented Concepts

ECE 122. Engineering Problem Solving with Java

22. Subtyping, Inheritance and Polymorphism

CMSC 202. Generics II

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Compiler construction 2009

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Chapter 5 Object-Oriented Programming

Object Oriented Issues in VDM++

Type Hierarchy. Lecture 6: OOP, autumn 2003

Last name:... First name:... Department (if not D-INFK):...

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Methods Common to all Classes

CSE 341: Programming Languages. Section AC with Nate Yazdani

C++ Programming: Polymorphism

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Parameter Passing Styles

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Interfaces, Mixins, & Multiple Inheritance

CSE 303 Lecture 23. Inheritance in C++ slides created by Marty Stepp

2. The object-oriented paradigm!

SWEN221: Software Development. 3: Inheritance I. David J. Pearce & Marco Servetto Computer Science, Victoria University

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

CS Programming I: Inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CSCI 355 Lab #2 Spring 2007

CSci 1113 Midterm 1. Name: Student ID:

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

VIRTUAL FUNCTIONS Chapter 10

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object-Oriented Concepts and Design Principles

Practice for Chapter 11

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

301AA - Advanced Programming [AP-2017]

Student Performance Q&A:

CS 251 Practice Final Exam R. Brown May 1, 2015 SHOW YOUR WORK No work may mean no credit Point totals will be adjusted to a 120 point scale later

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

Object typing and subtypes

Data Structures (list, dictionary, tuples, sets, strings)

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

CS111: PROGRAMMING LANGUAGE II

Programming in C++: Assignment Week 6

Everything is an object. Almost, but all objects are of type Object!

C12a: The Object Superclass and Selected Methods

Use the scantron sheet to enter the answer to questions (pages 1-6)

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

COMP 250 Fall inheritance Nov. 17, 2017

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

Programming Languages and Techniques (CIS120)

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Object-Oriented Design II

Transcription:

CSCI 3155: Principles of Programming Languages Exercise sheet #12 26 June 2007 Group name: Object-Oriented Programming I Exercise 1. The two most central aspects of object-oriented programming are dynamic dispatch and inheritance. (a) (Skill 16.1) Explain, in your own words, what dynamic dispatch is. Give an example to illustrate your point; you may recycle an example from class (as long as your explanation matches the example). (b) (Skill 16.2) Explain, in your own words, what inheritance is. Give an example that illustrates why inheritance is useful and explain the example. 1

Exercise 2. Consider the following C++ program: class A { public : A() {} A() {} f () { cout << A. f << endl ; } g () { cout << A. g << endl ; f ( ) ; } class B : public A { public : f () { cout << B. f << endl ; } class C : public A { public : g () { cout << C. g << endl ; f ( ) ; } class D : public C { A a ; B b ; C c ; D d ; (a) (Skills 16.2, 16.5) The inheritance hierarchy of an object-oriented program is an explanation of all inheritance happening in the program. Such hierarchies are often given as diagrams, such as the following: Vegetable Carrot Broccoli 26 June 2007 2

Here, Vegetable is the superclass of both Carrot and Broccoli, and neither Carrot nor Broccoli is the subclass of the other. Give the inheritance hierarchy of the program above. (b) (Skill 16.3) Assume that we invoke a.f(). What will the program print? Explain. (c) (Skill 16.3) Assume that we invoke c.f(). What will the program print? Explain. (d) (Skill 16.3) Assume that we invoke b.g(). What will the program print? Explain. 26 June 2007 3

Exercise 3. Analogously to our notion of structural type equivalence, we can define a notion of structural subtyping. With structural subtyping, T <: S iff we can turn any value from T into a value of S simply by discarding information. Most object oriented C-like languages (such as C#, Java, and C++) use by-name subtyping. By contrast, the object-oriented imperative functional language OCaml uses structural subtyping. Consider the following OCaml program: class a = object ( s e l f ) ( This j u s t gives the name s e l f to the s e l f reference ( t h i s in C++/Java ) ) method f (( x : i nt ), ( y : int )) = x method comp ( t ) = ( t = s e l f ) class b = object ( s e l f ) inherit a ( I n h e r i t a l l d e f i n i t i o n s from c l a s s a ) method g ( x : i nt ) = x class c = object ( s e l f ) method f (( a : i nt ), (b : int )) = b method comp (a : a) = true class d = object ( s e l f ) inherit a ( I n h e r i t a l l d e f i n i t i o n s from c l a s s a ) method d only () = true ( override the d e f i n i t i o n of comp from a ) ( Here, o#m i s a method c a l l to method m of object o ) method comp ( t ) = t#d only () && s e l f#d only () (a) (Skills 16.4, 17.3) Is b a subtype and/or a subclass of a? Explain. (b) (Skills 16.4, 17.3) Is c a subtype and/or a subclass of a? Explain. (c) (Skills 16.4, 17.3) Is d a subtype and/or a subclass of a? Explain. 26 June 2007 4

Exercise 4. You are given the task of implementing a simulator for a computer network with ring topology. Your simulator should allow different kinds of Hosts, specifically Print Servers and Workstations, though we may want to add other hosts later. It should further support Packets containing the network information. (a) (Skill 16.1) Draw an inheritance hierarchy that summarises the above. (b) We need the following functionality: i) Each host should have a next host associated with it. This host should be set whenever we create the host, but we should be able to change that host. ii) Each packet should have a receiver host associated with it. iii) Each host should be able to accept a new packet. iv) Each workstation should be able to send a new packet to a print server. v) Whenever the packet reaches the right host, it should be processed in some fashion. If it doesn t reach the right host, it should be forwarded to the next host. vi) Each print server, when it receives a packet, should print out the packet. vii) Each non-print server, when it receives a packet, should give an error message. For each class, describe all of the methods it requires. Clearly indicate any overriding. 26 June 2007 5

26 June 2007 6