{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture15 - Notes COMPUTER SCIENCE 51 Spring 2009...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
4/5/2009 1 COMPUTER SCIENCE 51 Spring 2009 cs51.seas.harvard.edu Prof. Greg Morrisett Prof. Ramin Zabih Notes Midterms: With Susan Welby in Maxwell Dworkin 239 Last Time: mutation & side effects see VII of HTDP, 4.9 & 5.8 of PLT Guide bottom line: difficult to reason about, minimize at all costs, but necessary in some cases for asymptotic space and/or time efficiency. Today key concepts of OO programming see 5.3 & 13 of PLT Guide OO “Object-Oriented” means a whole bunch of different things in different languages. All OO languages have these ideas: • an object is like a struct or record: – a named collection of values – including (possibly mutable) state, and functions – the functions are often called methods – like a first-class module want to encapsulate values within an object. – same idea as with modules: information hiding, minimal interface, easy to change internals, etc. – outside world doesn’t need to know the details Example Encoding ( define-struct stack (push pop)) ( define (new-stack) ( let * ([s empty] [push ( λ (v) ( set ! s (cons v s)))] [pop ( λ () (let ([v (car s)]) ( begin ( set ! s (cdr s)) v)))]) (make-stack push pop))) Additional OO Concept #1: Sub-typing: – an object that has a “bigger” interface can be used anywhere an object with a “smaller” interface is expected. – not really an OO concept, rather a math concept: • e.g., integers are a subtype of rationals but OO is pointless without subtyping – a form of polymorphism • a way for code to be re-used in multiple contexts e.g., matrix multiply algorithm is the same regardless as to whether the elments are integers or rationals. Example ( define-struct (sized-stack stack) (size)) ( define (new-sized-stack) ( let * ([s empty] [push ( λ (v) ( set ! s (cons v s)))] [pop ( λ () (let ([v (car s)]) ( begin ( set ! s (cdr s)) v)))] [size ( λ () (length s)]) (make-sized-stack push pop size)))
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4/5/2009 2 Subtyping on Structs ( define-struct (sized-stack stack) (size)) is almost the same as writing: ( define-struct sized-stack (push pop size)) However: – the accessors are still
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}