title: C++ interview inheritance puzzle
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/496440/c-virtual-function-from-constructor">C++ virtual function from constructor</a><br>
    <a href="https://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors">Calling virtual functions inside constructors</a>  </p>
  </blockquote>
  
  
  
  <p>This question was asked in interview .</p>
  
  <p>I guess I had answered the 1st part correctly but not sure about the 2nd part. In fact I have no clue about 2nd part.    </p>
  
  <ol>
  <li>What output does the following code generate? Why?     </li>
  <li>What output does it generate if you make A::Foo() a pure virtual function?  </li>
  </ol>
  
  <p>When I tried running same question on my compiler with <code>virtual void foo() = 0;</code> it throws 
  error "undefined reference to `A::Foo()'"</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class A     
  {    
  public:       
      A()             
      {
          this-&gt;Foo();
      }
      virtual void Foo() 
      {
          cout &lt;&lt; "A::Foo()" &lt;&lt; endl;
      }
  };
  
  class B : public A      
  {     
  public:     
      B()      
      {
          this-&gt;Foo();      
      }
      virtual void Foo() 
      {
          cout &lt;&lt; "B::Foo()" &lt;&lt; endl;
      }
  };
  
  int main(int, char**)
  {
      B   objectB;
      return 0;
  }
  </code></pre>