How to use the C++ control keyword protected
This article mainly introduces "how to use the C++ control keyword protected". In daily operation, I believe many people have doubts about how to use the C++ control keyword protected. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubts about "how to use the C++ control keyword protected". Next, please follow the editor to study!
The keyword protected is similar to private in that you can only use public class members to access protected members outside the class. Only when derived can the private and protected keywords reflect the difference. Members of the derived class can directly access the protected members of the base class, but not the private members of the base class. So for external classes, protected is similar to private, while for derived classes, protected is similar to public.
For example, the Human class sets the name member to protected:
Class Human {protected: string name;...}
In this case, its derived class Hero can access name directly without using the methods in Human:
Class Hero: public Human {void show () {cout