21CSL46 | PYTHON PROGRAMMING LABORATORY |
Subject Code:21CSL46
Subject :Python Programming Laboratory
Program-07
Aim: Demonstration of the concepts of classes, methods, objects and
inheritance
a) By using the concept of inheritance write a python program to find the
area of triangle, circle and rectangle.
PROGRAM
"""
Created on Wed May 24, 2023
@author: Hanumanthu
"""
import math
class Shape:
def __init__(self):
[Link] = 0
[Link] = ""
def showArea(self):
VTU 4TH SEM Page 1
21CSL46 | PYTHON PROGRAMMING LABORATORY |
print("The area of the", [Link], "is", [Link], "units")
class Circle(Shape):
def __init__(self, radius):
[Link] = 0
[Link] = "Circle"
[Link] = radius
def calcArea(self):
[Link] = [Link] * [Link] * [Link]
class Rectangle(Shape):
def __init__(self, length, breadth):
[Link] = 0
[Link] = "Rectangle"
[Link] = length
[Link] = breadth
def calcArea(self):
[Link] = [Link] * [Link]
class Triangle(Shape):
def __init__(self, base, height):
[Link] = 0
VTU 4TH SEM Page 2
21CSL46 | PYTHON PROGRAMMING LABORATORY |
[Link] = "Triangle"
[Link] = base
[Link] = height
def calcArea(self):
[Link] = [Link] * [Link] / 2
c1 = Circle(5)
[Link]()
[Link]()
r1 = Rectangle(5, 4)
[Link]()
[Link]()
t1 = Triangle(3, 4)
[Link]()
[Link]()
VTU 4TH SEM Page 3
21CSL46 | PYTHON PROGRAMMING LABORATORY |
OUTPUT
VTU 4TH SEM Page 4
21CSL46 | PYTHON PROGRAMMING LABORATORY |
b) Write a python program by creating a class called Employee to store the
details of Name, Employee_ID, Department and Salary, and implement
a method to update salary of employees belonging to a given
department.
Program
"""
Created on Wed May 24, 2023
@author: Hanumanthu
"""
class Employee:
def __init__(self):
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = 0
def getEmpDetails(self):
[Link] = input("Enter Employee name : ")
VTU 4TH SEM Page 5
21CSL46 | PYTHON PROGRAMMING LABORATORY |
[Link] = input("Enter Employee ID : ")
[Link] = input("Enter Employee Dept : ")
[Link] = int(input("Enter Employee Salary : "))
def showEmpDetails(self):
print("Employee Details")
print("Name : ", [Link])
print("ID : ", [Link])
print("Dept : ", [Link])
print("Salary : ", [Link])
def updtSalary(self):
[Link] = int(input("Enter new Salary : "))
print("Updated Salary", [Link])
e1 = Employee()
[Link]()
[Link]()
[Link]()
VTU 4TH SEM Page 6
21CSL46 | PYTHON PROGRAMMING LABORATORY |
Output
VTU 4TH SEM Page 7