title: Access of member functions in C++ inheritance
body: <p>I am just confused about the tiny program on inheritance below: </p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  struct B {
      virtual int f() { return 1; }
  }; // f is public in B
  
  class D : public B { 
      int f() { return 2; }
  }; // f is private in D
  
  int main()
  {
      D d;
      B&amp; b = d;
      cout&lt;&lt;b.f()&lt;&lt;endl; // OK: B::f() is public, D::f() is invoked even though it's private
      cout&lt;&lt;d.f()&lt;&lt;endl; // error: D::f() is private
  }
  </code></pre>
  
  <ol>
  <li>I can't figure out why <code>D::f()</code> is private, <code>D</code> is <strong>public inherited</strong> from <code>B</code>, so the public function <code>f</code> in <code>B</code><br>
  is also public in <code>D</code> (I know without inheritance, member access is private by default)</li>
  <li><code>f</code> is a virtual function in <code>B</code>, so if we call <code>b.f()</code>, we actually call <code>D::f()</code>, but just as the illustration mentioned, why <code>D::f()</code> is able to be invoked even though it's private?</li>
  </ol>
  
  <p>Can anyone explain the simple inheritance problem in detail?</p>