Constructing derived class from inherited variables

前端 未结 3 787
清酒与你
清酒与你 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:44

    You can create a constructor in base class that takes str and armor as parameters and then pass them to the base constructor in the constructor of the derived class.

    class Creature
    {
      public:
        Creature(int a, int s) : armor(a), strength(s) {};
    
    protected:
        int armor;
        int strength;
    };
    
    class Human: public Creature
    {
       public:
          Human(int a, int s) : Creature(a, s) {}
    };
    

    Note: you can make Creature constructor protected if you want only the derived classes to construct a Creature.

    If you want to access armor and str values you will have to add getter functions, since armor and strength are protected member variables.

    class Creature
    {
      public:
        Creature(int a, int s) : m_armor(a), m_strength(s) {};
    
        int armor() const     { return m_armor; }
        int strength() const  { return m_strength; }
    
    protected:
        int m_armor;
        int m_strength;
    };
    

    Now you can have your main() function:

    int main() 
    { 
      Human Male(30, 50);
      cout << Male.armor();
      cout << Male.strength();
      return 0;
    }
    
    0 讨论(0)
  • 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_){}     
    
    0 讨论(0)
  • 2021-01-21 09:09

    First, make sure that the base class has a constructor that can initialize its member variables properly.

    class Creature
    {
      public:
    
          Creature(int a, int b): armor(a), strength(b) {}
    
      private:
        int armor;
        int strength;
    };
    

    Unless the derived class has other member data that need to be initialized, you can use the following construct to be able to use the base class constructor like it is also defined in the derived class.

    class Human: public Creature
    {
       public:
          using Creature::Creature;
    };
    

    Now you can use:

    int main() 
    { 
      Human Male(30, 50);
      return 0;
    }
    

    Use of

    cout << Male.armor;
    cout << Male.strength;
    

    is not correct since armor and strength are private member variables of Creature. You should add couple of accessor functions and use them.

    class Creature
    {
       public:
       ...
    
       int getArmor() const { return armor; }
       int getStrength() const { return strength; }
    

    and then use them as:

    cout << Male.getArmor();
    cout << Male.getStrength();
    
    0 讨论(0)
提交回复
热议问题