1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"
University of Utah – School of Computing University of Utah 2 Procedural Programming What we've done so far: -variables -functions that operate on those variables
University of Utah – School of Computing University of Utah 3 Large-Scale Programming Hard to write BIG programs using procedural programming techniques WHY?
University of Utah – School of Computing University of Utah 4 Problem #1 Global Variables!
University of Utah – School of Computing University of Utah 5 Problem #2 Limits of human comprehension
University of Utah – School of Computing University of Utah 6 Problem #3 Brittle dependencies
University of Utah – School of Computing University of Utah 7 Conceptual Leap 1. Dividing a large job into small functions 2. Dividing several large jobs (or concepts) into multiple classes!
University of Utah – School of Computing University of Utah 8 Analogy Toy blocks
University of Utah – School of Computing University of Utah 9 Classes Have variables and functions Or "attributes" and "behaviors"
University of Utah – School of Computing University of Utah Example Consider our geometry program What attributes does a rectangle have?
University of Utah – School of Computing University of Utah Rectangle class class Rectangle { int length; int width; }
University of Utah – School of Computing University of Utah Rectangle class What do you want to do with a rectangle? -Draw it? -Resize it? -Calculate area? -Calculate perimeter?
University of Utah – School of Computing University of Utah Methods class Rectangle { int width; int height; void draw() { //do stuff } double computePerimeter() { //do stuff } };
University of Utah – School of Computing University of Utah How to draw a rectangle? void draw() { }
University of Utah – School of Computing University of Utah How to find a rectangle's perimeter? double computePerimeter() { }
University of Utah – School of Computing University of Utah Classes vs. Objects A class is just the "definition" or the code An object is when you make a variable whose type is a class Example: - Rectangle r;
University of Utah – School of Computing University of Utah Block example revisited Wrap up all attributes and methods inside a class Easier to manage! Safer too? Why?
University of Utah – School of Computing University of Utah Car example You can drive a car without knowing how it works internally "public" user interface: -steering wheel -gas/brake pedals -ignition
University of Utah – School of Computing University of Utah Car example You can drive a car without knowing how it works internally "private" inner workings -don't know, don't care
University of Utah – School of Computing University of Utah "public" explained public methods and variables -Other classes can use them A struct's variables are public by default.
University of Utah – School of Computing University of Utah "private" also! private methods and variables -Other classes may NOT access them Used for internal data By default, everything in a class is private
University of Utah – School of Computing University of Utah A General Rule private variables -they are our internal data public methods -they are how other classes use our class
University of Utah – School of Computing University of Utah Rectangle class revisited class Rectangle { private: int width; int height; public: void draw() { //do stuff } double computePerimeter() { //do stuff }
University of Utah – School of Computing University of Utah Why public and private? Secrecy? No. Simplicity? Yes. Order? Yes. Maintainability? Yes.
University of Utah – School of Computing University of Utah Examples Math.random() JPanel.add()
University of Utah – School of Computing University of Utah Reminder Homework 5 is due tomorrow
University of Utah – School of Computing University of Utah Request for Comments Please fill out online survey!