Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the function of C++ base class

Shulou Source: shulou.com Published: 2022-06-02 09:43:04 09月30日 Update

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!

Tags: Members protected private functional functional data next content learning practical deeper important personal interest can pass practical practical easy to operate way Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Docker Linux Redmi