Docsity

Object Oriented Programming - Introduction to Computer Science I | CS 110, Papers of Computer Science

Toggle sidebar
bg1
CS 110: Introduction to Computer Science Spring 2008
Object-Oriented Programming
Historical Perspective
Object-oriented programming was a huge conceptual change in 80s and 90s.
Simula, Algol, Smalltalk were early O-O languages
C++ was born from the language‘C’
Java was born
Grouping Data
Our brains can only handle 7 +- 2 things at a time.
We must create abstractions and think hierarchically.
Function allow us to abstract sequence of operations.
We also need to abstract groupings of data, something we call ‘complex data types’ or
‘classes’.
Consider a social network site. What data might be recorded for each person in the
system?
Other examples of grouping data:
Google result
A color
How about the Mastermind program? What data did we use that could have been grouped
but wasn’t?
Grouping Data in Python
How would you represent a person in Python?
‘Lists’ don’t really help here as you have related but heterogeneous data. You want to
name each part, e.g., person.lastName, person.firstName, etc.
pf3
pf4

Partial preview of the text

Download Object Oriented Programming - Introduction to Computer Science I | CS 110 and more Papers Computer Science in PDF only on Docsity!

Object-Oriented Programming

Historical Perspective

Object-oriented programming was a huge conceptual change in 80s and 90s. Simula, Algol, Smalltalk were early O-O languages C++ was born from the language‘C’ Java was born

Grouping Data

Our brains can only handle 7 +- 2 things at a time. We must create abstractions and think hierarchically. Function allow us to abstract sequence of operations. We also need to abstract groupings of data, something we call ‘complex data types’ or ‘classes’. Consider a social network site. What data might be recorded for each person in the system? Other examples of grouping data: Google result A color How about the Mastermind program? What data did we use that could have been grouped but wasn’t?

Grouping Data in Python

How would you represent a person in Python? ‘Lists’ don’t really help here as you have related but heterogeneous data. You want to name each part, e.g., person.lastName, person.firstName, etc.

Python Classes

The Class construct allows a programmer to define custom types ( programmer-defined types ). Similar syntax to function def, but it’s a class definition. Define your fields by setting them to initial values. class Person: # class definition name="" age= friends = [] Note that the variables are being set outside any function! Once you define a class, you can create instances of it. We call these instances objects. Create them with an object creation statement, set them using dot notation.

main

joe = Person() # this creates a Person object with name="" and age= bill = Person() # this creates a Person object with name="" and age= print joe.age # prints 0 joe.age= joe.name="Joe Williams" bill.name="William Joes" bill.age= Formally, object creation statements are of the form: = () Field assignment statements use "." to specify which field: .

Terminology

Person is a class. joe and bill are instances of the class Person. joe and bill are objects of type Person. name and age are fields (also known as data members ) of the class Person. A class can also have functions defined within it (we’ll revisit this later).

The first parameter is usually, by convention, called self. Self refers to the object on the left-hand side of the "." in the object-oriented function call There are really still two parameters sent to the function distance.

In Class Problems 2

  1. Copy your coordinate program into another file.
  2. Rewrite the code so that distance is a method inside the Coordinate class. You'll need to rewrite the main so that distance is called in an object-oriented manner.
  3. Add a method 'equal' which returns true if two coordinate objects have the same x and

y values. Write a test for this method and show how 'equal' is different from '=='.