Sponsor Area
Classes in Python
Define a class RING in Python with following specifications
Instance Attributes
- RingID # Numeric value with a default value 101
- Radius # Numeric value with a default value 10
- Area # Numeric value
Methods:
- AreaCal() # Method to calculate Area as
# 3.14*Radius*Radius
- NewRing() # Method to allow user to enter values of
# RingID and Radius. It should also
# Call AreaCal Method
- ViewRing() # Method to display all the Attributes 4
class RING: # OR class RING( ): OR class RING(Object):
def __init__(self):
self.RingID=101
self.Radius=10
self.Area=0
def AreaCal(self):
self.Area=3.14*self.Radius*self.Radius
def NewRing(self):
self.RingID=input('Enter RingID')
self.Radius=input('Enter radius')
self.AreaCal() # OR AreaCal(self)
def ViewRing(self):
print self.RingID
print self.Radius
print self.Area
Differentiate between static and dynamic binding in Python? Give the suitable example of each.
|
|
Static Binding |
Dynamic Binding |
|
Time of binding |
happens during compilation |
binding happens at run time |
|
Actual object |
Actual object is not used for binding |
Actual object is used for binding |
|
Example |
Method overloading |
Method overriding |
|
Methods of binding |
Private, static and final methods show static binding. Because they cannot be overridden. |
Other than private, static and final methods show dynamic binding. Because they can be overridden. |
|
Class |
Type class |
Object class |
Write two methods in Python using the concept of Function Overloading (Polymorphism) to perform the following operations:
(i) A function having one argument as side, to calculate Area of Square as side*side
(ii) A function having two arguments as Length and Breadth, to calculate Area of Rectangle as Length*Breadth.
def Area(side):
print side*side
def Area(length,breadth):
print length*breadth
If you run the code, the second Area(B, H) definition will overwrite/override the first one because python does not support function overloading “ as illustrated in the example shown above ”.
class Exam:
Regno=1
Marks=75
def __init__(self,r,m): #function 1
self.Regno=r
self.Marks=m
def Assign(self,r,m): #function 2
Regno = r
Marks = m
def Check(self): #function 3
print self.Regno, self.Marks
print Regno, Marks
class Exam:
Regno=1
Marks=75
def __init__(self,r,m): #function 1
self.Regno=r
self.Marks=m
def Assign(self,r,m): #function 2
Regno = r
Marks = m
def Check(self): #function 3
print self.Regno, self.Marks
print Regno, Marks(i) In the above class definition, both the functions - function 1 as well as function 2 have similar definition. How are they different in execution?
(ii) Write statements to execute function 1 and function 2.
(i) Function 1 is the constructor which gets executed automatically as soon as the object of the class is created. Function 2 is a member function which has to be called to assign the values to Regno and Marks.
(ii) Function 1 E1=Exam(1,95) # Any values in the parameter
Function 2 E1.Assign(1,95) # Any values in the parameter
Sponsor Area
Mock Test Series
Mock Test Series



