When it is required to find the area of a rectangle using classes, object oriented method is used. Here, a class is defined, attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to find the area of rectangle.
Below is a demonstration for the same −
class shape_rectangle(): def __init__(self,my_length, my_breadth): self.length = my_length self.breadth = my_breadth def calculate_area(self): return self.length*self.breadth len_val = 6 bread_val = 45 print("The length of the rectangle is : ") print(len_val) print("The breadth of the rectangle is : ") print(bread_val) my_instance = shape_rectangle(len_val,bread_val) print("The area of the rectangle : ") print(my_instance.calculate_area()) print()
The length of the rectangle is : 6 The breadth of the rectangle is : 45 The area of the rectangle : 270