15. The concept of class and encapsulation

Encapsulation: Not every property of a class is exposed to the outside world. The properties of some classes are exposed to the outside world. The exposure level of properties and behaviors must be defined in the class's notation.

In C++, the encapsulation mechanism is implemented by defining the access level of class members.

Member variables: variables used to represent class attributes

Member functions: functions used to represent the behavior of the class

public: Member variables and member functions can be accessed and called both inside and outside the class.

private: member variables and member functions can only be accessed and called within the class.

The scope of class members is only inside the class and cannot be directly accessed from outside;

Member functions can directly access member functions and call member functions. The outside of the class can access public members through class variables, and the scope of class members has nothing to do with the access level.

All members of a class defined with struct in C++ are public by default.

Note: If you want to access a member variable or member function of a class, you must pass the class object. But whether the access is successful depends on the access level of member functions and member variables.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325885797&siteId=291194637