title: Inheritance/Polymorphism - Am I forced to use "protected" variables?
body: <p>I am currently working with Big C++ 2nd Edition, using Code::Blocks 17.12, on the chapter for inheritance</p>
  
  <p>The book introduces the protected variable type for cases in which you want to allow a derived class to access said variable.</p>
  
  <p>The book also forewarns that protected elements suffer from some of the pitfalls of a public variable: in the worst cases derived class members can corrupt the base class data</p>
  
  <p>The book demos the use of a protected member in a clock program in a dedicated section in which they introduce this,  but in the final code they ultimately went with setting the variable to private and then using some get_ helper functions to access this private data. This combo of private and using helper functions always returned errors in my IDE and I wasn't able to pull that off</p>
  
  <p>Take this simple example I created for P8.1, a employee record for a programmer, with an employee base class and programmer derived class. I created the following ctor, with the variables name and sal set to <em>protected</em> status in the base class</p>
  
  <pre><code>Programmer::Programmer(string p_name, double p_sal)
      :Employee(get_name(), get_sal())
  {
      name=p_name;
      sal=p_sal;
  }
  </code></pre>
  
  <p>With this code, the program works perfectly.</p>
  
  <p>Based on the textbook, if the variables name and sal were set to <em>private</em> status in the base, then I should be able to execute the code also, granted that I am using a get_ helper function I created to accessed the data.</p>
  
  <p>Can anyone explain what the issue is here? Should I be ok with using protected or is there truly a way to keep all of my variables private for classes?</p>
  
  <p>I also found this on <a href="https://docs.microsoft.com/en-us/cpp/cpp/protected-cpp?view=vs-2019" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/cpp/cpp/protected-cpp?view=vs-2019</a></p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>I have not covered static so far, So I ultimately tried a bunch of different combinations with pointers and references, none of which worked either.</p>
  
  <p>I am trying to understand when to use protected vs when to use private basically and the book isn't being clear on that. Any ideas?</p>