Unit 3
'''
Write a program to create a Student class with name, age and marks as data members.
Also create a method named display() to view the student details. Create an object to
Student class and call the method using the object
'''
class Stud:
def __init__(self,name,age,mark): # this is Constructor double(__) define special function
self.name = name
self.age = age
self.mark = mark
def disp(self):
print(self.name)
print(self.age)
print(self.mark)
s = Stud('vikas',19,49)
s.disp()
'''
Write a program to create a Student class with name, age and marks as data members.
Also create a method named display() to view the student details. Create an object to
Student class and call the method using the object
'''
class Stud:
def __init__(self,name,age,mark): # this is Constructor double(__) define special function
self.name = name
self.age = age
self.mark = mark
def disp(self):
print(self.name)
print(self.age)
print(self.mark)
s = Stud('vikas',19,49)
s.disp()
'''
Write a program to create a static method that counts the number of instances
created for a class.
'''
class stud:
a=0
def __init__(self):
stud.a += 1
@staticmethod
def disp():
print(stud.a)
s = stud()
s1 = stud()
s1.disp()
Create a Student class to with the methods set_id, get_id, set_name, get_name, set_marks and get_marks where the method name starting with set are used to assign the values and method name starting with get are returning the values. Save the program by student.py. Create another program to use the Student class which is already available in student.py.
class stud:
def setid(self,id):
self.id = id
def getid(self):
return self.id
def setname(self,name):
self.name = name
def getname(self):
return self.name
---------------------------
from stud9 import stud
s = stud()
s.setid(1)
s.setname('vikas')
print(s.getid())
print(s.getname())
Write a program to access the base class constructor from a sub class by using super() method and also without using super() method.
class father:
def __init__(self,no):
self.no = no
def disp(self):
print(self.no)
class son(father):
def __init__(self,no,name):
super().__init__(no)
self.name= name
def disp(self):
super().disp()
print(self.name)
s = son(8,'vikas')
s.disp()
Write a program to implement single inheritance in which two sub classes are derived from a single base class.
class Bank():
cash=10000
@classmethod
def total(cls):
print("Bank total -> ",cls.cash)
class State(Bank):
cash = 20000
@classmethod
def total(cls):
print("State total -> ",cls.cash)
class Adc(Bank):
cash = 30000
@classmethod
def total(cls):
print("Total -> ",Bank.cash + cls.cash)
s = State()
s.total()
a = Adc()
a.total()
Write a program to override the super class method in subclass.
class Squares():
def fun(self,a,b):
self.a = a
self.b = b
def disp(self):
print("Square -> ",self.a * self.a)
class Rect(Squares):
def fun(self,a,b):
self.a = a
self.b = b
#super().fun(a,b)
def disp(self):
print("Rectangle -> ",self.a * self.b)
#super().disp()
r = Rect()
r.fun(5,3)
r.disp()

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home