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

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

CSC326 Object Oriented Programming i CSC326 Object Oriented Programming
Image of page 1

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

CSC326 Object Oriented Programming ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ
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
Image of page 3

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

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++)
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
Image of page 5

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

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern