Why base class object cannot call protected method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why base class object cannot call protected method

Hi, Why is it an object of the base class cannot call the protected method in this code: https://code.sololearn.com/c74t4H63mq96/#cpp I could understand if the object was from a derived class or unrelated class, but shouldn't a member object be able to call a member function?

2nd Jun 2017, 8:37 PM
Joseph P French
Joseph P French - avatar
2 Answers
+ 2
Not from outside of it's member class. The object may contain the protected properties but that's still only inside the scope of its own class/sub classes. int main is not inside the class, so when you create the object you can only access the public properties. Only derived classes or the class itself can access protected. Change it to public and it will work.
2nd Jun 2017, 8:47 PM
Rrestoring faith
Rrestoring faith - avatar
0
Ah, that makes sense - main() is not a member of the class. Thanks.
2nd Jun 2017, 8:56 PM
Joseph P French
Joseph P French - avatar