17

I am a newbie to C++, I have a question regarding to the c++ protected and private members in inheritance.

If a class is public inherits a based class, does the protected and private member variable will be part of derived class?

For example:

class Base
{
   protected: 
       int a;
       int b;
   private:
       int c;
       int d;
   public;
       int q;
};

class Derived: public Base
{

};

does class Derived also have all the member of a, b, c, d, q? and can we define a int a as public, protected, and private in Derived class?

ratzip
  • 1,571
  • 7
  • 28
  • 53
  • possible duplicate of [Difference between private, public, and protected inheritance](http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) – sonicwave Apr 02 '15 at 09:05
  • 3
    `public`, `protected`, and `private` are about access to the *names* of members. The inherited members exist in an instance of the derived class, even those that the instance itself can't access by name. – molbdnilo Apr 02 '15 at 09:08

6 Answers6

20

No class can access private variables. Not even subclasses.

Only subclasses can access protected variables.

All classes can access public variables.

therainmaker
  • 4,253
  • 1
  • 22
  • 41
13

All the member of the base class are part of the derived class. However, the derived class can only access members that are public or protected.

Declaring a member of the same name as a member of a Base class "shadows" the member of the Base class. That is the Derived class has its own independent variable that happens to have the same name as the base class version.

This is a personal choice, but I find using variables to communicate between base classes and derived classes leads to messier code so I tend to either make member variables private or use the PIMPL pattern.

Graham Dawes
  • 229
  • 1
  • 3
8

The private members of a class can be inherited but cannot be accessed directly by its derived classes. They can be accessed using public or protected methods of the base class.

The inheritance mode specifies how the protected and public data members are accessible by the derived classes.

If the derived class inherits the base class in private mode,

  1. protected members of base class are private members of derived class.
  2. public data members of base class are private members of derived class.

If the derived class inherits the base class in protected mode,

  1. protected members of base class are protected members of derived class.
  2. public data members of base class are protected members of derived class.

If the derived class inherits the base class in public mode,

  1. protected members of base class are protected members of derived class.
  2. public data members of base class are public members of derived class.

Refer this link for more clarification: http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

Reena Cyril
  • 417
  • 4
  • 11
  • 2
    The derived class always inherits ALL members of the base class. The different protection levels only control what the derived class can access, not what is inherited. – DaveB Apr 02 '15 at 11:57
5
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    // 'private' is default for classes 
{ 
    // x is private 
    // y is private 
    // z is not accessible from D 
}; 

The below table summarizes the above three modes and shows the access specifier of the members of base class in the sub class when derived in public, protected and private modes:

https://i.stack.imgur.com/AIncv.jpg

https://www.geeksforgeeks.org/inheritance-in-c/

0

No derived class can access attributes or methods of its private base class. It can only access that of either public or protected. Even with setter and getter defined in the base class, you still get errors accessing members of the base class.

0
class A{
protected:
    int a {9723999};
};


class B:public A{
public:
    B(){
        cout<<a<<endl;
    }

};

int main()
{
    B b;
    return 0;
}

child class can not access private member of parent class