University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2004

Notes: Wednesday 17 March 2004
Schedule

Notes
(define (make-number n)
  (lambda (message)
    (cond
     ((eq? message 'value) (lambda (self) n))
     ((eq? message 'add) (lambda (self other)
			   (+ (ask self 'value) (ask other 'value))))))))

(define (ask object message . args) (apply (object message) object args))
Inheritance is using the definition of one class to make another class.
(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))))))
The class fraction inherits from number.
fraction is a subclass of number.
The superclass of fraction is number.

Which of the traced applications are from fraction and which are from number?
> (ask half 'add half)
|(ask # add #)
| (eq? add value)
| #f
| (eq? add get-denominator)
| #f
| (eq? add get-numerator)
| #f
| (eq? add value)
| #f
| (eq? add add)
| #t
| (ask # value)
| |(eq? value value)
| |#t
| 1/2
| (ask # value)
| |(eq? value value)
| |#t
| 1/2
|1
1



If I have seen further it is by standing on the shoulders of giants.
Isaac Newton (on inheritance?)

If I have not seen as far as others, it is because giants were standing on my shoulders.
Hal Abelson (on subtyping?)

cs200-staff@cs.virginia.edu
Using these Materials