lecture15 - 4/5/2009 Notes COMPUTER SCIENCE 51 Spring 2009

Info iconThis 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: 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)))
Background image of page 1

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

View Full DocumentRight 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/26/2009 for the course COMPUTERSC CS51 taught by Professor Gregmorrisett during the Spring '09 term at Harvard.

Page1 / 5

lecture15 - 4/5/2009 Notes COMPUTER SCIENCE 51 Spring 2009

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online