Assignment6 Python S12 27
Assignment6 Python S12 27
Theory:
Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of
a common superclass, enabling flexibility and code reusability through
method overriding and method overloading.
Method Overriding:
Method overriding occurs when a subclass provides a specific
implementation of a method already defined in its parent class, maintaining
the same method signature while altering its behavior.
Method Overloading:
Method overloading allows multiple methods in the same class to share the
same name but differ in the number or type of parameters, enabling multiple
ways to perform similar actions within a class.
def get_marks(self):
print("\nMarks Data:")
for subject, mark in self.marks.items():
print(f"{subject}: {mark}")
def main():
student = Student()
while True:
more = int(input("\nEnter number of more subjects (0 to stop): "))
if more == 0:
student.get_marks()
break
if __name__ == "__main__":
main()
2. Operator Overloading:
class Pointer:
def __init__(self,x,y):
self.x=x
self.y=y
def __add__(self,other):
return Pointer(self.x+other.x, self.y+other.y)
def __str__(self):
return f"Point is ({self.x},{self.y})"
p1=Pointer(2,3)
p2=Pointer(3,4)
p3=p1+p2
p4=p1-p2
print(f"Point p1 is {p1}")
print(f"Point p2 is {p2}")
print(f"p1+p2 is {p3}")
print(f"p1-p2 is {p4}")
print(f"p1<p2: {p1<p2}")
print(f"p1>p2: {p1>p2}")
class Rectangle(Shape):
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth
def area(self):
print(f"Rectangle Area: {self.length * self.breadth}")
class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height
def area(self):
print(f"Triangle Area: {0.5 * self.base * self.height}")
def main():
while True:
print("Choose the shape to calculate the area\n1. Circle\n2. Rectangle\n3.
Triangle\n4. Exit")
choice = int(input("Enter choice: "))
if choice == 1:
radius = float(input("Enter radius: "))
Circle(radius).area()
elif choice == 2:
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
Rectangle(length, breadth).area()
elif choice == 3:
base = float(input("Enter base: "))
height = float(input("Enter height: "))
Triangle(base, height).area()
elif choice == 4:
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()
4.Method overriding class Employee:
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def show_details(self):
print(f"Employee: {self.name}, Salary: {self.salary}")
class Intern(Employee):
def __init__(self, name, salary, duration):
super().__init__(name, salary)
self.duration = duration
def show_details(self):
print(f"Intern: {self.name}, Salary: {self.salary}, Duration: {self.duration}
months")
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary)
self.team_size = team_size
def show_details(self):
print(f"Manager: {self.name}, Salary: {self.salary}, Team Size:
{self.team_size}")
class Engineer(Employee):
def __init__(self, name, salary, domain):
super().__init__(name, salary)
self.domain = domain
def show_details(self):
print(f"Engineer: {self.name}, Salary: {self.salary}, Department:
{self.domain}")
emp1.show_details()
emp2.show_details()
emp3.show_details()
OUTPUT: