Polymorphism

This is a discussion on Polymorphism within the C++ Programming forums, part of the General Programming Boards category; Hello, PHP Code: //[CODE] /*-------- Item Class --------*/ class  Item {  protected:       string name ;       int price ;       int useLevel ;  public:       ...

  1. #1
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142

    Polymorphism

    Hello,

    PHP Code:
    //[CODE]

    /*-------- Item Class --------*/
    class Item
    {
     protected:
         
    string name;
         
    int price;
         
    int useLevel;

     public:
         
    Item(string tnameint tpriceint tuseLevel);
         ~
    Item();

         
    string getName();
         
    int getPrice();
         
    int getUseLevel();
         
    virtual char getType();
         
    virtual int getDamage()=0;
         
    virtual bool isTwoHanded()=0;
         
    virtual int getProtection()=0;
    };


    /*-------- Weapon(Item) Class --------*/
    class Weapon : public Item
    {
     private:
         
    int causeDamage;
         
    bool twoHand;

     public:
         
    Weapon(string tnameint tpriceint tuseLevel ,int damagebool twohands);
         ~
    Weapon();

         
    int getDamage();
         
    bool isTwoHanded();
         
    char getType();
    };


    /*-------- Armor(Item) Class --------*/
    class Armor : public Item
    {
     private:
         
    int protection;

     public:
         
    Armor(string tnameint tpriceint tuseLevel ,int tprotection);
         ~
    Armor();

         
    int getProtection();
         
    char getType();
    };

    //[/CODE] 
    Item class is a base abstract class. Both Weapon and Armor are derived class. As far as i can remember the above is legal, but when the code goes like this
    Code:
    Item *equipedWeapon = new Weapon("Wooden Stick", 0, 1, 5, false);
    Item *equipedArmor = new Armor("Shirt", 0, 1, 2);
    MS VS compiler produces :
    error C2259: 'Weapon' : cannot instantiate abstract class
    error C2259: 'Armor' : cannot instantiate abstract class

    Following the error code i came up to this which doesn't explain why i have to define the functions of Armor also in Weapon class and vice versa.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You need to override all pure methods (=0) if you want to instantiate those.

    IMO, one idea to design the whole thing would be not to have different subclasses, but instead have the Item class contain all attributes in a map, e.g

    Code:
    class Item
    {
        std::map<std::string, int> data;
    ...
        int get(const std::string& attribute) const;
    };
    
    Item equipedWeapon("Wooden Stick", weapon_init_data ...);
    int weaponDamage = equipedWeapon.get("damage"); //etc
    If you want to use inheritance and polymorphism, then perhaps don't make anything pure abstract, but instead make the base class provide a default implementation (e.g by default an Item does 0 damage, offers 0 protection, is not two-handed etc), which subclasses may override if needed (e.g Weapon overrides getDamage, but doesn't add any protection and doesn't override getProtection).

    If you want to make sure Item is not instantiated, it is enough to make only the destructor pure virtual. Making other methods pure virtual means that derived classes are expected to override all of those.
    Last edited by anon; 02-15-2011 at 03:41 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Quote Originally Posted by anon View Post
    You need to override all pure methods (=0) if you want to instantiate those.
    I can't see the point of overriding (for example) the getProtection() function in Weapon class.
    It can lead to chaos for deep inheritance.

    Anyway, thank you.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps you replied before I had finished editing.

    If it doesn't make sense to force derived classes to override those methods, then don't mark them pure virtual.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    It really doesn't make sense to me right now (more than two years away from oop), because i can't see reuseability.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pure Virtual Polymorphism HELP
    By Syndacate in forum C++ Programming
    Replies: 17
    Last Post: 01-14-2011, 10:21 AM
  2. Polymorphism
    By Eman in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2010, 06:51 AM
  3. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  4. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  5. change sorting method using polymorphism
    By Forever82 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21