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
Related
Can the derived class access the public member of its private base class
class book {
public:
void read ();
void show ();
};
class science: private book
{
public:
void readbook ();
void showbook ();
};
}
Can the object of science class access the public of book class which is private base class.
you can not call book methods directly from an object of the science class.
science s;
s.read();
won't work
You can only access the book methods from insight the science class. For example a method in the science class that calls the now private methods from the book class.
An easy way to remember if you can access base class functions/members is to see the type of inheritance used.
Public inheritance
Every public member/function of the base class remains public in the derived class.
Protected inheritance
Every public member/function of the base class becomes protected in the derived class.
Private inheritance
Both public and protected members/functions in the base class become private in the derived class.
However it's still possible to expose the interface of private base class by writting a using declaration.
class book
{
public:
void show();
};
class science : private book
{
public:
void showscience();
using book::show;
}
Now you can call book::show through a science object
science s;
s.show();
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.
Regardless of whether it is public, private or protected inheritance, the private members of the base class are not accessible to the functions exclusive to the derived class.
This is my conclusion. Is it correct?
Related notes would be appreciated.
Also, in private inheritance, the public members of the base class would be private in the derived class but the new functions of the derived class can still access them directly. Correct?
Why not test it?
class Base {
private:
int a;
};
// Private inheritance.
class A : private Base {
public:
A() {
a = 0;
}
};
This gives me:
error: 'int Base::a' is private
The type of inheritance does not matter when it comes to the ability for a class to access it's base-classes private variables.
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.