Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Advantages, disadvantages and usage introduction of multiple inheritance in C language and C++

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report