Program No 6 7 and 8
Program No 6 7 and 8
class Student:
# Constructor to initialize the object's attributes
def __init__(self, name, age, student_id):
self.name = name
self.age = age
self.student_id = student_id
def display_details(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
# Derived class
class Student(Person):
def __init__(self, name, age, student_id, major):
# Call the constructor of the base class
super().__init__(name, age)
self.student_id = student_id
self.major = major
def display_details(self):
# Call the base class method
super().display_details()
print(f"Student ID: {self.student_id}")
print(f"Major: {self.major}")
# Instantiate objects and demonstrate inheritance
print("Base Class Example:")
person = Person("Alice", 45)
person.display_details()
Program no 8:
Write a program to implement function overloading.
class Calculator:
def add(self, a=None, b=None, c=None):
if a is not None and b is not None and c is not None:
return a + b + c
elif a is not None and b is not None:
return a + b
elif a is not None:
return a
else:
return 0
Output:
class Calculator:
def add(self, a=None, b=None, c=None):
if a is not None and b is not None and c is not None:
return a + b + c
elif a is not None and b is not None:
return a + b
elif a is not None:
return a
else:
return 0