title: C++ Protected Variables Not Inherited
body: <p>I have written some code to calculate the RSA cryptographic algorithm. The program uses classes and inheritance because I want to calculate a public and private key for multiple users. There is a parent class <code>rsa</code> and child classes <code>public_key</code> and <code>private_key</code>.</p>
  
  <p>When compiling the code below, I get many errors. All of them are about the derived classes not having the available fields in their respective constructors (see error message below code). However, these variables are defined with the <code>protected</code> access modifier in the parent class, so they should be accessible to the child class.</p>
  
  <p>One side note: I had the function <code>key</code> in both of the child classes, but I thought it would be better to put it once in the parent class, is this right?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  class rsa
  {
  protected:
      int p, q, d, m, n, f, e, c, end, k;
  
  public:
      rsa() : n(0), e(0), c(0), k(0), end(0), f(0)
      { }
  
      void set(int , int , int, int);
  
      int key()
      {
          n = p * q;
          f = (p - 1) * (q - 1);
  
          for (k; end &lt; 1; k++)
          {
              if ((1 + k * f) % d == 0) 
              {
                  end = 2;
                  e = (1 + k * f) / d;
              }
          }
  
          c = int(pow(m, e)) % n;
  
          return c;
      }
  };
  
  void rsa::set(int p_, int q_, int d_, int m_)
  {
      p = p_;
      q = q_;
      d = d_;
      m = m_;
  }
  
  class public_key : public rsa
  {
  public:
      public_key() : n(0), e(0), c(0), k(0), end(0), f(0)
      { }
  };
  
  class private_key : public rsa
  {
  public:
      private_key() : n(0), e(0), c(0), k(0), end(0), f(0)
      { }
  };
  
  int main()
  {
      public_key usr1, usr2;
      private_key usr1r, usr2r;
  
      usr1.set(11, 5, 23, 9);
      usr2.set(13, 7, 97, 6);
      usr1r.set(17, 7, 51, 8);
      usr2r.set(11, 17, 51, 4);
  
      cout &lt;&lt; "Public key of user 1: " &lt;&lt; usr1.key() &lt;&lt; endl;
      cout &lt;&lt; "Public key o user 2: " &lt;&lt; usr2.key() &lt;&lt; endl;
  
      cin.get();
  
      return 0;
  }
  </code></pre>
  
  <p>One of the errors:</p>
  
  <pre><code>error: class ‘private_key’ does not have any field named ‘e’
     private_key () : n(0), e(0), c(0), k(0), end(0), f(0) {} ;
  </code></pre>
  
  <p>All the other errors are the same but the field name changes.</p>