title: Using an interface class as member type in another class
body: <p>I'm trying to design a piece of code that entails the use of an algorithm. The algorithm should be easily replaceable by someone else in the future. So in my <strong>LargeClass</strong> there has to be a way to invoke a specific algorithm.</p>
  
  <p>I provided some example code below. My idea was to make an interface class <strong>IAlgorithm</strong> so that you have to provide an implementation yourself. I thought you could initialize it to which ever derived class you wanted in the constructor of the <strong>LargeClass</strong>. However the below code doesn't compile in VS2015 because <code>IAlgorithm: cannot instantiate abstract class</code> </p>
  
  <p><strong>My question:</strong> How should I design this in order to get the result I want?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Algorithm.h</strong></p>
  
  <pre><code>class IAlgorithm
  {
  protected:
      virtual int Algorithm(int, int) = 0;
  };
  
  class algo1 : public IAlgorithm
  {
  public:
      virtual int Algorithm(int, int);
  };
  
  class algo2 : public IAlgorithm
  {
  public:
      virtual int Algorithm(int, int);
  };
  </code></pre>
  
  <p><strong>Algorithm.cpp</strong></p>
  
  <pre><code>#include "Algorithm.h"
  
  int algo1::Algorithm(const int a, const int b)
  {
      // Do something
  }
  
  int algo2::Algorithm(const int a, const int b)
  {
      // Do something
  }
  </code></pre>
  
  <p><strong>Source.cpp</strong></p>
  
  <pre><code>#include "Algorithm.h"
  
  class LargeClass
  {
  
  private:
      IAlgorithm algo;
  };
  
  int main()
  {
  
  
  }
  </code></pre>