If a base class has a protected member, is it also a member of the derived class?
Yes. In your example, it becomes a protected member of the derived class.
"Within the class, the keyword protected has exactly the same effect as the keyword private...The difference between protected and private members only becomes apparent in a derived class. Members of a base class that are declared as protected are freely accessible in the function members of a derived class, whereas the private members of the base class are not."(Ivor Horton's Beginning C++, p 588)
I keep reading and reading, somehow it is still a mystery to me.
Try experimenting. Here is an example:
Code:
#include <iostream>
using namespace std;
class Base
{
public:
Base(int n1, int n2, int n3)
{
publicNum = n1;
protectedNum = n2;
privateNum = n3;
}
public:
int publicNum;
protected:
int protectedNum;
private:
int privateNum;
};
/**********************/
class Derived : public Base
{
public:
Derived(int num1, int num2, int num3): Base(num1, num2, num3)
{
}
void showPublic()
{
cout<<publicNum<<endl;
}
void showProtected()
{
cout<<protectedNum<<endl;
}
void showPrivate()
{
cout<<privateNum<<endl; //error
/*error C2248: 'privateNum' : cannot access private member declared in class 'Base'*/
}
};
/*************************/
int main ()
{
Derived d(1, 2, 3);
d.showPublic();
d.showProtected();
return 0;
}
Now trying adding this function to the public section of Base:
Code:
void showAll()
{
cout<<publicNum<<" "<<protectedNum<<" "<<privateNum<<endl;
}
and add this code to the end of main():
Code:
cout<<endl;
d.showAll();
My book says that all members of the base class are inherited--it's just a question of whether they are accessible or not; and private members in the base class are not accessible by members of the derived class.