title: C++ | Derived class is accessing private members of the base class rather than its own private members
body: <p>sorry if this is an obvious one but I've searched around and I'm still unclear about how to solve this issue. Here is my code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class PermMagnet {
  public:
      string name;
      int ac_rating;
      int dc_rating;
      int mass_kg;
      int age;
      PermMagnet(){
          // default constructor
          name = &quot;&quot;;
          ac_rating = 0; dc_rating = 0;
          mass_kg = 0; age = 0;
      }
      PermMagnet(string c_name, int c_ac_rating, int c_dc_rating, int c_mass_kg, int c_age){
          // parameterised constructor
          name = c_name;
          ac_rating = c_ac_rating;
          dc_rating = c_dc_rating;
          mass_kg = c_mass_kg;
          age = c_age;
      }
      string get_owner(){
          return owner;
      }
      string get_classifier(){
          return classifier;
      }
      int get_coil_count(){
          return coil_num;
      }
  protected:
      string location = &quot;facility hall&quot;;
  private:
      string owner = &quot;Unspecified Staff&quot;;
      string classifier = &quot;MAG-DP-&quot;;
      const int coil_num = 2;
  };
  
  class ElecMagnet : public PermMagnet {
  public:
      // inherit base class constructors
      using PermMagnet::PermMagnet;
  
      string get_location(){
          return location;
      }
  
  private:
      string owner = &quot;Specified Staff&quot;;
      string classifier = &quot;MAG-QD-&quot;;
      const int coil_num = 4;
  };
  
  int main() {
  
      // Create object using default constructor
      PermMagnet perm1;
      cout &lt;&lt; &quot;'perm1' age: &quot; &lt;&lt; perm1.age &lt;&lt; endl;
  
      // Create object using parameterised constructor
      PermMagnet perm2(&quot;PermMagnet 2&quot;, 380, 400, 1500, 35);
      cout &lt;&lt; &quot;'perm2' age: &quot; &lt;&lt; perm2.age &lt;&lt; &quot; | 'perm2' name: &quot; &lt;&lt; perm2.name &lt;&lt; endl;
      cout &lt;&lt; &quot;Owner of 'perm2': &quot; &lt;&lt; perm2.get_owner() &lt;&lt; endl;
      cout &lt;&lt; &quot;Upper current bound of 'perm2': &quot; &lt;&lt; perm2.get_current_limit(&quot;upper&quot;) &lt;&lt; &quot;A&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;Number of coils in 'perm2': &quot; &lt;&lt; perm2.get_coil_count() &lt;&lt; endl &lt;&lt; endl;
  
      // Create a ElecMagnet (derived class) object
      ElecMagnet elec1(&quot;ElecMagnet 1&quot;, 170, 200, 850, 27);
      cout &lt;&lt; elec1.get_classifier() &lt;&lt; endl;
      cout &lt;&lt; elec1.get_coil_count() &lt;&lt; endl;
  
      return 0;
  }
  
  </code></pre>
  <p>The output of this code is:</p>
  <pre><code>'perm1' age: 0
  'perm2' age: 35 | 'perm2' name: PermMaget 2
  Owner of 'perm2': Unspecified Staff
  Upper current bound of 'perm2': 780A
  Number of coils in 'perm2': 2
  
  MAG-DP-
  2
  
  Process finished with exit code 0
  </code></pre>
  <p>As you can see, I want &quot;owner&quot;, &quot;classifier&quot; and &quot;coil_num&quot; to be private members that cannot be changed by the user. These also vary depending on the class in question.</p>
  <p>The problem:</p>
  <p>The problem is the last two lines of the output. When the derived class (ElecMagnet) inherits the public functions that return these members, it returns the members of the base class; not it's own. You can see this because it returns the &quot;classifier&quot; and &quot;coil_num&quot; of the PermMagnet class.</p>
  <p>Does anyone know why the derived class is behaving this way? Shouldn't it access its own private members rather than the base?</p>