title: C++ - Accessing protected/private members of a base class
body: <p>I'm trying out a small example to practice the concepts of inheritance, and polymorphism. Here is a simplified version of my code:</p>
  
  <pre><code>class Shape {
  protected:
      int length;
      int width;
  public:
      virtual void setLength(int l) = 0;
      virtual void setWidth(int w) = 0;
  };
  
  class Rectangle : public Shape {
  public:
      Rectangle(int l, int w)
      : length(l), width(w)
      { }
  
      void setWidth(int w) { width = w; }
      void setLength(int l) { length = l; }
  };
  
  int main() {
      Rectangle r(0,0);
  }
  </code></pre>
  
  <p>I'm trying to run the above program. However, when I compile rectangle.cc, I get the following error</p>
  
  <pre><code>g++ -c rectangle.cc
  
  rectangle.cc: In constructor 'Rectangle::Rectangle(int, int)':
  rectangle.cc:13:5: error: class 'Rectangle' does not have any field named 'length'
  rectangle.cc:13:16: error: class 'Rectangle' does not have any field named 'width'
  </code></pre>
  
  <p>To my understanding, in public inheritance, protected members of the base class become protected members of the derived class and should be able to e accessed like public members. Is that incorrect? Also, how would the code then need to be modified if length and width were private members of the base class?</p>