title: Why does the size of class in c++ depend on the public/private status of data members?
body: <p>From what i know, the size of a class in c++ depends on the below factors -</p>
  
  <ol>
  <li>Size of all non-static data members.</li>
  <li>Order of data members.</li>
  <li>If byte padding is enabled or not.</li>
  <li>Size of its immediate base class.</li>
  <li>The existence of virtual functions.</li>
  <li>Mode of inheritance (virtual inheritance).</li>
  </ol>
  
  <p>Now I've created 2 classes as below -</p>
  
  <pre><code>class A{
      int a;
      short s;
      int b;
      char d;
  };// kept a char at last on purpose to leave a "hole"
  
  class B : public A{
      char c;  
  };
  </code></pre>
  
  <p>now on checking the size of A and B I see</p>
  
  <ul>
  <li>size of A: 16</li>
  <li>size of B: 16</li>
  </ul>
  
  <p>my assumption is the char c in class B is accommodated in "hole" left in class A.</p>
  
  <p>But, whats confused me is the below scenario wherein I make the members public</p>
  
  <pre><code>class A{
      public:
      int a;
      short d;
      int b;
      char s;
  };
  
  class B : public A{
      public:
      char c;
  };
  </code></pre>
  
  <p>Now the size becomes</p>
  
  <ul>
  <li>size of A: 16</li>
  <li>size of B: 20</li>
  </ul>
  
  <p>I cannot seem to understand the reason for this difference.</p>