1 / 20

Objects

Objects. Object Oriented Programming (OOP). OOP is a different way to view programming Models the real world A different view than ‘Structured’ programming Functions break a program into ‘modules’ like an outline What is an Object? Usually based on a ‘noun’ It has ‘properties’

bonner
Download Presentation

Objects

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects

  2. Object Oriented Programming (OOP) • OOP is a different way to view programming • Models the real world • A different view than ‘Structured’ programming • Functions break a program into ‘modules’ like an outline • What is an Object? • Usually based on a ‘noun’ • It has ‘properties’ • It has ‘actions’ (methods) that it can do (verbs) • An examples of an object that you know: • String • The dot operator is used to make the String do work!

  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

  4. Object Example: Human • Functions • Sleep • Wake-Up • Eat • Ride something • Attributes • Age • Height • Weight • Name • Eye color • Hair color • …

  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 it’s functions • You have to have a ‘plan’ before you can make an object: • Class before Object

  6. Exercise 8.1: Plan a Car Class Let’s plan a simple Car List data and functions Let’s compare how we could convert the function we wrote to show different card with objects We’ll use our setup() and draw() methods

  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 car’s location based on speed

  8. 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! 8

  9. 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) 9

  10. The ‘Car’ class Convert the non-OOP Car Data to a Class Non-OOP OOP

  11. 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() 11

  12. 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 move() { // Function. xpos = xpos + xspeed; if (xpos > width) { xpos = 0; } } }

  13. 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; }

  14. 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.

  15. Car Class should really be in its own file… Click ‘Right Arrow’ in tabs row Use ‘New Tab’ in menu to create a new file Name the new tab exactly like the class = ‘Car’ Move the code for the ‘Car’ class to it Saves to a ‘Car.pde’ file in your folder Keep both files in the same folder If you want to use your ‘Car’ class in another project, simply copy the ‘Car.pde’ file there

  16. Your first multi-file Project!

  17. 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; }

  18. Now we can make two ‘custom’ cars Use your new ‘parameterized’ constructor!

  19. 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..);

  20. 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!

More Related