Home >>Cpp Programs >C++ protected keyword
The C++ protected keyword generally specifies the access to a class members in the member-list up to the next access specifier that can be public or private or the end of the class definition in the C++ programming language. Please note that the CPP protected class members that are declared as protected can only be used by the following elements that are depicted below:
The C++ protected keyword generally specifies that the public and the protected members of the base class are generally the protected members of its derived classes, this happens in the case where preceding is done by the name of a base class.
Please note that the protected members are not as private as the private members that are basically accessible only to the members of the class in which they have been declared but doing this doesn’t make as public as the public members that are generally accessible in any of the function.
Protected members that are basically known to be accessible to any of the friend or member function that are of a derived class in C++ programming language and they are also declared as static. Protected members that are basically known to be not getting declared as the static that are basically being accessible to the friends and member functions in a derived class that too only through a reference to, pointer to, or object of the derived class.
Syntax
protected: [member-list] protected base-class
Here is an example of the C++ protected keyword that will help you understand the basic concept of it and get you the physical aspect of its use:
#includeusing namespace std; class Base { protected : int x = 10; }; class Child : public Base { public : void func() { cout << "value of x = " << x; } }; int main() { Child obj; obj.func(); return 0; }