Python program to display student’s information using multiple inheritances

Here’s an example Python program that demonstrates how to use multiple inheritances to display student information:

# Python program to display student's information using multiple inheritances

# define a class for student information
class StudentInfo:
    def __init__(self, name, rollno):
        self.name = name
        self.rollno = rollno

    def display_info(self):
        print("Name:", self.name)
        print("Roll No:", self.rollno)

# define a class for student marks
class StudentMarks:
    def __init__(self, marks):
        self.marks = marks

    def display_marks(self):
        print("Marks:", self.marks)

# define a class that inherits from both StudentInfo and StudentMarks
class Student(StudentInfo, StudentMarks):
    def __init__(self, name, rollno, marks):
        # call the constructors of both parent classes
        StudentInfo.__init__(self, name, rollno)
        StudentMarks.__init__(self, marks)

    def display_student(self):
        # call the display methods of both parent classes
        self.display_info()
        self.display_marks()

# create a new student object and display their information
student = Student("Alice", 1234, [85, 90, 95])
student.display_student()

In this program, we define three classes: StudentInfo, StudentMarks, and Student. StudentInfo contains information about the student’s name and roll number, StudentMarks contains information about the student’s marks, and Student inherits from both StudentInfo and StudentMarks.

Student‘s constructor calls the constructors of both parent classes, passing in the relevant parameters. Student‘s display_student method calls the display_info and display_marks methods of both parent classes to display the student’s information and marks.

Finally, we create a new Student object and call its display_student method to display the student’s information and marks. The output of the program will be:

 

Name: Alice
Roll No: 1234
Marks: [85, 90, 95]

Comments

Leave a Reply

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