title: Error on C++: "error: int aaa::bbb is protected within this context"
body: <p>After years of programming in C, I'm doing my first steps in C++.</p>
  <p>I'm trying to grasp the &quot;protected&quot; concept, and there is a lot of material on the web that explains what a <code>protected</code> variable is, and what they are used for.  However, when trying to code a super-basic example, just to get my hands dirty with C++, I bumped into an error:</p>
  <blockquote>
  <p>error: 'int parent::protected1' is protected within this context</p>
  </blockquote>
  <p>So, a little help will be appreciated.</p>
  <pre><code>class parent {
  public:
      int getProtected() { return protected1; }
  protected:
      int protected1;
  };
  
  class child: public parent { };
  
  int main()
  {
      child ch;
  
      cout &lt;&lt; ch.protected1 &lt;&lt; endl;    // error: 'int parent::protected1' is protected within this context
      cout &lt;&lt; ch.getProtected() &lt;&lt; endl;   // OK
      return 0;
  }
  </code></pre>
  <p>Everywhere it is said that <code>protected</code> variables are only accessible within an inheritance hierarchy.  If that's the case, I'm trying to understand - what am I doing wrong here?</p>
  <p>The concept of &quot;protected variable&quot; hasn't really sunk in. The <code>private</code> is well understood, as a private variable belongs to the child instance and therefore can be accessed only by child methods.  However, if a child can access a <code>protected</code> variable of the parent, does it mean a parent must be instantiated before a child can access this <code>protected</code> variable?</p>