原文 : http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A
{
// x is private
// y is private
// z is not accessible from D
};
IMPORTANT NOTE: Classes B, C and D all contain the variables x, y and z. It is just question of access.
About usage of protected and private inheritance you could read
here.
原文 : http://www.programiz.com/cpp-programming/public-protected-private-inheritance
Summary of Public, Protected and Private Inheritance
Accessibility in Public Inheritance
Accessibility | private | protected | public |
Accessible from own class? | yes | yes | yes |
Accessible from dervied class? | no | yes | yes |
Accessible outside dervied class? | no | no | yes |
Accessibility in Protected Inheritance
Accessibility | private | protected | public |
Accessible from own class? | yes | yes | yes |
Accessible from dervied class? | no | yes | yes |
Accessible outside dervied class? | no | no | no |
Accessibility in Private Inheritance
Accessibility | private | protected | public |
Accessible from own class? | yes | yes | yes |
Accessible from dervied class? | no | yes | yes |
Accessible outside dervied class? | no | no | no |
沒有留言:
張貼留言