Classes / Objects An introduction to object-oriented programming

  • View
    214

  • Download
    2

Embed Size (px)

Text of Classes / Objects An introduction to object-oriented programming

  • Slide 1
  • Classes / Objects An introduction to object-oriented programming
  • Slide 2
  • 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 werent 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)
  • Slide 3
  • 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
  • Slide 4
  • Human example Attributes Age Height Weight Name Eye color Hair color Functions Sleep Wake-Up Eat Ride something
  • Slide 5
  • Classes and Objects A Class 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 An Object An instance of a Class: A Cookie A real thing that has all of the specifics It can be told to execute its functions You have to have a plan before you can make an object: C lass before O bject
  • Slide 6
  • Plan a Car Class Lets plan a simple Car List data and functions Lets compare how we could convert the function we wrote to show different cars with objects Well use our setup() and draw() methods
  • Slide 7
  • 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 cars location based on speed
  • Slide 8
  • 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 cars location based on speed
  • Slide 9
  • Setup a Car using Objects 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! We end up with one variable to represent the car Instead of initializing all of those variables, we initialize the car object!
  • Slide 10
  • 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)
  • Slide 11
  • The Car class Convert the non-OOP Car Data to a Class Non-OOP OOP
  • Slide 12
  • Setup a Car using Objects - Data Lets break it down step by step Declare the plan for the Car Outside draw() and setup() Put variables for color, location and speed inside
  • Slide 13
  • 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
  • Slide 14
  • 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()
  • Slide 15
  • 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; }
  • Slide 16
  • 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 : class Car { Car() { // Constructor c = color(175); xpos = width/2; ypos = height/2; xspeed = 1; } void setup() { size(200,200); // Initialize Car object myCar = new Car(); }
  • Slide 17
  • 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.
  • Slide 18
  • 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, int xp, int yp, int speedp) { c = colp; xpos = xp; ypos = yp; xspeed = speedp; }
  • Slide 19
  • Please review parameter passing Make a new frog with a length of 100
  • Slide 20
  • Now we can make two custom cars Use your new parameterized constructor!
  • Slide 21
  • 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..);
  • Slide 22
  • 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 (int i; i < 10; i++) { parkingLot[i] = new Car(color, x, y..); } Arrays and loops work wonderfully together! And they are even more fun with objects!
  • Slide 23
  • 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