Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"

Similar presentations


Presentation on theme: "1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming""— Presentation transcript:

1 1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"

2 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

3 University of Utah – School of Computing University of Utah 3 Large-Scale Programming Hard to write BIG programs using procedural programming techniques WHY?

4 University of Utah – School of Computing University of Utah 4 Problem #1 Global Variables!

5 University of Utah – School of Computing University of Utah 5 Problem #2 Limits of human comprehension

6 University of Utah – School of Computing University of Utah 6 Problem #3 Brittle dependencies

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

8 University of Utah – School of Computing University of Utah 8 Analogy Toy blocks

9 University of Utah – School of Computing University of Utah 9 Classes Have variables and functions Or "attributes" and "behaviors"

10 University of Utah – School of Computing University of Utah 10 10 Example Consider our geometry program What attributes does a rectangle have?

11 University of Utah – School of Computing University of Utah 11 11 Rectangle class class Rectangle { int length; int width; }

12 University of Utah – School of Computing University of Utah 12 12 Rectangle class What do you want to do with a rectangle? -Draw it? -Resize it? -Calculate area? -Calculate perimeter?

13 University of Utah – School of Computing University of Utah 13 13 Methods class Rectangle { int width; int height; void draw() { //do stuff } double computePerimeter() { //do stuff } };

14 University of Utah – School of Computing University of Utah 14 14 How to draw a rectangle? void draw() { }

15 University of Utah – School of Computing University of Utah 15 15 How to find a rectangle's perimeter? double computePerimeter() { }

16 University of Utah – School of Computing University of Utah 16 16 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;

17 University of Utah – School of Computing University of Utah 17 17 Block example revisited Wrap up all attributes and methods inside a class Easier to manage! Safer too? Why?

18 University of Utah – School of Computing University of Utah 18 18 Car example You can drive a car without knowing how it works internally "public" user interface: -steering wheel -gas/brake pedals -ignition

19 University of Utah – School of Computing University of Utah 19 19 Car example You can drive a car without knowing how it works internally "private" inner workings -don't know, don't care

20 University of Utah – School of Computing University of Utah 20 20 "public" explained public methods and variables -Other classes can use them A struct's variables are public by default.

21 University of Utah – School of Computing University of Utah 21 21 "private" also! private methods and variables -Other classes may NOT access them Used for internal data By default, everything in a class is private

22 University of Utah – School of Computing University of Utah 22 22 A General Rule private variables -they are our internal data public methods -they are how other classes use our class

23 University of Utah – School of Computing University of Utah 23 23 Rectangle class revisited class Rectangle { private: int width; int height; public: void draw() { //do stuff } double computePerimeter() { //do stuff }

24 University of Utah – School of Computing University of Utah 24 24 Why public and private? Secrecy? No. Simplicity? Yes. Order? Yes. Maintainability? Yes.

25 University of Utah – School of Computing University of Utah 25 25 Examples Math.random() JPanel.add()

26 University of Utah – School of Computing University of Utah 26 26 Reminder Homework 5 is due tomorrow

27 University of Utah – School of Computing University of Utah 27 27 Request for Comments Please fill out online survey!


Download ppt "1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming""

Similar presentations


Ads by Google