【问题标题】:C++ inheritance constructor problemsC++继承构造函数问题
【发布时间】:2021-06-24 09:35:24
【问题描述】:

我创建了 2 个类(EntityPlayer)。

所以基本上Player 类继承自Entity 类,所以Player 实例本身就是Entity 对象。

这是第一堂课:

#include <iostream>

#define print(obj) std::cout << obj << std::endl

class Entity {
    public:
        int xPos, yPos;

    Entity(int xInitPos, int yInitPos) {
        xPos = xInitPos;
        yPos = yInitPos;
    }

    void move(int _x, int _y) {
        xPos += _x;
        yPos += _y;
    }
};

第二类Player需要有一些额外的数据:

int healt;
int level;

这里是我困惑的部分,我不知道我是否应该为这个类指定一个新的构造函数,因为它需要获取2个额外的参数。

这是我到目前为止所做的:

class Player : public Entity {
    public:
        int healt;
        int level;
// I know that this piece of code is wrong
    Player(int _level, int _healt) {
        level = _level;
        healt = _healt;
    }
};

我是 C++ 编程新手,我不知道继承是如何工作的,我也不知道如何创建 Player 类的实体以及它的参数是什么需要。

这里是主要功能:

int main() {  
    
    Entity ent1 = Entity(0, 0);
    ent1.move(4, 8);

    Player player = Player(what attributes);

    return 0;
}

【问题讨论】:

  • 您可以在子构造函数中调用基本构造函数,例如Player(int _level, int _health) : Entity(0, 0), level(_level), health(_health) {} 如您所见,我使用 0, 0 作为 Entity 构造函数。因此,如果我想指定位置,我将不得不调整我的 Player 构造函数以也接受 xy 位置:Player(int _level, int _health, int x, int y) : Entity(x, y), level(_level), health(_health) {}

标签: c++ class object inheritance


【解决方案1】:

因为Entity 没有不带参数的默认构造函数,所以你也必须初始化它:

Player(int _level, int _healt) : Entity(0 , 0) {
    level = _level;
    healt = _healt;
}

或者更习惯的说法:

Player(int _level, int _healt) : Entity(0 , 0), healt(_healt), level(_level) {}

在上面的示例中,我默认将两个位置都初始化为 0,如果您想自己提供位置值,则必须有一个构造函数也接受这些值:

Player(int _level, int _healt, int posX, int posY) 
       : Entity(posX , posY), healt(_healt), level(_level) {}

然后这样称呼它:

Player player = Player(1, 100, 0, 0);

【讨论】:

    【解决方案2】:

    下面是一个例子:

    #include <iostream>
    
    class Entity {
        public:
            int xPos, yPos;
    
        Entity(int xInitPos, int yInitPos) {
            xPos = xInitPos;
            yPos = yInitPos;
        }
    
        void move(int _x, int _y) {
            xPos += _x;
            yPos += _y;
        }
    };
    
    class Player : public Entity {
        public:
            int health;
            int level;
    // Constructor that also accepts x and y which are passed on to the base constructor
    // The syntax with the : and , separated values is a initializer list.
        Player(int x, int y, int _level, int _health)
        : Entity(x, y)
        , level(_level)
        , health(_health) {}
    
    // Constructor without x and y, passing 0 for x and y to the base constructor
        Player(int _level, int _health)
        : Entity(0,0)
        , level(_level)
        , health(_health) {}
    };
    
    int main() {
        Player p(2, 3, 1, 100); // Creates a player at position (2,3) with level 1 and 100 health
        Player p2(2, 110); // Creates a player at position (0,0) and level 2 and 110 health
        std::cout << p.xPos << '\n';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 2019-01-21
      • 2020-02-10
      相关资源
      最近更新 更多