Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 18: Topological sort Part 1:
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 4: Objects.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 12: Design by Contract™
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 18: Undo/Redo.
Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering ATOT - Lecture 11, 7 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 16: Topological sort Part 1:
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering ATOT - Lecture 5, 14 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 20: Multiple inheritance.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Chair of Software Engineering Complement to lecture 11 : Levenshtein.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and the dynamic model

2 Purpose of this lecture Few really new concepts, but gain a better understanding of the tricky matter of references Reminder on garbage collection and associated concepts

3 Assignment Replaces a value by another x y 2 0 p. set_coordinates (2, 1) 0 1 p

4 Setting fields (in routines of the class) class POSITION feature –– Access x: REAL -- Horizontal position y: REAL -- Vertical position feature –– Element change set_coordinates (xval, yval: REAL) -- Set coordinates to (`xval', `yval'). require x_positive: xval >= 0 y_positive: yval >= 0 do ensure x_set: x = xval y_set: y = yval end x := xval y := yval

5 class LINKED_CELL feature item : INTEGER right : LINKED_CELL set_fields (n : INTEGER ; r : LINKED_CELL) -- Reset both fields. do item := n right := r end Effect of an assignment Reference types: reference assignment Expanded types: value copy item right i tem := n right := r t, u : LINKED_CELL create t ; create u t. set_fields (25, u) 3 See LINKABLE in EiffelBase t 0 u  t. set_fields (25, u) 25 0

6 A linked list of strings Haldenegg item right Central item right Haupt- bahnhof item right (LINKABLE) first_element last_element active count 3 Parade- platz item right : inserting at the end (LINKABLE) 4

7 Inserting an item at the end extend (v : STRING) -- Add v to end. -- Do not move cursor. local p : LINKABLE [STRING] do create p. make (v) if is_empty then first_element := p active := p else last_element. put_right ( p) if after then active := p end end last_element := p count := count + 1 end

8 Exercise (uses loops) Reverse a list! Halden- egg item right Central Haupt- bahnhof last_element first_element count 3 (LINKABLE) (LINKED_LIST) Parade- platz

9 Reversing a list

10 Reversing a list from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot. right first_element. put_right (i ) end first_element pivot right i 12345

11 Reversing a list from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot. right first_element. put_right (i ) end first_element pivot right i 12345

12 Reversing a list from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot. right first_element. put_right (i ) end first_element pivot right i 12345

13 Reversing a list from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot. right first_element. put_right (i ) end first_element pivot right i 12345

14 i i pivot Reversing a list from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot. right first_element. put_right (i ) end first_element right 12345

15 The loop invariant from pivot := first_element first_element := Void until pivot = Void loop i := first_element first_element := pivot pivot := pivot. right first_element. put_right (i ) end first_element pivot right i Invariant: from first_element following right, initial items in inverse order; from pivot, rest of items in original order

16 The trouble with reference assignment A comfortable mode of reasoning: -- Here SOME_PROPERTY holds of a “Apply SOME_OPERATION to b” -- Here SOME_PROPERTY still holds of a This applies to “expanded” values, e.g. integers -- Here P (a) holds OP (b) -- Here P (a) still holds of a

17 Dynamic aliasing a, b: LINKABLE [STRING] create a.... a. put ("Haldenegg") ‏ b := a -- Here a. item has value "Haldenegg" b. put ("Paradeplatz") ‏ -- Here a. item has value ????? Haldenegg item right a b

18 On the other hand I heard that the boss’s cousin earns less -- than 50,000 francs a year “Raise Caroline’s salary by 1 franc” -- ????? Metaphors:  “The beautiful daughter of Leda”  “Menelas’s spouse”  “Paris’s lover” = Helen of Troy

19 Practical advice Reference assignment is useful It’s also potentially tricky As much as possible, leave it to specialized libraries of general data structures

20 Variants of assignment and copy Reference assignment (a and b of reference types): b := a Object duplication (shallow): c := a. twin Object duplication (deep): d := a. deep_twin Also: shallow field-by-field copy (no new object is created): e. copy (a) ‏

21 Shallow and deep cloning Initial situation: Result of: b := a c := a. twin d := a. deep_twin “Almaviva” name landlord loved_one O1 “Figaro” O2 “Susanna” O3 b “Almaviva” O4 c “Almaviva” O5 “Figaro” O6 “Susanna” O7 d a

22 Where do these mechanisms come from? Class ANY in the Eiffel “Kernel Library” Every class that doesn’t explicitly inherit from another is considered to inherit from ANY As a result, every class is a descendant of ANY.

23 Completing the inheritance structure A BDEC ANY NONE Inherits from

24 A related mechanism: Persistence a. store (file) ‏.... b := retrieved (file) Storage is automatic. Persistent objects identified individually by keys. These features come from the library class STORABLE. Needs to be improved, see “ “object test”

25 Objects and references States of a reference: Operations on references: create p p := q p := Void if p = Void then... VOIDATTACHED create p p := q (where q is attached) ‏ p := Void p := q (where q is void) ‏ p ATTACHED p VOID

26 The object-oriented form of call some_target. some_feature (some_arguments) ‏ For example: Paris. display Line6. extend (Station_Parade_Platz) ‏ x := a. plus (b) ???????

27 Infix and prefix operators In a − b the − operator is “infix” (written between operands) ‏ In − b the − operator is “prefix” (written before the operand) ‏

28 Operator features expanded class INTEGER feature plus alias "+" (other : INTEGER): INTEGER -- Sum with other do... end times alias “*“ (other : INTEGER): INTEGER -- Product by other do... end minus alias “-" : INTEGER -- Unary minus do... end... end Calls such as i.plus (j) can now be written i + j

29 Reading assignment for next week Chapters on  Syntax (11)  Inheritance (16)

30 What we have seen  Playing with references: list reversal  Dynamic aliasing and the difficulties of pointers & references  Overall inheritance structure  Copy, clone and storage operations  Persistence closure  Infix & prefix operators