How can protected-members of the base class be made accessible from the class derived from it?

Protected members are accessible from the derived classes. #include <iostream> class B { protected: const int a = 3; }; class D : B { public: void foo() { std::cout << a << std::endl; } }; int main() { D d; d.foo(); } Output: 3. The protected member (a) of the base class (B) is accessible from the derived class (D).

2019 Feb 4, 8:48:20 AM

The question is a bit misleading IMO. Does this case falls under the problem's condition? class A { protected: const int a = 3; }; class B : A { void foo() { std::cout << a << std::endl; };

2019 Jan 11, 4:52:53 PM

@oleksiimld80 thanks!

2017 Dec 5, 3:41:41 PM

Missed word "using " in answer

2017 Nov 26, 11:46:20 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes