Inheritance python program to find the area of triangle, circle and rectangle.

7.a) By using the concept of inheritance write a python program to find the area of triangle, circle and rectangle.

# using concept of inheritance find area of circle ,triangle, rectangle

import math
class shape:
    def __init__(self):
        self.area=0
        self.name=""
    
    def showArea(self):
        print("The area of the",self.name,"is",self.area,"unit")

class Circle(shape):
    def __init__(self,radius):
        self.area=0
        self.name="circle"
        self.radius=radius
    def calcArea(self):
        self.area=math.pi*self.radius*self.radius

class Rectangle(shape):
    def __init__(self,length,breadth):
        self.area=0
        self.name="Rectangle"
        self.length=length
        self.breadth=breadth
    def calcArea(self):
        self.area=self.length*self.breadth

class Triangle(shape):
    def __init__(self,base,height):
        self.area=0
        self.name="Triangle"
        self.base=base
        self.height=height
    def calcArea(self):
        self.area=self.base*self.height/2

c1=Circle(5)
c1.calcArea()
c1.showArea()

r1=Rectangle(5,4)
r1.calcArea()
r1.showArea()

t1=Triangle(2,3)
t1.calcArea()
t1.showArea()

Leave a Reply

Your email address will not be published. Required fields are marked *