title: Abstract Factory in C++
body: <p>I'm trying to understand the abstract factory pattern, here is my first approximation:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Soldier
  {
      public:
      virtual void shoot()=0;
  };
  
  class Archer: public Soldier
  {
      public:
      void shoot(){
          cout&lt;&lt;"Archer shoot"&lt;&lt;endl;
      }
  };
  
  class Rider: public Soldier
  {
      public:
      void shoot(){
          cout&lt;&lt;"Rider shoot"&lt;&lt;endl;
      }
  };
  
  class AbstractFactory
  {
      public:
      virtual Soldier* createArcher()=0;
      virtual Soldier* createRider()=0;
  };
  
  class OrcFactory: public AbstractFactory
  {
      Soldier* createArcher()
      {
          return new Archer();
      };
      Soldier* createRider()
      {
          return new Rider();
      };
  };
  
  class HumanFactory: public AbstractFactory
  {
      Soldier* createArcher()
      {
          return new Archer();
      };
      Soldier* createRider()
      {
          return new Rider();
      };
  };
  
  class Game
  {
  
      public:
      AbstractFactory* factory;
      Game(AbstractFactory* factory):factory(factory){};
  };
  
  int main()
  {
      Game* game = new Game(new HumanFactory);
      Archer* HumanArcher = static_cast &lt;Archer*&gt;(game-&gt;factory-&gt;createArcher());
      Rider* humanRider = static_cast &lt;Rider*&gt;(game-&gt;factory-&gt;createRider());
      HumanArcher-&gt;shoot();
      humanRider-&gt;shoot();
      return 0;
  }
  </code></pre>
  
  <p>This is what I want to reproduce:</p>
  
  <p><a href="https://i.stack.imgur.com/24Kp0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/24Kp0.png" alt="class diagram"></a></p>
  
  <p>I have experience in programing but I'm newbie with patterns, not sure if this is the optimal solution or even if it's a good solution.</p>
  
  <p>I'm reading about game engine architecture, but I'm stuck in this, not by errors, just doubt about the right solution for this exercise. The book has basic examples but not enough to understand it at all.</p>