6.001 Recitation 16Structure and Interpretation of Computer Programs April 6, 2005Object Oriented Programming1. Object oriented programming• Dispatch on operation• State is captured in objects or, more s pecifically, instances• Classes provide templates for the structure of instances (what internal state variablesthey have and what messages they understand), but no actual values for the internalstate. Each instance has an independent copy of the values of its state variables.• Communication with objects is done by passing them messages (equivalent to methodsin most OOP languages)2. OOP code templateAs our object system currently stands, here’s w hat the code f or a class looks like:; ; C o n s t r u c t o r f o r o b j e c t s o f t y p e x( d e f i n e (make−x co n s t r u c t o r − arg1 arg2 . . . ); ; Handler f o r message a( d e f i n e ( message− handler− a a rg1 arg 2 . . . ); ; Do wh a te v er nee ds t o be done t o h a n d l e t h i s message ,; ; i n c l u d i n g s e t ! ’ in g i n t e r n a l s t a t e i f n e c e s s a r y); ; H a n d l er f o r message b( d e f i n e ( message− handler− b arg 1 arg2 . . . ); ; . . .); ; T his p r o c e d u r e i s what a c t u a l l y r e p r e s e n t s an i n s t a n c e . I t; ; d i s p a t c h e s t o t h e c o r r e c t message han dl er , p a s s i n g a r g s(lambda (msg . a r gs )(cond ( ( eq? msg ’ a ) ( apply message− handler− a a rgs ))( ( eq? msg ’ b ) ( apply message− handler− b a r gs ) ); ; . . .( e l s e ( e r r o r ”No method” msg ) ) ) ) ); ; I n s t a n t i a t e an o b j e c t o f t y p e x( d e f i n e x− in st an ce ( make−x ar g1 arg2 . . . ) ); ; Pass message a t o o b j e c t x− i n s ta n ce( x− in st an ce ’ a arg 1 arg2 )In the next few lectures, we’ll extend this to make it much more powerful and introduce someabstractions to make our lives easier.Simple variations on this include wrapping the body of make−x in a let that introduces moreinternal state, and putting the body of simple message handlers (such as accessors) directlyinto the dispatch procedure.16.001 Structure and Interpretation of Computer Programs Object Oriented Programming3. Environment model practiceWhat do the last three statements evaluate to?( d e f i n e ( magic x )( l e t ( ( fu nc ( lambda ( ) x ) ) )(lambda ( )( l e t ( ( cur− func func )( v a l u e ( fu n c ) ) )( s e t ! fun c (lambda ( ) (+ x ( cur− func ) ) ) )v al u e ) ) ) )( d e f i n e f oo ( magic 1 ) )( d e f i n e bar ( magic 2 ) )( fo o )( fo o )( bar )Draw the environment diagram after the evaluation of the above statements.4. OOPWrite an object-oriented counter with operations: increment, get and reset5. BrainteaserMost mutators return unspecified. This is because they are side-effect only, so there’s noreason to return any value (any reasonable value they could retur n would just be for conve-nience). This is not true of append!. append! does return a value (specifically, it returns thewhole, appended list). There’s a very good reason it does this, and without understandingthe reason for this, programs that use append! will probably misbehave.Why does append! not return
View Full Document