Looking at this, I keep thinking with any class you implement using protected data, and when you create a base class (or any other class for that matter, you also create member functions within them. So I can't really make sense of what it's asking... Then it says below the question... Basically u use protected members when you want that immidiately derived class should access the private data members in some way "Hint: Perhaps you gain efficiency by one method, but lose information hiding."
-Private members of the base class are not accessible in the derived class (to preserve encapsulation)
-Sometimes, however, we would like to be able to define encapsulated data members which are not publicly accessible, but which are accessible to derived classes:
class A
{
public:
A():x(0),y(0){}
void IncrVal()
{
++x;
++y;
}
private:
int x;
int y;
};
class B : public A
{
public:
B():z(0){}
void change()
{
++x; // ACCESS ERROR!
}
private:
int z;
}; -So C++ provides a third access specifier: protected:
- "Protected" members are not accessible from outside the class, except in derived classes.
-So a protected member which is inherited, is accessible in the derived class (and remains protected there)