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

How to talk about the C++ base class in C++

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

Share

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

This article introduces you how to talk about the C++ base class in C++, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

There is an important feature in the C++ language: the Private derivation ensures that the methods in the C++ base class can only be used indirectly by the methods of the objects of the derived class, and not externally, unless these methods have been redefined, which is explained in more detail below.

Inheritance is a process through which an object can obtain the properties (including functions) of another object and add some characteristics of its own to it. As an important mechanism of C++ language, inheritance method can be used to automatically provide operations and data structures from another class to one class.

In turn, programmers can quickly build a new class on the basis of a general class without having to design each class from scratch. When a class is inherited by another class, the inherited class is called the base class, also known as the parent class. A class that inherits other class properties is called a derived class, also known as a subclass.

In general, an inherited process originates from the definition of a C++ base class, which defines the public properties of all its derived classes. In essence, the base class has common properties in the same class collection, and the derived class inherits these attributes and adds its own unique properties. The essence of inheriting from any existing class is to build new derived classes.

Inheritance derived from a base class is called single inheritance, in other words, a derived class has only one direct C++ base class. The common format of a single inheritance declaration statement is:

Class derived class name: access control keyword base class name {data member and member function declaration}

In contrast, inheritance derived from multiple base classes is called multiple inheritance or multiple inheritance, that is, a derived class has multiple direct base classes. In some object-oriented languages (such as Java), multiple inheritance between classes is not supported, but only single inheritance is supported, that is, a class can only have at most one direct parent class, so other mechanisms such as interfaces are needed to achieve similar functions. The syntax support of multiple inheritance is provided in C++, which makes the problem much easier. The common format of multiple inheritance declaration statements is:

Class derived class name: access control keyword base class name 1, access control keyword base class name 2. {data members and member function declarations}

In addition to multiple inheritance, there is another way for a derived class to inherit multiple base classes, which is to use the derived class as the base class to provide other class inheritance again, resulting in a multi-level inheritance relationship. For example, class A derived class B, class B derived class C, then class An is said to be the direct base class of class B, class B is the direct base class of class C, and class An is the indirect base class of class C.

The hierarchy of a class is also called an inheritance chain. In the above example, when an object of class C is created, the constructor of class An is called, followed by the constructor of class B, and * is the constructor of class C. Destructors are called in the opposite order. When a derived class inherits a hierarchical class, each derived class on the inheritance chain must pass the variables it needs to its base class.

In the inheritance declaration statement, the access control keyword is used to indicate the extent to which members and member functions declared in the base class definition can be accessed by derived classes. The access control keyword can be public, private or protected. If the access control keyword is public.

It is said that the derived class inherits publicly from the C++ base class, also known as public derivation. If the access control keyword is private, the derived class is said to inherit privately from the C++ base class, also known as private derived. Now the author lists the specific differences between public inheritance and private inheritance as follows.

From the above table, we can summarize the characteristics of the two derivatives as follows:

Base class member base class private member base class public member

Derivation mode

Privatepublicprivatepublic derived class members invisible invisible external functions visible about how to tell the C++ base class in C++ to share here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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