{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSC326 Lecture 8 - CSC326 Object Oriented Programming i...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC326 Object Oriented Programming i CSC326 Object Oriented Programming
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CSC326 Object Oriented Programming ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ
Background image of page 2
CSC326 Object Oriented Programming iii Contents 1 Agenda 1 2 Classes and Objects 1 3 Classes and Functions 3 4 Classes and Methods 4 5 Operator Overloading 5 6 Inheritance 6 7 Recap 7
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CSC326 Object Oriented Programming 1 / 7 1 Agenda • Classes and Objects • Classes and Attributes • Classes and Methods • Operator Overloading • Inheritance 2 Classes and Objects • We have seen built-in types • User defined types: class • Example: point in 2-D space class Point( object ) : """reprsents a point in 2-D space""" • header: indicates a new class with name "Point" • header: indicates the class is a kind of "object", a built-in type • body: a docstring explaining what the class is for >>> print Point < class ’__main__.Point’ > • Class is a factory of objects Call Point() to create instance as if it were a function returns a "reference" to a Point object >>> blank = Point() >>> print blank <__main__.Point instance at 0xb7e9d2ac> • Note Point instance and Point class is NOT the same thing >>> print type(blank) < class ’__main__.Point’ > • Attributes Each object has named elementes, called attributes (or fields) In python, attribuites are introduced by use (with dot notation), not by declaration (C++)
Background image of page 4
CSC326 Object Oriented Programming 2 / 7 >>> blank.x = 3.0 >>> blank.y = 4.0 >>> print blank.x 3.0 >>> distance = math.sqrt( blank.x ** 2 + blank.y ** 2) >>> print distance 5.0 • Rectangle class Rectangle( object ) : """ represent a rectangle. attributes: width, height, corner """ box = Rectangle() box.width = 100.0 box.height = 200.0 corner = Point() box.corner.x = 0.0 box.corner.y = 0.0 def find_center( box ) : p = Point() p.x = box.corner.x + box.width/2.0 p.y = box.corner.y + box.height/2.0 return p • Objects are mutable def grow_rectangle( rect, dwidth, dheight ) : rect.width = dwidth rect.height = dheight
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}