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