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

What is the function of C++ base class

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

Share

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

This article mainly explains "what is the role of C++ base class", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the role of the C++ base class"?

Private inheritance: public members in the base class are private in the derived class; protected members in the C++ base class are private in the derived class; and private members in the base class are not visible in the derived class, only the base class is visible. Protect inheritance: public members in the base class are protected in the derived class

Protected members in the C++ base class are protected in the derived class; private members in the base class are not visible in the derived class, only the C++ base class is visible. Through the inheritance mechanism, the derived class inherits the members of the base class, and the inherited members only belong to the derived class. That is, it is important that the base class and the derived class each have one such member. Next, let's not panic about inheritance, let's go back to the class: (personal understanding of the so-called visibility is to invoke permissions! )

Next, instead of looking at inheritance, let's go back to the class:

# include "iostream" using namespace std; class A {public: void dump () const {....} protected: void get_x () {Xerox;} private: int x;}; class B:public A {public: void get () {get_x ();} / * void get_y () {yardx;} * / void compare (A & a) {a.get_x ();} protected: void set () {...} private: int y }; int main () {

So we know that members that are not visible in derived classes are not inaccessible and can be accessed indirectly! Summarize the above and make a table: (note: the visibility of members in derived classes refers to inherited and self-added members.

Members

C++ base class (class)

Derived class

Main

Private data

Visible

Invisible

Invisible

Private function

Visible

Invisible

Invisible

Protect data

Visible

Visible

Invisible

Protection function

Visible

Visible

Invisible

Public data

Visible

Visible

Visible

Public function

Visible

Visible

Visible

At this point, I believe that everyone on the "C++ base class what the role of" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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