CSC152 2005S, Class 2: An Introduction to Object-Oriented Programming Admin: * Homework due: HW1 (tomorrow is okay) * Homework assigned: HW2: Course Guidelines * Homework assigned: Read "An Introduction to Unix (in MathLAN)" * Office hours: M-Th, 1:15-2:05. * Sarcasm can be reduced. Let me know. * Sam is sometimes absent. Cassie usually teaches instead. * What to call me. Overview: * Object-Oriented Programming basics * Abstract Data Types and Data Structures * Exercise: Rational Numbers Object-Oriented Programming * A paradigm / philosophy of programming in which programs are structured as collections of "objects" * In computer science terms, an object collects * Data * Capabilities * Example: Most magazines have * Data * Words * Pictures * Topic or Topics * Title * Issue Number * Pages * Glossiness * Status value * Capabilities * Get the text and graphics on some page * Look-up information * Combust * Use as projectile * Remove pages * Example: Pairs in Scheme have (e.g., (define x ('a . 2))) * Data * car (a) * cdr (2) * Capabilities * get the car * get the cdr * embed in another pair (perhaps an operation on the other pair) * create a new pair (e.g., (cons 3 2)) * change the car (set-car! x 2) * change the cdr (set-cdr! x 'b) * Three basic capabilities associated with each object: + Constructors - Build new things + Observers/Accessors - Get information, but don't change the object + Mutators - Change the object * Why use object-oriented programming? + The real world is composed of objects, so why not include objects in our programs? + Object-oriented programming provides additional benefits, particularly for large programs - Objects "encapsulate" data - Objects support reuse * Objects are normally grouped into "classes" * Classes specify what fields and methods objects within the class have * Different objects have different values for those fields * Classes can be defined in terms of other classes * Example: Magazine library has, for each magazine * Name of borrower, if there is one, so we can report them undewr the patriot act * Permitted length of withdrawal * Location or call number or both * When it was checked out * Condition * ETC * PLUS ALL THE THINGS WE HAVE FOR A "REGULAR" MAGAZINE * In most object-oriented languages, you can say that "LibraryMagazine is a SUBCLASS of Magazine" * It automatically "inherits" all of the fields and methods of Matazine * You can add new fields and methods * You can also change the behavior of methods * The idea that you can define classes in terms of other classes is called INHERITANCE * Inheritance supports a third key idea in OOP: Polymorphism * Anything I can do with an object in a class, I can do with objects in its subclasses Programmers therefore like OOP, even when they aren't modelling the world. * Encapsulation creates safer code * Inheritance promotes reuse * Polymorphism promotes reuse New topic: Abstract Data Types and Data Structures * The way you organize the information in your program has a significant affect on the algorithms you write * We need ways to talk about the organization of data in a program * We need a common voculabary for such discussions * We'll have some common organizations of data * N common ways of organizing data * Lists * Vectors * Pairs * Trees * Two basic ways of discussing the organziation of data * Capabilities: * Scheme Lists permit you to create them (cons,null), to get the car (car), to get the cdr (cdr), and to determine emptiness (null?). Everything else can be built from those five basic operations. * Organization of memory within the computer * Scheme lists are created with a linked set of CONS cells (pairs). The second element of each CONS cell points to the next CONS cell for the list. * In this class, we will first strive to talk about ABSTRACT DATA TYPES (ADTS), which focus on capabilities * Once we've determined those operations, we can discuss the underlying DATA STRUCTURE(s) used to implement them * Example, suppose I wanted to keep track of last name, first name, email, and numeric grade for each student * Create(lname,fname,email,grade) * getLname() * getFname() * getEmail() * getGrade() * increaseGrade() * decreaseGrade() * One implementation technique: Association list (define create (lambda (lname fname email grade) (list (cons 'lname lname) (cons 'fname fname) (cons 'email email) (cons 'grade grade)))) (define getLname (lambda (student) (assoc student 'lname))) * Another implementation technique: Vector (define create (lambda (lname fname email grade) (vector lname fname email grade))) (define getLName (lambda (student) (vector-ref student 0))) Warning! Subtle thing about Object-Oriented Languages * We are used to saying "apply this operation to these values" (getlname brad) * We now have to say "Hey object, do this operation and give me a return value" brad.getlname() * First place this might get confusing Old style: add(fraction1,fraction2) new style: fraction1.add(fraction2)