Write a Program to get the average marks of six subjects using the Multiple Inheritance
Certainly! In Python, we can demonstrate multiple inheritance by creating a base class for subjects and a derived class to calculate the average marks. Below is an example of a Python program that uses multiple inheritance to compute the average marks of six subjects:
```python
# Base class for Subject
class Subject:
def __init__(self, name, marks):
self.name = name
self.marks = marks
# Another base class for Student
class Student:
def __init__(self, name):
self.name = name
self.subjects = []
def add_subject(self, subject):
self.subjects.append(subject)
def get_average_marks(self):
if not self.subjects:
return 0
total_marks = sum(subject.marks for subject in self.subjects)
return total_marks / len(self.subjects)
# Derived class for AcademicRecord which inherits from Student and Subject
class AcademicRecord(Student):
def display_average(self):
average_marks = self.get_average_marks()
print(f"Average marks for {self.name}: {average_marks:.2f}")
# Usage example
if __name__ == "__main__":
# Creating student
student_name = input("Enter the student's name: ")
student = AcademicRecord(student_name)
# Adding subjects and marks
for _ in range(6):
subject_name = input("Enter the subject name: ")
marks = float(input(f"Enter marks for {subject_name}: "))
subject = Subject(subject_name, marks)
student.add_subject(subject)
# Display average marks
student.display_average()
```
### Explanation:
1. **Subject Class**: Represents each subject with its name and marks.
2. **Student Class**: Contains the student's name and a list of subjects. It has methods to add subjects and calculate the average marks.
3. **AcademicRecord Class**: Inherits from `Student` and provides a method to display the average marks.
4. In the main program block, we create an instance of `AcademicRecord`, input the subject names and corresponding marks, and finally display the average.
### Running the program:
When you run this program, it will prompt you to input the student's name followed by the names and marks of six subjects. After all inputs are given, it will calculate and display the average marks.