My beginning C++ book has a list of the different access levels with inheritance, so I assume most do. There are 9 different combinations:
Code:
class DerivedName : public BaseName
base class: derived class:
public----------->public
protected-------->protected
private---------->inherited but not accessible--except by publicly inherited base class functions
class DerivedName : protected BaseName
base class: derived class:
public---------->protected
protected------->protected
private--------->inherited but not accessible*
class DerivedName : private BaseName
base class: derived class:
public--------->private
protected------>private
private-------->inherited but not accessible**
(src: "Ivor Horton's Beginning C++")

Originally Posted by
Daved
No. priv() is not visible in any way in any of the derived classes.
That's not quite true. The result: "inherited but not accessible--except for publicly inherited functions" allows you to do this:
Code:
#include <iostream>
using namespace std;
class Base
{
private:
int member; //Can you get to this with public inheritance?
public:
void display()
{
cout<<member<<endl;
}
Base(int num)
{
member = num;
}
Base()
{
member = 0;
}
};
class Derived : public Base
{
private:
double number;
public:
Derived(double d, int b): Base(b)
{
number = d;
}
Derived()
{
number = 0.0;
}
};
int main()
{
Base B(2);
B.display();
Derived D(1.0, 2);
D.display(); //inherited method
return 0;
}
In addition, after examining the chart above, I can see there is also a way to get some visibility of a private base class member with protected inheritance. A public base class function, like display() (which has access to and displays the private base class member) will be protected in the derived class. Protected is the same as private within a class, and one of the first things we learn about classes is that class methods can access private members of the class. Although that means you cannot access display() directly with derived class objects, it does mean you can create methods in the derived class that have access to display(). That means you can create a method in the derived class which calls the display() method, and display() in turn has access to the private base class member. So, once again a private member of the base class has some visibility with protected base class inheritance:
Code:
#include <iostream>
using namespace std;
class Base
{
private:
int member; //can you get to this with protected inheritance?
public:
void display()
{
cout<<member<<endl;
}
Base(int num)
{
member = num;
}
Base()
{
member = 0;
}
};
class Derived : protected Base
{
private:
double number;
public:
Derived(double d, int b): Base(b)
{
number = d;
}
Derived()
{
number = 0.0;
}
void getBaseMember()
{
display();
}
};
int main()
{
Base B(2);
B.display();
Derived D(1.0, 2);
//D.display();
D.getBaseMember();
return 0;
}
In fact, that example also works if you change the inheritance to private. With private inheritance, the public base class method display() will be private instead of protected in the derived class, but within a class there is no difference between protected and private, and both are accessible to derived class methods. So, the private base class member has some visibility with private inheritance as well.
*
** The code examples above show that there is a way to get some visibility of private members in the base with all three types of inheritance: public, protected, and private.