title: Why is initialization of derived class through a base class pointer different from that through a derived class pointer?
body: <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Base {
  public:
          void F(){cout &lt;&lt; "Base::F" &lt;&lt; endl;};
          virtual void G(){cout &lt;&lt; "Base::G" &lt;&lt; endl;};
  };
  
  class Derived : public Base {
  public:
          void F(){cout &lt;&lt; "Derived::F" &lt;&lt; endl;};
          void G(){cout &lt;&lt; "Derived::G" &lt;&lt; endl;};
  };
  
  int main(){
          Derived *pDerived = new Derived;
          pDerived-&gt;F(); //F was redefined
          pDerived-&gt;G(); //G was overriden
          Base *pBase = new Derived;
          pBase-&gt;F();
          pBase-&gt;G();
  
  }
  </code></pre>
  
  <p>The output of this code is:</p>
  
  <pre><code>Derived::F
  Derived::G
  Base::F
  Derived::G
  </code></pre>
  
  <p>Why does not the code produce the following output?</p>
  
  <pre><code>Derived::F
  Derived::G
  Derived::F
  Derived::G
  </code></pre>
  
  <p>i.e. When a derived class object is initialized through a base class pointer, why is the function definition for a non-virtual function different from that of a derived class object initialized through a derived class pointer? Shouldn't the same type of object be initialized when we call "new Derived" whether it be from a base class pointer or a derived class pointer?</p>