title: C++ pure virtual functions implementation in derived class
body: <p>I have a <strong>top</strong> API (based on abstract class) which is composed by a <strong>base</strong> API (that will be reused by different <strong>top</strong> APIs).</p>
  
  <pre><code>class api_base {
  public:
      virtual int foo() = 0;
  };
  
  class api_top : public api_base {
  public:
      virtual int bar() = 0;
  }
  </code></pre>
  
  <p>Then, I want to provide a base implementation for base API:</p>
  
  <pre><code>class imp_base {
  public:
      int foo() { return 1; }
  }
  </code></pre>
  
  <p>And finally implement <strong>top</strong> API using base implementation for those functions defined in base API:</p>
  
  <pre><code>class imp_top : public api_top, public imp_base {
  public:
      int bar() { return 1; }
  }
  </code></pre>
  
  <p>When I instantiate an object of <code>imp_top</code> type, compiler say that <code>foo()</code> function is not implemented. But that is not true, since <code>imp_top</code> derives from <code>imp_base</code>, which do have <code>foo()</code> function implemented.</p>
  
  <p>Any advice about this?</p>
  
  <p>Thank you in advance.</p>
  
  <p>Full test code:</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
  
      class api_base { 
      public:
          virtual int foo() = 0;
      };
  
      class api_top : public api_base { 
      public:
          virtual int bar() = 0;
      };
  
      class imp_base { 
      public:
          int foo() { printf("foo from imp_base\n"); } 
      };
  
      class imp_top : public api_top, public imp_base { 
      public:
          int bar() { printf("bar from imp_top\n"); } 
      };
  
      int main()
      { 
         printf("Hello\n");                                                                                                                                                                                                                                                   
         imp_top a;
  
         a.bar();
         a.foo();
  
         return 1;
      }
  </code></pre>
  
  <p>Compiler result:</p>
  
  <pre><code>    test.cpp:26:12: error: cannot declare variable ‘a’ to be of abstract type ‘imp_top’                                                                                                                                                                                     
          imp_top a;
                  ^
      test.cpp:18:7: note:   because the following virtual functions are pure within ‘imp_top’:
       class imp_top : public api_top, public imp_base {
             ^
      test.cpp:5:17: note:  virtual int api_base::foo()
           virtual int foo() = 0;
                       ^
      test.cpp:29:6: error: request for member ‘foo’ is ambiguous
          a.foo();
            ^
      test.cpp:15:9: note: candidates are: int imp_base::foo()
           int foo() { printf("foo from imp_base\n"); }
               ^
      test.cpp:5:17: note:                 virtual int api_base::foo()
           virtual int foo() = 0;
  </code></pre>