classes objects
Download
Skip this Video
Download Presentation
Classes / Objects

Loading in 2 Seconds...

play fullscreen
1 / 23

Classes / Objects - PowerPoint PPT Presentation


  • 104 Views
  • Uploaded on

Classes / Objects. An introduction to object-oriented programming. Object-oriented programming (OOP) allows you, the programmer, to create objects You have been using objects all along, you just weren’t aware of it …

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Classes / Objects' - khanh


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
classes objects

Classes / Objects

An introduction to object-oriented programming

what is object oriented programming

Object-oriented programming (OOP) allows you, the programmer, to create objects

  • You have been using objects all along, you just weren’t aware of it …
  • Every time you call a rect(); or an ellipse();, you are calling an object called an ellipse. The ellipse has properties (x,y,w,h)
What is object-oriented programming?
why objects

OOP allows you to put data about something and actions it can do together into one “object”

  • OOP has been around since the 1960s
  • Most popular current languages have either adapted or were designed with OOP
    • C  C++  C#
    • JavaScript
    • Java … and Processing
Why Objects?
human example
Attributes

Functions

Sleep

Wake-Up

Eat

Ride something

Human example
  • Age
  • Height
  • Weight
  • Name
  • Eye color
  • Hair color
classes and objects
A Class

An Object

An ‘instance’ of a Class: A Cookie

A ‘real’ thing that has all of the specifics

It can be ‘told’ to execute it’s functions

Classes and Objects
  • A ‘plan’ for an object: Cookie Cutter
  • The general idea of a thing
  • Has placeholders for details, but not ‘made’ yet
  • It declares functions that could be done to an object
  • You have to have a ‘plan’ before you can make an object:
    • Class before Object
plan a car class

Let’s plan a simple Car

    • List data and functions
Plan a Car Class
  • Let’s compare how we could convert the function we wrote to show different cars with objects
  • We’ll use our setup() and draw() methods
setup for a car before objects

Use global variables for ‘properties’

    • Color: carColor
    • Location: carX, carY
    • Speed: carSpeed
  • In setup()
    • Set the carColor
    • Set the starting location
    • Set the initial speed
  • In draw()
    • Fill background
    • Display car at location with color (may use a function)
    • Increment car’s location based on speed
Setup for a ‘Car’ before Objects
setup multiple cars before objects
Setup multiple ‘Cars’ before Objects
  • Use global arrays of variables for ‘properties’
    • Allows us to use a loop to initialize a number of Cars
    • Declare Color array: carColor [ ]
    • Declare carX array: carX [ ]
    • Declare carY array: carY [ ]
    • Declare carSpeed array: carSpeed [ ]
  • In setup()
    • Set and initialize the arrays
  • In draw()
    • Fill background
    • Loop through all the cars
      • Display car at location with color (may use a function)
      • Increment car’s location based on speed
setup a car using objects
Setup a ‘Car’ using Objects

We end up with one variable to represent the car

Instead of initializing all of those variables, we initialize the car object!

  • One big difference with objects is that you move all of the global variables inside the Car object
    • Color: carColor
    • Location: carX, carY
    • Speed: carSpeed
    • Car object instead!
setup a car using objects1
Setup a ‘Car’ using Objects
  • Outside of all methods (global)
    • Declare a ‘parking place’ for a car
  • In setup()
    • Make a new car object
      • based on the ‘Car’ plan)
      • Sets initial values for color, location and speed
  • In draw()
    • Fill background
    • Tell the car to ‘move’ based on speed
    • Tell the car to ‘display’ itself (call a function)
the car class
The ‘Car’ class

Convert the non-OOP Car Data to a Class

Non-OOP OOP

setup a car using objects data
Setup a ‘Car’ using Objects - Data
  • Let’s break it down step by step
  • Declare the plan for the ‘Car’
    • Outside draw() and setup()
    • Put variables for color, location and speed inside
setup a car using objects constructor
Setup a ‘Car’ using Objects - Constructor
  • We need to write instructions to build a car
    • It is called the ‘Constructor’ method
    • Move the code that was in setup()
    • Set variables for color, location and speed inside
setup a car using objects functions
Setup a ‘Car’ using Objects - Functions
  • Move the functions that did things to the car
    • Move the code to inside the Car class
    • The will be called methods of the class
    • Move the code that was in display()and drive()
the whole car example 8 1
The whole ‘Car’ (Example 8.1)

class Car { // Define a class for a car

color c; // Variables.

float xpos;

float ypos;

float xspeed;

Car() { // A constructor.

c = color(175);

xpos = width/2;

ypos = height/2;

xspeed = 1;

}

void display() { // Function.

// The car is just a square

rectMode(CENTER);

stroke(0);

fill(c);

rect(xpos,ypos,20,10);

}

void drive() { // Function.

xpos = xpos + xspeed;

if (xpos > width) {

xpos = 0;

}

}

}

what was that about a constructor
What was that about a ‘Constructor’?

A constructor is a special method of a class

Has the same name as the class

“Builds’ the object

Sets initial values

It is ‘called’ with you use new:

void setup() {

size(200,200);

// Initialize Car object

myCar = new Car();

}

class Car {

Car() { // Constructor

c = color(175);

xpos = width/2;

ypos = height/2;

xspeed = 1;

}

and here is the oopized version

Is the OOP version shorter?

  • Is it easier to read?
    • Not yet maybe, but soon, and for the rest of your life.
And here is the OOPized version
what if we want to make more cars
What if we want to make more cars?

Right now, all cars are exactly the same

Their constructor sets up the color, location…

How could we make ‘custom’ cars?

Remember parameters?

What if a Car constructor took parameters?

class Car {

Car(color colp, intxp, intyp, intspeedp) {

c = colp;

xpos = xp;

ypos = yp;

xspeed = speedp;

}

please review parameter passing
Please review parameter passing…

Make a new frog with a length of 100

now we can make two custom cars
Now we can make two ‘custom’ cars

Use your new ‘parameterized’ constructor!

and imagine an array of cars
And imagine an array of Cars!

You can use the ‘Car’ class just like any other type

Declare an array of our new Cars object:

Car [ ] parkingLot;

setup() {

parkingLot = new Car[10];

}

But wait… what just happened?

Did you create 10 cars? No, not yet.

You created 10 parking stalls for cars

So we still have to ‘build’ the cars and set all of the colors, locations and speeds…

parkingLot[0] = new Car(color, x, y..);

filling the parking lot the easy way
Filling the parking lot the easy way!

Once you have the parking lot created,

Car [ ] parkingLot;

setup() {

parkingLot = new Car[10];

}

Use a for loop to make a bunch of cars!

for (inti; i < 10; i++) {

parkingLot[i] = new Car(color, x, y..);

}

Arrays and loops work wonderfully together!

And they are even more fun with objects!

summary
Objects provide a different way of designing programs
    • Combine data and functionality
  • A class is a ‘plan’ for an object
  • An object is an ‘instance’ of a class
  • The ‘new’keyword is used to instantiate (create) an object from a class
  • Use the ‘dot’ operator ‘.’ to access member data and functions of the class
  • Classes are normally written in separate files
    • One class per file
    • Put the class ‘source’ file in the same folder as the ‘main’
Summary
ad