title: how is the decision taken at run time whether to call derived class function or base class function?
body: <p>How is the decision taken internally (during runtime) as to which function to call, either the base class or derived class function. In the below sample code how is decision taken to call <strong>fn()</strong> of <strong>B</strong> and <strong>fn2()</strong> of <strong>A</strong>.</p>
  
  <pre><code>Class A { 
      virtual void fn() { std::cout &lt;&lt; "A" &lt; &lt;std::endl; }
      virtual void fn2() { std::cout &lt;&lt; "A-fn2" &lt; &lt;std::endl; }
      };
  
  Class B : A
  {
      void fn() { std::cout &lt;&lt; "B" &lt; &lt;std::endl; }
  }
  
  int main() {
      B b = new B;
      A *a = &amp;b;
      a-&gt;fn();
      a-&gt;fn2();
  }
  </code></pre>
  
  <p>The output would be <strong>B</strong> and <strong>A-fn2()</strong></p>
  
  <p>Basically how is the decision taken at run time whether to call derived class function or base class function?</p>