Access rights of C++ class members and encapsulation of classes

We used the public keyword many times when defining the class before, which means that the members of the class have "public" access rights, and we will explain it in detail in this section.

C++ controls the access rights of member variables and member functions through three keywords: public, protected, and private. They represent public, protected, and private respectively, and are called member access qualifiers . The so-called access rights are whether you can use the members of the class.

Note to Java and C# programmers that public, private, and protected in C++
can only modify members of classes, not classes, and classes in C++ are not public or private.

Inside the class (inside the code that defines the class), no matter whether the members are declared as public, protected or private, they can all access each other without restrictions on access rights.

Outside the class (outside the code that defines the class), members can only be accessed through objects, and only members of public attributes can be accessed through objects, and members of private and protected attributes cannot be accessed.

This section focuses on public and private, and protected will be explained in inheritance.

The following demonstrates the access rights of members through a Student class:

#include 

Guess you like

Origin blog.csdn.net/m0_68539124/article/details/129471803