1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"
-
Upload
marcia-miles -
Category
Documents
-
view
213 -
download
0
Transcript of 1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"
University of Utah – School of Computing
University
of Utah
22
Procedural Programming
• What we've done so far:- variables- functions that operate on those variables
University of Utah – School of Computing
University
of Utah
33
Large-Scale Programming
• Hard to write BIG programs using procedural programming techniques
• WHY?
University of Utah – School of Computing
University
of Utah
55
Problem #2
• Limits of human comprehension
University of Utah – School of Computing
University
of Utah
77
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
99
Classes
• Have variables and functions• Or "attributes" and "behaviors"
University of Utah – School of Computing
University
of Utah
1010
Example
• Consider our geometry program• What attributes does a rectangle have?
University of Utah – School of Computing
University
of Utah
1111
Rectangle class
class Rectangle {int length;int width;
}
University of Utah – School of Computing
University
of Utah
1212
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
1313
Methods
class Rectangle {
int width;
int height;
void draw() {
//do stuff
}
double computePerimeter() {
//do stuff
}
};
University of Utah – School of Computing
University
of Utah
1414
How to draw a rectangle?
void draw() {
}
University of Utah – School of Computing
University
of Utah
1515
How to find a rectangle's perimeter?
double computePerimeter() {
}
University of Utah – School of Computing
University
of Utah
1616
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
1717
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
1818
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
1919
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
2020
"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
2121
"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
2222
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
2323
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
2424
Why public and private?
• Secrecy? No.• Simplicity? Yes.• Order? Yes.• Maintainability? Yes.
University of Utah – School of Computing
University
of Utah
2525
Examples
• Math.random()• JPanel.add()
University of Utah – School of Computing
University
of Utah
2626
Reminder
• Homework 5 is due tomorrow