Python Programming (CSE3011) |
LP (3 credits)
UNIT – 3 Python Example Programs
Object Oriented Programmming
By
Dr. MK Jayanthi Kannan, B.E.,M.E.,MS.,MBA.,M.Phil., Ph.D.,
Professor School of Computing Science and Engineering,
AB 104, 8.30am- 5.pm
VIT Bhopal University, Bhopal-Indore Highway,
Kothrikalan, Madhya Pradesh - 466114.
email: [email protected], Emp Id : 100600
Ph: +91 6379397594
Recap
• Object Orientation
– merge data and functions (that operate on the
data) together into classes
– class is like a blue print of an object
– objects are instances of a class
– typically two kinds of members in a class
• members that store data are attributes
• members that are functions are methods
Exmple 1: Class Coordinate
Keyword to indicate declaration of a class
Exmple 1: Class Coordinate
Name of a class
Exmple 1: Class Coordinate
Parent class
Exmple 1: Class Coordinate
special method
constructor
Exmple 1: Class Coordinate
method distance
Exmple 1: Class Coordinate
new object of type Coordinate
with initial attributes
Equivalent
Circle Class
class Circle(object):
Beginning of the class definition
def init (self, center, radius): self.center = The constructor. This is called when
center self.radius = radius
someone creates a new Circle, these
assignments create attributes.
def draw(self, canvas): rad = self.radius
x1 = self.center[0]-rad y1 = self.center[1]-rad x2 =
self.center[0]+rad y2 = self.center[1]+rad
A method that uses attributes to draw the
circle
canvas.create_oval(x1, y1, x2, y2, fill='green')
def move(self, x, y): self.center = [x, y]
A method that sets the center to a new
location and then redraws it
objects/CircleModule.py
Operator Overloading
What the operator does, depends on the objects
it operates on. For example:
>>> a = "Hello "; b = "World"
>>> a + b # concatenation
'Hello World'
>>> c = 10; d = 20
>>> c + d # addition
30
This is called operator overloading because the
operation is overloaded with more than one
meaning.
Exmple 2: Class Fraction
Methods: set and get
A well designed class provides methods to get and
set attributes.
• These methods define the interface to that class.
•This allows to perform error checking when values
are set, and to hide the implementation of the class
from the user.
Methods: set and get
Methods: set and get
Class Hierarchy
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Class Inheritance
Sometimes, we need classes that share certain (or very
many, or all) attributes but are slightly different.
• Example 1: Geometry
a point (in 2 dimensions) has an x and y attribute
a circle is a point with a radius
a cylinder is a circle with a height
• Example 2: People at universities
A person has an address.
A student is a person and selects modules.
A lecturer is a person with teaching duties.
•In these cases, we define a base class and derive other
classes from it.
• This is called inheritance.
Class Inheritance
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Class Inheritance
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Class Inheritance
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Class Inheritance: Another Example
Source:Computational Science and Engineering in Python by Hans Fangohr, Engineering and Environment, University of Southampton,
United Kingdom
Class Inheritance: Another Example
Source:Computational Science and Engineering in Python by Hans Fangohr, Engineering and Environment, University of Southampton,
United Kingdom
Class Inheritance: Another Example
Source:Computational Science and Engineering in Python by Hans Fangohr, Engineering and Environment, University of Southampton,
United Kingdom