title: How to refer to instances of a class universally without type codes?
body: <p>I was making a text based RPG in which I have an abstract <code>Item</code> class. From this <code>Item</code> class, I have the classes <code>Weapon</code>, <code>Potion</code>, <code>Key</code>, and <code>Armor</code>. The main character class, <code>Protagonist</code> uses these items and has a function <code>doItemEffect(Item*)</code>. How do I implement <code>doItemEffect(Item*)</code> in a way that I refer to all items in universally? To better phrase my problem, if I wasn't clear, here is an example that uses a quite ugly solution.</p>
  
  <pre><code>class Protagonist
  {
  public:
      void doItemEffect(Item* it)
      {
          switch(it-&gt;getType()) //&lt;-- The type is an enum class
          {
          case ItemType::WEAPON:
              attackOpponent(it.getAttackPower()); //If it is a weapon it would have this function
              break;
          case ItemType::POTION:
              heal(it.getHealPower()); //If it is a weapon it would have this function
              break;
          case ItemType::KEY:
              //..Code..
              break;
          case ItemType::ARMOR:
              //More Code...
              break;
       }
  };
  </code></pre>
  
  <p>And an example of two of the classes <code>Potion</code> and <code>Weapon</code> (The type of the class is a private variable stored in <code>Item</code> with a mutator method <code>setType()</code>):</p>
  
  <pre><code>class Potion : public Item
  {
      int healPower;
  public:
      Potion(std::string name, std::string description) : Item(name, description)
      {
          setType(ItemType::POTION);
      }
      //Code
  };
  </code></pre>
  
  <p>Weapon:</p>
  
  <pre><code>class Weapon : public Item
  {
      int attackPower;
  public:
      Weapon(std::string name, std::string description) : Item(name, description)
      {
          setType(ItemType::WEAPON);
      }
      //Code
  };
  </code></pre>
  
  <p>As you can see, this code relies on an class code and a switch in the <code>Protagonist</code> class. Because of this, this doesn't seem very object oriented or polymorphic. Thus, is there a way I could get what subclass a type of Item is, without having to use class codes? Or is there any other solution? The other problem with this snippet above is also that whenever I refer to an item outside of its class, I have to use the same switch statement for each type of item.</p>