Object-Oriented Programming

The heart of object-oriented programming is one simple idea: things know how to do stuff. In more technical terms, we say our data is represented by objects, which have both methods (things they can do) and local state (information about the object that is specific to the object). The latter is implemented by variables, which can be defined with a default value or set at instantiation time. (Of course, you can modify either kind of variable later with set!.) The last piece of OOP is inheritance, which means using parent classes to share common behavior and allow common interactions for similar objects.

Here are the most important rules for determining the relationship between two things:

There are already many great references for our object-oriented system and OOP in general in your reader. The best places to look are:

  1. The "Above the line view" reference at the beginning of the Volume II reader.
  2. The "Reference Manual" just after the explanation above. (In my Volume II reader it's on page 11, but I have an old copy.)
  3. Brian's lecture notes at the end of Volume II
  4. Other TAs' handouts and practice problems for Week 7.

If you're looking for some practice, here are several problems that test your understanding of object-oriented programming.

Mutation (and Doing Several Things)

In CS61A OOP, we're introduced to the special obj.scm syntax for classes and objects. But we also need two more Scheme special forms: set! and begin.

As a convention in Scheme, procedures and special forms dealing with mutation (changing things) end with an exclamation point, to mark that they are non-functional and might affect another part of the program. This is like how we use question marks for predicates (procedures returning #t or #f. I strongly encourage you to follow these conventions!

The set! special form (pronounced "set-bang") has the same syntax as define, where the first argument is the name of the variable you're setting, and the second is the value you're setting it to. Note that, like define, the first argument is not evaluated, though the second one is.

set! is used to change the value of existing variables, while define is used to make new variables. We'll see how this works next week, but for now, consider the following:

What will Scheme print?

Our other new special form is begin. This allows you to do multiple things in a row; the value of a begin expression is just the value of the last thing you do. So, (begin (display 3) 5) will display a three and then has the value 5. (Note that the value is not okay, or okay and 5, or (okay 5)!)

It turns out that cond clauses and lambda bodies have "implicit begins". So you can already say (lambda (x) (display x) (* x x)), without a begin. This also applies to anything implemented using lambda, such as procedure definitions, method definitions, and let. This means that in practice, you end up only needing begin when working with if.

There is an important distinction between displaying a value and returning a value. Almost always, you want to return a value, so it can be used by the calling procedure. (For example, if you ask for a person's name, having it printed doesn't help if you're trying to make a list of names.)

Stay tuned for next week, in which we learn what's underneath obj.scm's OOP. (Spoiler: it's lambda!)

Back to Jordy's home page | Course home page