DOC PREVIEW
MIT 6 001 - Message Passing Objects

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

6.001, Fall 2007—Recitation 17 — 11/1/2007 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001—Structure and Interpretation of Computer ProgramsFall 2007Recitation 17 — 11/1/2007Message Passing ObjectsEarlier we’ve seen methods for abstracting data objects and also for procedures. Today we’llcombine the two, using an object system quite similar to the objects used in the (forthcoming)project 4, also known as the adventure game. Objects combine data and procedural abstractions.What does that mean? Object-oriented programming allows us to combine the procedures withthe data it manipulates. In effect, this is one way to enforce that the abstractions are followed.Here’s a start of defining a set of objects, in normal Scheme:(define (make-named name)(lambda (msg)(cond((eq? msg ’NAME) (lambda () name))(else (error "no handler for " msg)))))(define (make-animal name)(let ((named-part (make-named name)))(lambda (msg)(cond((eq? msg ’DRINK) (lambda () (display "slurp")))((eq? msg ’EAT) (lambda (object)(display "crunch crunch ")(display ((object ’NAME)))(display " was yummy")))(else (named-part msg))))))(define fluffy (make-animal ’fluffy))(define lunch (make-named ’pizza))In objects terminology, we’d say that there are two classes, named, and animal. We also have twoobjects, fluffy, which is an instance of an animal, whereas lunch is an instance of the namedclass.Problem: How would you ask fluffy to return his name?Problem: How would you ask fluffy to eat lunch?6.001, Fall 2007—Recitation 17 — 11/1/2007 2As we can quickly see, defining objects this way will work, but the interface is rather clunky.Instead, we’ll build on a set of abstractions for creating and manipulating objects. Here’s what thesame class definition would look in the project 4 system. We won’t talk at all today about how thevarious provided procedures are defined.(define (create-animal name)(create-instance animal name))(define (animal self name)(let ((named-part (named-object self name)))(make-handler’ANIMAL(make-methods’DRINK (lambda () (display-message (list "slurp")))’EAT (lambda (x) (display-message(list "crunch crunch "(ask x ’NAME)" was yummy"))))named-part)))(define fluffy (create-animal ’fluffy))(define lunch (create-named-object ’pizza))Problem: How would you ask fluffy his name and to eat lunch now?Note that that the constructor for every class we will define takes as it’s first argument an objectcalled self. Draw a diagram of the methods that fluffy can respond to, and try to come up withexamples of when the self pointer would be useful.6.001, Fall 2007—Recitation 17 — 11/1/2007 3Now here’s a cat class that derives from the animal class, and specific instance of a cat, garfield.(define (create-cat name)(create-instance cat name))(define (cat self name favorite-food)(let ((animal-part (animal self name))(mood 0))(make-handler’CAT(make-methods’FETCH(lambda (x)(display-message (list "Go fetch" (ask x ’NAME) "yourself"))(set! mood (- mood 3)))’MOOD(lambda () (if (>= mood 4) ’content ’angry))’INSTALL(lambda ()(ask animal-part ’INSTALL)(display-message (list "I am" (ask self ’NAME) "yaawn"))(ask our-clock’ADD-CALLBACK(create-clock-callback ’EAT-CB self ’EAT)))’EAT(lambda ()(ask animal-part ’EAT favorite-food)(set! mood (+ mood 1))))animal-part)))(define lasagna (create-named-object ’lasagna))(define garfield (create-cat ’garfield lasagna))Note that there are references to additional classes that aren’t provided here (namely, clocks andclock callbacks).1. What will the following evaluate to:(ask garfield ’MOOD)(ask our-clock ’TICK)(ask our-clock ’TICK)(ask garfield ’MOOD)(ask our-clock ’TICK)(ask our-clock ’TICK)(ask garfield ’MOOD)6.001, Fall 2007—Recitation 17 — 11/1/2007 42. Add a method to the cat class called ’GREET. If the cat is content, the cat should (display)“purr”, otherwise “hiss”.3. Write a class definition for a dog, and then create an instance of a dog named odie. Dogsshould respond to the same methods as cats, but with opposite effects – Odie should growmore unhappy as time progresses, rather than being happier the longer he’s left alone, andany attention (such as being asked to ’FETCH) should improve his


View Full Document

MIT 6 001 - Message Passing Objects

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Message Passing Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Message Passing Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Message Passing Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?