Protected data class members c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Protected data class members c++

Can the members of the protected class print out in the terminal? I’m quite confused how they work or how to access them

14th Mar 2019, 4:24 AM
lakas
2 Answers
+ 6
Class members with protected access specifiers are accessible by derived classes. class Base { private: int a {1}; protected: int b {2}; }; class Derived : public Base { public: void test() { std::cout << a; // error std::cout << b; // OK } }; int main() { Derived obj; obj.test(); }
14th Mar 2019, 4:45 AM
Fermi
Fermi - avatar
+ 5
Yes they are accessible in the class and immediate subclasses.
14th Mar 2019, 10:36 AM
Sonic
Sonic - avatar