CSC151.02 2003F, Class 51: Objects Admin: * Mr. Park's editorial. * Questions on the exam? * The danger to late arrivals of early arrivals. Overview: * Strengths and weaknesses of records. * An alternative: Objects. * Key aspects of object-oriented programming: Encapsulation, Polymorphism, and Inheritance * Implementing encapsulation in Scheme. (I.e., "See the reading") * Lab. Question and answers * How do I deal with symbols or strings? (if (symbol? tag) (symbol->string tag) tag) * How do you write a procedure that takes one or two parameters. (define one-or-two (lambda (first . rest) (cond ((null? rest) (display first) (newline)) ((null? (cdr rest)) (display (car rest)) (newline)) (else (error 'one-or-two "You idiot. You should only call this procedure with one or two parameters. You used more. Are you sure that you understand Scheme? Take German instead."))))) * Boy, it seems like you're giving away answers left and right. Will you give away more? * Try and see. Tell me about records * Records are data structures * Data structures provide a way to structure data * "Structure" = "Organize or give form to" * "Data" = "Information" * Also a way to store information. * We've certainly seen other ways to store information in Scheme: Lists, Vectors, Association Lists, Files, ... * What sets records apart? * You need to write your own procedures to access the data in records. * Records seem to be harder to write than vectors. * Records might be defined in terms of the other structures (particularly vectors). * Records are intended to be somewhat "encapsulated"; It is hard for someone other than the designer to make new records of a particular type or to do "inappropriate" things to the contents of the record. * Each kind of record is designed to store a particular kind of information (e.g., sales for a shirt shop, information on a library book, etc.). Vectors and Lists are designed to be more general. * When you use a record, you don't need to worry about the internal structuring (because someone has written all the helpful procedures) * Why would you ever want to use records instead of vectors or lists? * Clearer code and less worry on the part of the user. * Encapsulation is also important. * How do you build records in Scheme? * Create a record constructor (e.g., new-compound) * Represent most records as vectors. * Write a lot of xxx-get and xxx-set! procedures for that new record (which look a lot like calls to vector-ref and vector-set! with some accompanying "safety" code). * What's wrong with these kinds of records, given the goals above? * You can still use vector-set! to change parts that are not mutable. Some smart Norwegian computer scientists (Nygaard and ...) in the 1960's decided to design a new way of structuring data to handle this problem. They decide something called an object and started a "revolution" called object-oriented programming. * Like records, objects collect information for a specific purpose. * Unlike records, objects also group "capabilities" with that information. * Objects generally provide better encapsulation than records. * "Real" objects provide polymorphism and inheritance. * "Polymorphism" = "Many forms" ; Idea that you can write one procedure that can work on many similar objects. "If you know how to add things, you can sum lists of those things." * "Inheritance" = "To give or pass down"; Idea that you can design new objects in terms of old. "Every library book is a book; Library books add the following characteristics." * We will focus on encapsulation. How to encapsulate a vector in Scheme Rather than applying procedures to the vector, we make the vector respond to "instructions". * We put the lambda after the let. ; 0: mood (0: particuarly grump to 10: incredibly happy) ; 1: weight (a number) ; 2: eye-color (a string) ; 3: cash on hand (an integer, represents money in cents) (define sams-characteristics (let ((characteristics (vector 5 10 "brown" 2000))) (lambda (command) (cond ((eq? ':describe) (display "Everything you needed to know about Sam Rebelsky but didn't want to ask.") (newline) (display "Sam's mood is ranked at: ") (display (vector-ref characteristics 0)) (newline) (display "Sam's weight is ") (display "none of your damn business.") (newline) (display "Sam has $") (display (/ (vector-ref characteristics 3) 100)) (display " on hand."))))))