Docstoc

lecture21

Document Sample
lecture21 Powered By Docstoc
					                          Lecture 21:
                          Inheritance
CS200: Computer Science
University of Virginia                    David Evans
Computer Science           http://www.cs.virginia.edu/evans
                     Menu
•   Objects Review
•   Inheritance
•   PS5: Databases
•   PS6




17 March 2003    CS 200 Spring 2003   2
                Objects

• When we package state and
  procedures together we have
  an object
• Programming with objects is
  object-oriented programming

17 March 2003   CS 200 Spring 2003   3
                make-number
(define make-number
 (lambda (n)
   (lambda (message)
     (cond
      ((eq? message 'value)
       (lambda (self) n))
      ((eq? message 'add)       Why don’t we just use n?

       (lambda (self other)
                                (Well see why later today.)

         (+ (ask self 'value)
            (ask other 'value))))))))
17 March 2003         CS 200 Spring 2003                4
                                  ask
                Lecture 20:

                (define (ask object message)
                   (object message))


  (define (ask object message . args)
   (apply (object message) object args))



17 March 2003                 CS 200 Spring 2003   5
(define make-number                               global
 (lambda (n)                                      environment
   (lambda (message)
     (cond
      ((eq? message 'value)                           + : #<primitive:+>
       (lambda (self) n))
      ((eq? message 'add)                                             make-number:
           (lambda (self other)
             (+ (ask self 'value)                            san:
            (ask other 'value))))))))


 > (define san
      (make-number 3))                       n:   3                 parameters:
 > (ask san 'value)
                                                                    body: ((lambda …
 3
 > (ask san 'add
     (make-number 4))
 7                                                     parameters: message
                                                       body: (cond ((eq? …
    17 March 2003                       CS 200 Spring 2003                       6
    There are many kinds of numbers…

•   Whole Numbers (0, 1, 2, …)
•   Integers (-23, 73, 0, …)
•   Fractions (1/2, 7/8, …)
•   Floating Point (2.3, 0.0004, 3.14159)

• But they can’t all do the same things
    – We can get the denominator of a fraction, but
      not of an integer
17 March 2003        CS 200 Spring 2003               7
                  make-fraction
(define make-fraction
 (lambda (numerator denominator)
   (lambda (message)
     (cond
      ((eq? message 'value)
       (lambda (self) (/ numerator denominator))
      ((eq? message 'add)                               Same as in
          (lambda (self other)                          make-number
            (+ (ask self 'value) (ask other 'value)))
         ((eq? message ‘get-numerator)
          (lambda (self) numerator))
         ((eq? message ‘get-denominator)
          (lambda (self) denominator))
         )))))
17 March 2003            CS 200 Spring 2003                    8
Why is redefining add a bad thing?
• Cut-and-paste is easy but…
• There could be lots of number methods
  (subtract, multiply, print, etc.)
• Making the code bigger makes it harder to
  understand
• If we fix a problem in the number add
  method, we have to remember to fix the
  copy in make-fraction also (and real,
  complex, float, etc.)
17 March 2003    CS 200 Spring 2003           9
                Inheritance




17 March 2003     CS 200 Spring 2003   10
                make-fraction
(define (make-fraction numer denom)
   (let ((super (make-number #f)))
     (lambda (message)
       (cond
        ((eq? message 'value)
          (lambda (self) (/ numer denom)))
        ((eq? message 'get-denominator)
          (lambda (self) denom))
        ((eq? message 'get-numerator)
          (lambda (self) numer))
        (else
          (super message))))))
17 March 2003       CS 200 Spring 2003       11
                Using Fractions

   > (define half (make-fraction 1 2))
   > (ask half 'value)
   1/2
   > (ask half 'get-denominator)
   2
   > (ask half 'add (make-number 1))
   3/2
   > (ask half 'add half)
   1

17 March 2003        CS 200 Spring 2003   12
> (trace ask)
> (trace eq?)
> (ask half 'add half)
|(ask #<procedure> add #<procedure>)
| (eq?   add value)
| #f                               | (ask #<procedure> value)
| (eq?   add get-denominator)      | |(eq? value value)
| #f                               | |#t
| (eq?   add get-numerator)        | 1/2
| #f                               | (ask #<procedure> value)
| (eq?   add value)                | |(eq? value value)
| #f                               | |#t
| (eq?   add add)                  | 1/2
| #t                               |1
                                   1
17 March 2003            CS 200 Spring 2003                 13
                                              make-number
> (trace ask)
> (trace eq?)
                                               make-fraction
> (ask half 'add half)
|(ask #<procedure> add #<procedure>)
| (eq?   add value)
| #f                               | (ask #<procedure> value)
| (eq?   add get-denominator)      | |(eq? value value)
| #f                               | |#t
| (eq?   add get-numerator)        | 1/2
| #f                               | (ask #<procedure> value)
| (eq?   add value)                | |(eq? value value)
| #f                               | |#t
| (eq?   add add)                  | 1/2
| #t                               |1
                                   1
17 March 2003            CS 200 Spring 2003                 14
                Inheritance
   Inheritance is using the definition of one
   class to make another class

   make-fraction uses make-number to
   inherit the behaviors of number




17 March 2003      CS 200 Spring 2003           15
                      • English
                         A Fraction is a kind of Number.
    Number            • C++
                         Fraction is a derived class whose base
                           class is Number
                      • Java
    Fraction             Fraction extends Number.
                      • Eiffel
                         Fraction inherits from Number.
Note: people          • Beta
sometimes draw           Fraction is a subpattern of Number.
this different ways
                      • Smalltalk (72)
                         Didn’t have inheritance!
17 March 2003              CS 200 Spring 2003                  16
                CS 200:

Number          Fraction inherits from Number.

                Fraction is a subclass of Number.
Fraction

                The superclass of Fraction is
                 Number.

17 March 2003          CS 200 Spring 2003        17
        Inheritance and Subtyping
• Inheritance: reusing the implementation of
  one object to make a new kind of object

• Often confused with subtyping which is
  saying one kind of object can be used
  where another kind of object is expected
    – CS200 won’t cover subtyping (take CS201J)
    – We will cover types (Class 29)


17 March 2003       CS 200 Spring 2003            18
                          PS5
   How are commercial databases different
   from what you implemented for PS5?
        UVa’s Integrated Systems Project to
        convert all University information
        systems to use an Oracle database was
        originally budgeted for $58.2 Million
        (starting in 1999). Actual cost is likely
        to be $100 Million.
                            http://www.virginia.edu/isp/
17 March 2003          CS 200 Spring 2003                  19
                   Real Databases
• Atomic Transactions: a transaction may involve many
   modifications to database tables, but the changes should only
   happen if the whole transaction happens (e.g., don’t charge the
   credit card unless the order is sent to the shipping dept)

• Security: limit read/write access to tables,
   entries and fields
• Storage: need to efficiently store data on disk,
   provide backup mechanisms
• Scale: to support really big data tables,
  real databases do lots of clever things
17 March 2003                 CS 200 Spring 2003                     20
     How big are big databases?
• Microsoft TerraServer
    – Claimed biggest in 1998
    – Aerial photos of entire US (1 meter resolution)




17 March 2003         CS 200 Spring 2003            21
                                         Rotunda




 You are here             Amphitheater




17 March 2003   CS 200 Spring 2003          22
                                    You are here




            AFC?


17 March 2003      CS 200 Spring 2003   Picture from 2 Apr 199423
                Big Databases
• Microsoft TerraServer
    – 3.3 Terabytes (claimed biggest in 1998)
    – 1 Terabyte = 240 Bytes ~ 1 Trillion Bytes
• Wal-Mart
    – 285 Terabytes
• Stanford Linear Accelerator (BaBar)
    – 500 Terabytes (30 KB per particle collision)


17 March 2003         CS 200 Spring 2003             24
                    PS6

         Make an adventure game
         programming with objects



17 March 2003    CS 200 Spring 2003   25
                                             object
PS6 Classes

                   physical-object                                 place
                                                           make-class is the
            mobile-object                                  procedure for
                                                           constructing
                                                           objects in the
 thing                                person               class class

student inherits from person
which inherits from mobile-object
which inherits from physical-object   student              police-officer
which inherits from object.


   17 March 2003                      CS 200 Spring 2003                   26
                              object
PS6 Objects

          physical-object                         place
                                       Cabal
        mobile-object                   Hall          Recursa

                                         (make-place name)
                                         evaluates to an object
thing                  person            that is an instance of
                                         the class place.



                         student       police-officer

           Alyssa P. Hacker
       Are there class hierarchies
       like this in the real world or
        just in fictional worlds like
             Charlottansville?


17 March 2003     CS 200 Spring 2003    28
   Microsoft Foundation Classes




   CButton inherits from CWnd inherits from CObject
   “A button is a kind of window is a kind of object”
17 March 2003        CS 200 Spring 2003            29
                                                                             RotationPathInterpolator
                                      PathInterpolator
                                  Interpolator
         Node
                            Selector

                 Leaf



   SceneGraphObject




                                                                          Not at all uncommon to have
                                                                          class hierarchies like this!

Java 3D Class Hierarchy Diagram
   17 March 2003                                                CS 200 Spring 2003                      30
http://java.sun.com/products/java-media/3D/collateral/j3dclass.html
                    Charge
• PS5
    – Return now, comments will be available
      Wednesday
• PS6
    – Programming with Objects
• Wednesday and Friday:
    – How will solving the (generalized) Cracker
      Barrel Peg Board puzzle help cure cancer?


17 March 2003        CS 200 Spring 2003            31

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/18/2012
language:
pages:31
fanzhongqing fanzhongqing http://
About