Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the C++ control keyword protected

Shulou Source: shulou.com Published: 2022-06-02 04:16:46 09月29日 Update

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

Tags: Keywords keywords members permissions permissions controls methods learning situations data time more help design practical special similar next code practices Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Linux MariaDB Redmi