Object-Oriented Programming

At this beginning of this course, we compared computer code to a recipe: A recipe has ingredients and instructions, and computer code has data (variables) and statements.

Functions let us organize and “hide” our instructions, letting us execute lots of instructions with a single statement. They make our code a bit easier to understand, easier to organize, and more powerful.

Thinking In Objects

However - let’s think about the more complex computer programs we deal with on an everyday basis:

  • A videogame might have characters and items, each with their own attributes such as a name, health, etc.
  • A webpage might have interactive buttons, input boxes, ads, and images.
  • An app on your phone might have multiple pages, each with their own buttons that do various things.

What do all these types of software have in common?

Well, they all have various Objects in them.
Object-Oriented Programming (or OOP for short) refers to a way of coding that builds on this concept of objects.

An Object in code will have two types of things on it:

  1. Data (called member variables), and
  2. Methods (which are called just another name for functions that belong to an object).

An Example: Nice Kitty!

As an example, let’s say we wanted to write a program that has a cat in it. First, let’s think about the attributes a cat might have that make it unique:

  1. A name
  2. Fur color
  3. A weight
  4. A particular Tail length

Given what you know about writing code, how would you express these attributes?
That’s right, with variables! It’s just that in this case, these variables conceptually all belong to a single object.

Next, let’s think about the things a cat does:

  1. Eats
  2. Sleeps
  3. Purs
  4. Meows
  5. Hisses

These are all verbs describing actions. What does that remind you of when it comes to code? That’s right - these actions could all be expressed as statements in code. It turns out that in Object-Oriented Programming, the statements for an object are always organized into methods, aka “member functions”.

If this is all a little confusing - no worries, we’re going to go ahead and build this Cat object in the next section, and all will become clear!


Next submodule: