When does multiple inheritance need to be used?
As long as the problem you encounter cannot be described by a single "one" relationship, it is the time to inherit more.
For example, there are teachers and students in school. They are all Person s. We can use the grammar of "teacher is human" and "student is human" to describe this situation.
From the perspective of object-oriented programming, we should create a base class named Person and two subclasses named Teacher and Student, which are inherited from the former.
The question arises: Some students also teach classes to earn money (assistants). What should they do? In this way, there exists a complex relationship between teachers and students, that is, there are two "one" relationships at the same time.
We need to write a TeachingStudent class to inherit both Teacher and Student classes, in other words, we need to use multiple inheritance.
Basic grammar:
class TeachingStudent: public Student,public Teacher
{....}
Example:
Requirement: Create a class hierarchy composed of Person, Teacher, Student and Teaching Student.
#include <iostream> #include <string> using namespace std; class Person { public: Person(string theName); void introduce(); protected: string name; }; class Teacher:public Person { public: Teacher(string theName, string theClass); void teach(); void introduce(); protected: string classes; }; class Student:public Person { public: Student(string theName, string theClass); void attendClass(); void introduce(); protected: string classes; }; class TeachingStudent:public Student, public Teacher { public: TeachingStudent(string theName, string classTeaching, string classAttending); void introduce(); }; Person::Person(string theName) { name = theName; } void Person::introduce() { cout << "Hello everyone,I am" << name << endl; } Teacher::Teacher(string theName, string theClass):Person(theName) { classes = theClass; } void Teacher::teach() { cout << name << "teach" << classes << endl; } void Teacher::introduce() { cout << "Hello everyone,I am" << name << ",I teach" << classes << endl; } Student::Student(string theName, string theClass):Person(theName) { classes = theClass; } void Student::attendClass() { cout << name << "join" << classes << "Study.\n"; } void Student::introduce() { cout << "Hello everyone,I am" << name << ",I am here" << classes << "Study.\n"; } TeachingStudent::TeachingStudent(string theName, string classTeaching, string classAttending) :Teacher(theName, classTeaching), Student(theName, classAttending) { } void TeachingStudent::introduce() { cout << "Hello everyone,I am" << Student::name << ",I teach" << Teacher::classes << ","; cout << "Meanwhile, I am" << Student::classes << "Study.\n"; } int main() { Teacher teacher("Little Turtle", "C++Sevillanas"); Student student("Lost lamb", "C++Sevillanas"); TeachingStudent teachingStudent("Tintin", "C++Sevillanas", "C++Advanced class"); teacher.introduce(); teacher.teach(); student.introduce(); student.attendClass(); teachingStudent.introduce(); teachingStudent.teach(); teachingStudent.attendClass(); return 0; }