Question:
Calculate area of a rectangle using classes
Program:
1 2 3 4 5 6 7 8 9 10 11 12 |
class rect(): def __init__(self,breadth,length): self.breadth=breadth self.length=length def area(self): return self.breadth*self.length a=int(input("Enter length of rectangle: ")) b=int(input("Enter breadth of rectangle: ")) obj=rect(a,b) # Creating an object 'obj' of class rect print "Area of rectangle:",obj.area() |
Explanation:
A class rect is created with two breadth and length, and method area which tabulates the area of the rectangle.
An object obj of class rect is created and used
Output:
1 2 3 |
Enter length of rectangle: 10 Enter breadth of rectangle: 20 Area of rectangle: 200 |