L10 ClassesAndObjects
L10 ClassesAndObjects
and Practise
L10 - Classes and Objects
• Class contains data and methods that can access or manipulate this data.
• Data is typically accessed through the methods (data protection).
• The methods are accessed through ‘object’ instantiation.
• Broadly comes under object-oriented programming (others being functional,
structural programming).
def printout(self):
print(self.name, self.rollno, self.sem)
class StudentDetail:
def printout(self):
print(self.name, self.rollno, self.sem)
def printout(self):
print(self.name, self.rollno, self.sem)
def printout(self):
print(self.name, self.rollno, self.sem)
#Constructor
def __init__(self, n='R', r=1, s=1):
self._name = n
self._rollno = r
self._sem = s
#destructor
def __del__(self):
print('Del obj' + str(self))
s1 = StudentDetail()
s1.printout()
s1 = StudentDetail('Ram')
s1.printout()
s1 = StudentDetail('Raman', 23)
s1.printout()
s1 = StudentDetail('Ramana', 23, 5)
s1.printout()
Class variables and methods
L10_cmv.py
• a + b already defined
• Operator overloading is done for user defined classes, for e.g. class Complex.
• def add_comp(self, other):
• c1.add_comp(c2)
• def __add__(self, other)
• c1 + c2 #(More intuitive usage)
• __sub__
• __mul__
• ………… (find out the list of operators that can be overloaded)
class Passbook:
pass
p1 = Passbook( )
p1._name = ‘Raman’
p1._number = 1234