The following describes the access control technology in C ++. Access Control in C ++ falls into two categories: private, public, and protected. The second is: the method attributes are changed after inheritance. For more information, see...
1. Access scope of private, public, and protected:
Private: It can only be accessed by functions in the class and their friends functions. It cannot be accessed by any function of other classes and their subclasses, nor can the objects in the class be accessed. protected: it can be accessed by functions in the class, sub-class functions, and their friends functions, but cannot be accessed by objects in the class public: it can be accessed by functions in the class, sub-class functions, and their friends functions, or by the objects in the class. Note: Friends functions include two types: global functions set as friends functions, set as a member function in the youyuan class.
Second, method property changes after class inheritance:
Use private inheritance. All methods of the parent class become private in the subclass. Use protected inheritance. The protected and public methods of the parent class become protected in the subclass. The private method remains unchanged. Use public inheritance, the method attributes in the parent class are not changed;
- How beginners learn C ++
- Explore the secrets in C ++
- How to better learn C ++
- Introduction to C ++ Design
- What does C ++ bring to us?
Horizontal access: declares an object of a certain type and accesses its member functions and data members. Generally, only the public area does not include the protected area !) Member functions and data members can be accessed horizontally. Vertical access: A class is derived from a base class. The derived class accesses the member functions and data members of the base class. Generally, only the member functions and data members in the public and protected areas can be vertically accessed.
Again, the subject of the access behavior that can be provided is "function ". There is no access restriction on the class body, that is, the private function can access the public/protected/private member function or data member. Similarly, the protected function, the public function can also access the public inheritance defined in the class body. The public and protected members in the base class inherit from the public and protected member functions or data members of the subclass ), then the access is still unrestricted within the class.
Access to member functions within the scope of a category is not restricted. In the case of inheritance, the base class private member still inherits from the derived class, but it cannot be accessed directly, even in the class, not to mention the class object.
In fact, Bjarne has been promoting this issue for ten years. As early as 99 years ago, Bjarne wrote "Learning C ++ as A New Language". In several technical interviews, here, here, and here) this section describes how to correctly treat and use various abstract mechanisms supported by Access Control in C ++. Andrew Koenig also wrote a modern C ++ tutorial "Accelerated C ++" which will be mentioned later ). But has the C ++ community improved over the years? As far as I know, improvements are very small.
The learners still blindly drill down the language details, only to see the trees not see the forest; there are still various "technical" articles and unreliable "learning C ++'s XX suggestions" on the Internet "; some industry-specific experts are still writing skills and corners that ordinary programmers don't need in their eight lives );
Companies that really use C ++ in the industry always ask some detailed questions during the interview, rather than examining the basic literacy of programming, mastering all the language details cannot make you a qualified programmer ). This interview philosophy is wrong. It is estimated that the reasoning behind this interview should be "if this guy doesn't know the details, then he may not be familiar with the language;
But if he knows, although he may not be a good programmer, we can still further test the next problem. "The problem with this philosophy is, if you are familiar with the language to a certain extent, we will provide specific suggestions later.) You can program well. For the rest, you only need to check the documentation );
However, many companies are far away from testing their "familiarity with the language". For example, it is understandable to ask about the life cycle and structure sequence of temporary objects, but how can I avoid a class being copied or how can I avoid it being built on a stack ?); Of course, some language knowledge must be mastered in advance. Which of the following statements will be mentioned later? During the interview, the key is not to ask about the language details ".