This question already has answers here:
What is the difference between public, private, and protected inheritance in C++?
(16 answers)
Closed 6 years ago.
Simple code:
class A
{
private: int a;
protected: int b;
public: int c;
};
class B : protected A
{
};
class C : protected B
{
};
I know in Class B, a will remain private & b and c are protected.
But what I'm confused about is what will the access specifiers be in class C?
With protected inheritance inherited public members become protected.
With private inheritance inherited public and protected members become private.
Related
This question already has answers here:
What is the difference between public, private, and protected inheritance in C++?
(16 answers)
Closed 4 years ago.
I don't understand and i really don't get public vs private inheritance in classes.
Suppose we have the following code:
class A {
int A_a;
protected:
int A_b;
public:
int A_c;
};
class B : public A {
int B_a;
protected:
int B_b;
public:
int B_c;
};
class C : private A {
int C_a;
protected:
int C_b;
public:
int C_c;
};
I know it has to be with access rights over vars and funcs, but doing all the tests i really can grasp it and i don't know when to apply public or private inheritance;
Here is simple sheme (base class -> derived clas) how visibility of class members change with different types of inheritance:
Public inheritance:
public -> public
protected -> protected
private -> private
Protected inheritance:
public -> protected
protected -> protected
private -> private
Private inheritance:
public -> private
protected -> private
private -> private
Here you have few simple examples https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
This question already has answers here:
What is the difference between public, private, and protected inheritance in C++?
(16 answers)
Closed 6 years ago.
My questions is why b.getmultiply(); will not cause compilation error?
Class B is private inherit from class A, and x and y are members of class A.
class A {
public:
int x;
int y;
void set(int a, int b) { x = a; y =b;}
};
class B : private A{
public:
int getmultiply (void){
return x*y;}
};
int main(void)
{
B b;
//b.set(3,4); // this will cause compilation error
cout << b.getmultiply(); // why this will not??
return 0;
}
When you private inherit from a base class, its public members become private members of the derived class. These members are public and accessible inside of member functions of the derived class (e.g. B.getmultiply()), but are private and not accessible to outside code (e.g. main()) that is not a friend of the derived class.
When a class privately inherits from another, it still has access to that class's (non-private) members just like under public inheritance. It is only the outside world that doesn't have this access to them because they become private in the context of the derived class (in fact the outside world doesn't even know the derived is a derived: you can't refer to an instance of B with a pointer of type A for example).
This question already has answers here:
Why does C++ not allow inherited friendship?
(9 answers)
Closed 8 years ago.
There are three classes:
class A
{
friend I_B;
protected:
void* mData;
};
class I_B
{
void foo() = 0;
};
class B_Impl : public I_B
{
B_Impl( A* value )
:
mData( value->mData ) <--- ERROR
{
}
void foo() { mData->DoSomething() };
protected:
void* mData;
};
At compile time I get an error in the constructor, that mData is a protected member.
Please explain me please why it happens.
Can I get access to protected members using "friendship" of the base class?
Friendship is not inherited. If you want B_Impl to be a friend of A you must declare B_Impl as a friend.
Friendship is also not transitive: your friend's friend is not necessarily your friend.
This question already has answers here:
What are access specifiers? Should I inherit with private, protected or public?
(2 answers)
Closed 10 years ago.
How is base class access specification different from member access specification?
Base class access specification decides about base class' members access specification in your class. They provide a way to hide base class' members if you don't want them to appear publicly in your class. They doesn't affect visibility of members of your class though.
C++ FAQ explains this issue quite nicely.
class Base
{
protected:
int A;
public:
int B;
};
class Derived1 : public Base
{
// Derived1::A outside class is seen as protected
// Derived1::B outside class is seen as public
};
class Derived2 : protected Base
{
// Derived1::A outside class is seen as protected
// Derived1::B outside class is seen as protected
};
class Derived3 : private Base
{
// Derived1::A outside class is seen as private
// Derived1::B outside class is seen as private
};
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between private, public and protected inheritance in C++
What is difference between deriving as protected or private in c++? i am not able to figure out, since both seem to restrict base class member access from derived class object
Let's consider a code example showing what would be allowed (or not) using different levels of inheritance:
class BaseClass {};
void freeStandingFunction(BaseClass* b);
class DerivedProtected : protected BaseClass
{
DerivedProtected()
{
freeStandingFunction(this); // Allowed
}
};
DerivedProtected can pass itself to freeStandingFunction because it knows it derives from BaseClass.
void freeStandingFunctionUsingDerivedProtected()
{
DerivedProtected nonFriendOfProtected;
freeStandingFunction(&nonFriendOfProtected); // NOT Allowed!
}
A non-friend (class, function, whatever) cannot pass a DerivedProtected to freeStandingFunction, because the inheritance is protected, so not visible outside derived classes. Same goes for private inheritance.
class DerivedFromDerivedProtected : public DerivedProtected
{
DerivedFromDerivedProtected()
{
freeStandingFunction(this); // Allowed
}
};
A class derived from DerivedProtected can tell that it inherits from BaseClass, so can pass itself to freeStandingFunction.
class DerivedPrivate : private BaseClass
{
DerivedPrivate()
{
freeStandingFunction(this); // Allowed
}
};
The DerivedPrivate class itself knows that it derives from BaseClass, so can pass itself to freeStandingFunction.
class DerivedFromDerivedPrivate : public DerivedPrivate
{
DerivedFromDerivedPrivate()
{
freeStandingFunction(this); // NOT allowed!
}
};
Finally, a non-friend class further down the inheritance hierarchy cannot see that DerivedPrivate inherits from BaseClass, so cannot pass itself to freeStandingFunction.
Use this matrix (taken from here) to decide the visibility of inherited members:
inheritance\member | private | protected | public
--------------------+-----------------+---------------+--------------
private | inaccessible | private | private
protected | inaccessible | protected | protected
public | inaccessible | protected | public
--------------------+-----------------+---------------+--------------
Example 1:class A { protected: int a; }
class B : private A {}; // 'a' is private inside B
Example 2:
class A { public: int a; }
class B : protected A {}; // 'a' is protected inside B
Example 3:
class A { private: int a; }
class B : public A {}; // 'a' is inaccessible outside of A
private allows only the class it is declared in to access it
protected allows that class and derived/sub classes to access as if it were private
I had added a very detailed explanation about Inheritance & Access Specifiers in this Q. It explains all the types of Inheritance and how access specifiers work with each of them. Do have a look.
Hth :)
Basically, protected inheritance extends further down the inheritance hierarchy than does private inheritance. See the C++ FAQ Lite for details.