Explain why and when do we use protected instead of private.- Private data members cannot be accessed outside the class.
- When a class inherits a base class, all the data members except the private get inherited into it. So if we want data members to be accessible to only derived classes and not privately or publicly accessible, then we can use protected.
- Protected is similar to private.
- It makes class member inaccessible outside the class, but the members can be accessed by any subclass of that class.
|