Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between protected and private derivation in c++ [duplicate]

Tags:

c++

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

like image 996
paseena Avatar asked May 05 '11 21:05

paseena


People also ask

What is the difference between public/private and protected derivation?

If the class member declared as public then it can be accessed everywhere. If the class members declared as protected then it can be accessed only within the class itself and by inheriting child classes. If the class members declared as private then it may only be accessed by the class that defines the member.

What is the difference between private and protected access specifiers?

In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is the difference between private and protected visibility?

Things that are private are only visible within the class itself. Things that are protected are visible in the class itself and in subclasses.

What is protected derived class?

Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.


3 Answers

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.

like image 89
Daniel Gallagher Avatar answered Oct 06 '22 01:10

Daniel Gallagher


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
like image 34
Peter Jankuliak Avatar answered Oct 05 '22 23:10

Peter Jankuliak


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

like image 40
Meberem Avatar answered Oct 05 '22 23:10

Meberem