Constructing derived class from inherited variables

前端 未结 3 791
清酒与你
清酒与你 2021-01-21 08:23

Perhaps the title is a bit confusing so I\'ll try my very best to make sure it\'s as clear as possible.

Basically, I\'m trying to create a game where there is a abstract

3条回答
  •  执笔经年
    2021-01-21 08:58

    Allow setting them these variables in a constructor in the base class:

    class Creature
    {
      public:
        Creature();
      protected:
        Creature(int armor_, int strength_) : armor(armor_), strength(strength_){}        
    
        int armor;
        int strength;
    };
    

    Now in the derived class, just pass these to the base class:

    Human(int armor_, int strength_) : Creature(armor_, strength_){}     
    

提交回复
热议问题