CSC151, Class 53: Objects Overview: * Problems with records. * The notion of object. * Objects in Scheme. * Some details. * Lab Notes: * Read my essay on end-of-course evaluations (soon). * Start working on the end-of-course evaluation for this course. Plan to turn it in on Wednesday. * Sorry that so few of you took the exam. * Questions on the take-home? Now due start of class on Wednesday * Email me extra credit. Question: What is a record? One element in a database or other collection of data. A record is a collection of information Often different types of information With intended meanings to each piece of information WIth a uniform format across similar collections A record for a book might include * Title: String * Authors: List of names * ISBN Number: String * Genres: List of strings * LOC number: String * Publication date: Number * Price: Number + Units A record for a name might include * Last name, string * First name: String * Middle name: String * Suffix: String * Title: String How might we store records in Scheme? In vectors. Since someone might get confused about what goes where in the vector, we should also provide procedures to set and get values. If we want to prevent someone from changing something (e.g., the price) what can we do? Good programming techniques: Encapsulation * Goal: Limit access to predefined techniques Simula (mid 1960's) started the idea of objects Object: Collects data and the operations on those data Simultaneously limits what can be done to the object Hmmm ... rather than thinking about applying procedures to objects, why not think about objects as procedures? Here's a simple example. It's an extra-credit counter. You can tell it when you deserve extra credit. You can't tell it to lose extra credit. You can't get more than 3 points of extra credit. (define andys-extra-credit (let ((ec (vector 0))) (lambda (message) (cond ; Increment count ((eq? message ':more) (vector-set! ec 0 (+ 1 (vector-ref ec 0)))) ; Get count ((eq? message ':check) (vector-ref ec 0)) ; Default (else (error "Bozo"))))) Example of its usage: ; Andy attends Richard's concert (andys-extra-credit ':more) ; Andy attends cool summer research talk (andys-extra-credit ':more) ; How much has Andy accumulated? (andys-extra-credit ':check) 2 ; We need a vector to store Andy's ec count ---------------------------------------- Here's the stuff from DrScheme (define andys-extra-credit (let ((ec (vector 0))) (lambda (message) (cond ; Increment count ((eq? message ':more!) (if (< (vector-ref ec 0) 3) (vector-set! ec 0 (+ 1 (vector-ref ec 0))) (error "That's enough extra credit!"))) ; Get count ((eq? message ':check) (vector-ref ec 0)) ; Default (else (error "Bozo")))))) ; Goal: Create a procedure that returns extra-credit ; objects (define make-ec (lambda () (let ((ec (vector 0))) (lambda (message) (cond ; Increment count ((eq? message ':more!) (if (< (vector-ref ec 0) 3) (vector-set! ec 0 (+ 1 (vector-ref ec 0))) (error "That's enough extra credit!"))) ; Get count ((eq? message ':check) (vector-ref ec 0)) ; Default (else (error "Bozo")))))))