the protected and private member variables in C++ inheritance

ratzip picture ratzip · Apr 2, 2015 · Viewed 26.7k times · Source

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?

Answer

therainmaker picture therainmaker · Apr 2, 2015

No class can access private variables. Not even subclasses.

Only subclasses can access protected variables.

All classes can access public variables.