PYTHON
Class and Object
Fill in the blanks
1.Class is Collections of Object.
2.Object is Collections of data.
3.Creating an object is called Constructor
4.Deleting an object is called destructor
5.Write a different kinds of attribute 2 (data attribute,class attribute)
6.Private data can be accessed by public member function
7.private data can be declared with __(underscore)
8.__init__ means Constructor
9.__del__ means destructor
10.write a syntax of object objectvariable=classname()
Answer the following Question
(4 marks)
1.Write a program for Class and Object
class Test:
# A
sample method
def
fun(self):
print("Hello
world")
# creating object to a class test
obj=Test()
#Executing the class method
obj.fun()
2.Write a program for Constructor and Destructor
class sample:
def
__init__(self,x,y):
self.x=x
self.y=y
def
__del__(self):
print("An
object removed from memory")
def
disp_data(self):
print("x
value-{},y value-{}".format(self.x,self.y))
s1=sample(2,4)
s2=sample(4,5)
s3=sample(10,12)
s1.disp_data()
s2.disp_data()
s3.disp_data()
#del command remove one reference and
invoke destructor method _ del _ () automatically
del s1
del s2
del s3
3.Write a program for Constructor Overloading
class jknum:
def
__init__(self,x=1,y=0):
self.cx=x
self.cy=y
def callme(self):
print("x
value=%d, y value=%d"%(self.cx,self.cy))
b1=jknum(10)
b2=jknum(20,30)
b1.callme()
b2.callme()
4.Write a program for Class Attribute
class classattrib:
num=10
def count_fun(self):
self.__class__.num+=1
N1=classattrib()
N2=classattrib()
print("\n num
value...",N1.num)
print("\n num
value...",N2.num)
print("\n num
value...",N1.__class__.num)
print("\n num
value...",N2.__class__.num)
print("\n num value...",
classattrib.num)
N1.count_fun()
N2.count_fun()
print("\n num
value...",N1.num)
print("\n num
value..",N2.num)
print("\n num
value...",N1.__class__.num)
print("\n num
value...",N2.__class__.num)
print("\n num
value...",classattrib.num)
5.Write a program for Private method and Private Variable
class number:
_a=0
_b=0
c=0
def _callme(self,x,y):
self._a=x
self._a=y
def cal(self):
s=self._a+self._b+self.c
return(s)
def maincall(self,x,y):
self._callme(x,y)
obj=number()
obj.c=40
obj.maincall(10,20)
print("sum value = %d "
%obj.cal())
6.Write a program for data hiding and Object printing
class MyClass:
#Hidden
member of My Class
_Hidvar
= 0
#A
member method that changes
#_Hidvar
def
add(self,N):
self._Hidvar
=self._Hidvar + N
print(self._Hidvar)
#Driver code
myObject = MyClass()
myObject.add(2)
myObject.add(5)
print(myObject._Hidvar)
Answer the following Question
(10 marks)
1.Write a program for ATM Machine Operation using Class and Object.
v Deposit
v Withdraw
v Balance
v Exit
class BankAccount:
balance = 0
def
withdraw(self,amount):
self.balance-=amount
return
self.balance
def
deposit(self,amount):
self.balance
+= amount
return
self.balance
def
balanceCheck(self):
print("\n New
balance is...Rs.{}".format(self.balance))
def
bal_return(self):
return(self.balance)
BT=BankAccount()
while True:
print("1.
Deposit\n")
print("2.
Withdraw\n")
print("3.
Balance\n")
print("4.
Exit")
ch =
int(input("Enter your choice..."))
if ch == 1:
while
True:
amt=int(input("Enter
amount to Deposit..."))
if
amt>0:
BT.deposit(amt)
break
else:
print("Enter
Valid mount")
continue
if ch == 2:
while
True:
amt=int(input("Enter
amount to withdraw..."))
if
BT.bal_return()>amt:
BT.withdraw(amt)
break
else:
BT.balanceCheck()
print("\n Low
balance.please Enter Less Amount..")
continue
if ch ==3:
BT.balanceCheck()
if ch ==4:
break
No comments:
Post a Comment