Introduction to Object-Oriented Programming (OOP) II

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein Classes and Objects Object Oriented Programming.
Recitation 6 Programming for Engineers in Python.
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Classes 2 COMPSCI 105 S Principles of Computer Science.
Unit 8 Classes and Objects; Inheritance Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)
Classes 3 COMPSCI 105 S Principles of Computer Science.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in.
Introduction. 2COMPSCI Computer Science Fundamentals.
Introduction to Pyrex September 2002 Brian Quinlan
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Overriding toString()
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Object Oriented Programing (OOP)
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
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,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Exceptions and Handling
INTRO2CS Tirgul 8 1. What We’ll Be Seeing Today  Introduction to Object-Oriented Programming (OOP).  Using Objects  Special methods 2.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
EECS 110: Lec 14: Classes and Objects
CMSC201 Computer Science I for Majors Lecture 25 – Classes
EECS 110: Lec 15: Classes and Objects (2)
COMPSCI 107 Computer Science Fundamentals
Guide to Programming with Python
Object Oriented Programming
Classes and Objects; Inheritance
EECS 110: Lec 17: Review for the Final Exam
COMPSCI 107 Computer Science Fundamentals
Example: Vehicles What attributes do objects of Sedan have?
Software Development Java Classes and Methods
Copyright (c) 2017 by Dr. E. Horvath
Fundamentals of Programming II Equality and Multiple Inheritance
Fundamentals of Programming II Interfaces and Implementations
Fundamentals of Programming I More Data Modeling
Object-Orientated Programming
Introduction to Object-Oriented Programming (OOP)
Adapted from slides by Marty Stepp and Stuart Reges
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Introduction to Object-Oriented Programming (OOP)

EECS 110: Lec 14: Classes and Objects
Fundamentals of Programming I Commonly Used Methods More Modeling
Fundamentals of Programming I Windows, Labels, and Command Buttons
Object Oriented Programming in A Level
Lecture 18 Python OOP.
A Level Computer Science Topic 6: Introducing OOP
Introduction to Object-Oriented Programming (OOP)
Object-Oriented Design AND CLASS PROPERTIES
Searching and Sorting (1)
Introduction to Object-Oriented Programming (OOP) II
COMPUTING.
Introduction to PYTHON
Presentation transcript:

Introduction to Object-Oriented Programming (OOP) II

Operator overloading We have learned some basic features of OOP Constructor: def __init__(self): String representation: def __str__(self):, or def __repr__(self): Method within a class: def tomorrow(self): Object attributes (object variables …) self.year, self.month, self.day. We now will discuss the topic of operator overloading

What does it mean? An operator such as ‘==‘, ‘>’ can be associated with a function to reflect its meaning. E.g., in our Date class, we have three functions isEuqal(), isBefore(), isAfter() When comparing two Date objects, we’d say d1.isEqual(d2), d1.isBefore(), d1.isAfter() If we implement operator overloads for the Date class, we could have said d1 == d2, d1 < d2, d1 > d2

Overloading ‘==‘ class Date: … def __eq__(self, other): if self.year == other.year and \ self.month == other.month and \ self.day == other.day: return True else: return False If the function isEqual() has been defined, we can do … class Date: … def __eq__(self, other): if self.isEqual(other): return True else: return False class Date: … def __eq__(self, other): return self.isEqual(other)

Overloading ‘>‘ Overloading ‘>=‘ Run demonstration of Date class class Date: … def __gt__(self, other): return self.isAfter(other) Overloading ‘>=‘ class Date: … def __ge__(self, other): return self.isAfter(other) or \ self.isEqual(other) Run demonstration of Date class

Class exercises Given a Book class as follows, define methods to overload ‘>’, ‘<‘, ‘>=‘, ‘<=‘, and ‘==‘, if the comparison is based on the attribute ‘pubYear’ class Book: def __init__( self, title, author, pubYear ): ''' Create an object self.author = author self.title = title self.pubYear = pubYear # an integer

Class exercises If the comparison is based on the attribute ‘title’, write the method that overloads ‘>’ using string comparison. class Book: def __init__( self, title, author, pubYear ): ''' Create an object self.author = author # a string self.title = title # a string self.pubYear = pubYear # an integer

Class exercises If the comparison is based on the attribute ‘pubYear’, if ‘pubYear’ is the same, then check ‘title’, if title is the same, check ‘author’. class Book: def __init__( self, title, author, pubYear ): ''' Create an object self.author = author # a string self.title = title # a string self.pubYear = pubYear # an integer

Other operator overload Python supports more operator overload __ne__ : not equal __contains__ : membership check __add__ : add to the collection (+) __iadd_ : for += See BookShelf demonstration book.py, book_shelf.py, book_shelf_app.py