chapter 17 q and a
Skip this Video
Download Presentation
Chapter 17 Q and A

Loading in 2 Seconds...

play fullscreen
1 / 14

Chapter 17 Q and A - PowerPoint PPT Presentation

  • Uploaded on

Chapter 17 Q and A. Victor Norman, et al. CS104. What is Object-oriented Programming?. Q: What is object-oriented programming? A: It means defining classes/objects, which hold data, but also can do stuff to themselves or for the caller. Functional style: set_add (set, newItem )

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Chapter 17 Q and A' - diep

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 17 q and a

Chapter 17 Q and A

Victor Norman, et al.


what is object oriented programming
What is Object-oriented Programming?

Q: What is object-oriented programming?

A: It means defining classes/objects, which hold data, but also can do stuff to themselves or for the caller.

  • Functional style: set_add(set, newItem)
    • Pattern: function(object, values)
  • OO style: set.add(newItem)
    • Pattern: object.method(values)
example set class type
Example: Set class (type)
  • Old style:
    • set = newSet(<optional values>)
    • set = setAdd(set, newItem)
    • set = setDel(set, delItem)
    • tOrF = setIn(set, value)
    • set = setAnd(set, otherSet)
    • set = setOr(set, otherSet)
  • New style:
    • set = Set(<optional values>)
    • set.add(newItem)
    • set.del(delItem)
    • tOrF =
    • set = set.and(otherSet)
    • set = set.or(otherSet)
binding together data and ops
Binding together data and ops
  • A class definition binds together
    • the data an instance stores (its attributes) and
    • the operations on the data (called “methods”)
  • Forces programmer to keep related code together.
  • Allows you to create a new type.
advantages of oo
Advantages of OO
  • You can control how an object can be created and used.
    • because you write the code for how an object is constructed.
  • You can hide how an object implements its operations (data hiding).
    • because you define what a user can do with objects, so the user doesn’t have to know how it works.
  • Suppose we want to define a type Scribbler, which will represent a programmable robot.
  • We want it to be able to move around, keep track of where it is and what direction it is pointing, etc.
    • it might even keep track of where it saw obstacles, etc.
  • Its attributes would be: x, y, angle, etc.
example 2
Example (2)
  • To create one, we’d do this code:scrib = Scribbler()
    • calls the constructor code we’ve defined:

class Scribbler:

def __init__(self):

self._x = 0

self._y = 0

self._angle = 0 # east

  • Now, we control how a Scribbler is made and ensure its state is correct.
example 3
Example (3)
  • What do we want to do a Scribbler?
  • scrib.move(16, 12)
    • moves the robot there and object remembers where it is and what angle it is facing.
  • scrib.getX()scrib.getY()scrib.getAngle()
    • scrib user can ask the scribbler where it is and what angle it is facing.
example 4
Example (4)
  • scrib.seeObstacle()
    • ask the scribbler if it sees an obstacle.
  • scrib.forwardUntilSeeObstacle()
  • scrib.setAngle(newAngle)
    • turns the scribbler to that angle
  • scrib.setY(newY)
    • moves the scribbler to new position
string representation
String representation
  • (Review: when constructor is called, __init__() is run in the class.)
  • When you do str(object), __str__ is run in the object’s class, if defined.
    • it must return a string, which should be a readable representation of that object.
  • Very convenient to be able to do:

print scrib # which calls str(scrib) before printing

str code
__str__ code

class Scribbler:

... init method …

def __str__(self):

return “Scribbler at (%g, %g), facing %g degrees” %

(self._x, self._y, self._angle)

print scrib


Scribbler at (3.0, 4.0), facing 45.0 degrees

self parameter
self parameter
  • if we want to do scrib.getAngle(), we implement:

…. in Scribbler class ….


return self._x

  • Notice that scrib self. Object before the . is the first parameter in the method definition.
  • self is the name of the object in code in the class.
operator overloading
Operator Overloading
  • if I do 3 + 4, we are adding integers.
  • if I do “hi” + “there” we are concatenating strings.
    • This is overloading of the operator +.
  • for a Set type, we might want to do this:

s = Set(…)

s = s + 3 # add 3 to the set.

  • We’d have to implement __add__ in Set class.
other operators to overload
Other operators to overload
  • Common operators to overload:
    • __cmp__(self, other): compares two objects. Return -1 if self < other, 0 if self == other, 1 if self > other. (If implemented, you can sort objects.)
    • __len__(self): can use len(obj). Useful, e.g., for Set type to ask how many items are in the set.
    • __contains__(self, item): if implemented, can ask if something is in the object.