Advantages, disadvantages and usage introduction of multiple inheritance in C language and C++
This article mainly explains the advantages, disadvantages and usage of multiple inheritance in C language and C++. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the advantages, disadvantages and usage of multiple inheritance in C language and C++.
Catalogue
Overview
Advantages and disadvantages
Advantages
Shortcoming
Declare the method of multiple inheritance
Format
Examples
Ambiguity
The two base classes have members of the same name
The base class and derived class have members of the same name
Two base classes derive from the same base class
Overview
Multiple inheritance (multiple inheritance): a derived class has two or more base classes that inherit required properties from two or more base classes. To adapt to this situation, C++ allows a derived class to inherit multiple base classes at the same time. This kind of behavior is called multiple inheritance.
Advantages and disadvantages
It naturally extends the single inheritance.
Can inherit the functions of multiple classes
Shortcoming
Structural complication
Priority ambiguity
Functional conflict
Declare the method format of multiple inheritance
Format of multiple inheritance:
Derived class constructor name (total form parameter table list): base class 1 constructor (actual parameter table list), base class 2 constructor (actual parameter table list), base class 3 constructor (actual parameter table list) {example of newly added members in a derived class according to member initialization statements}
Teacher class:
# ifndef PROJECT5_TEACHER_H#define PROJECT5_TEACHER_H#include using namespace std;class Teacher {protected: string name; int age; string title;public: Teacher (string n, int a, string t); void display_teacher ();}; # endif / / PROJECT5_TEACHER_H
Teacher.cpp:
# include # include "Teacher.h" using namespace std;Teacher::Teacher (string n, int a, string t): name (n), age (a), title (t) {} void Teacher::display_teacher () {cout