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 understand the inheritance of C++ class

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

Share

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

This article mainly explains "how to understand the inheritance of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to understand the inheritance of C++.

When the public members of the base class are derived from public, they can be accessed not only by the members of the derived class object, but also by external functions; while when the private is derived, the public members of the C++ base class allow access to the members of the derived class object, but not external access.

However, when the next level is derived, since all members of the C++ base class have been privatized, members of other classes are no longer accessible. The way to implement access to only object members with derived consanguinity is to use a member with another access property, the protected member, in the base class.

A protected member is a kinship in which there are other members inside and outside. For derived objects, it is a public member and can be accessed; for consanguinity, it is hidden as well as private members, except that private and public are allowed to be derived. C++ also allows the use of protected derivation. Now the changes of the access properties of the three members with different access attributes in the derived class after three derivations are summarized in the following table, which is an improvement and supplement to the previous table.

The access attribute changes caused by the derivation of the public member of the base class, the protected member of the base class, the private member derivation of the base class, the private derivation becomes the private member, the non-private member of the base class becomes invisible, the non-private member of the base class becomes the private member of the derived class, the protected derivative becomes the protected member, the non-private member of the private member invisible base class, the access property of the non-private member in the derived class is reduced to one level, the public derivation is still public. The member is still an invisible member of the protected member. The access property of the non-private member of the base class in the derived class remains unchanged.

It is important to note that the private members of the C++ base class are not visible in the derived class, regardless of their derivation.

(1) friend function

Usually, the private members of the class can only be accessed by the members of the class, the external functions can only access the member functions of the class, and then the private members of the class can be accessed by the member functions. However, if an external function (perhaps a member of another class) is declared with friend in a class definition, the external function can exceptionally access any private member of the class. The external function declared with friend is called the friend function of this class.

When a friend function is a member function of another class, you should pay attention to the following:

A: when a friend function is a member function of a class, it should not only be declared in the class definition in which it is located, but also declare its friend relationship in another class. The format of the declaration statement is:

Friend function type function name:: function name (parameter list)

B: the friend function does not need the reference parameter of the object of this class when referencing the private member of the object of this class, but it must have the reference parameter of the object of the friend metaclass when referencing the private member of the class of which it is the friend.

C: when a member function of one class acts as a friend function of another class, it must be defined first, not just declared.

By using the friend function to access the private members of the object directly, you can avoid the overhead of calling the member functions of the class again. At the same time, the friend function as an interface of the class, for the designed class, as long as we add a declaration statement, we can use the external function to supplement its function, or build a bridge between different kinds of objects. However, it also destroys object encapsulation and information hiding, so you need to be careful when using it.

(2) friend class

You can also declare a class rather than just a function as a friend of another C++ base class. At this point, you only need to declare it first and not necessarily define it first. It should be noted that friends are one-way and valid only between two classes. Even if class X is a friend of class Y.

Whether class Y is a friend of class X depends on whether there is a corresponding declaration in class X. That is, friendly relations are not exchangeable. If class X is a friend of class Y and class Y is a friend of class Z, it does not necessarily mean that class X is a friend of class Z, that is, the friend relationship is not transitive. It is useful to make a class a friend of another class when it is about to work with another class. At this time, each member function of the friend class becomes the friend function of the other party.

Thank you for your reading, the above is the content of "how to understand the inheritance of C++". After the study of this article, I believe you have a deeper understanding of how to understand the inheritance of C++. The specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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