代码如下:

#include<iostream>
#include<memory>
struct Food
{

};
class Animal
{
public:
    Animal(int HP, int power) :m_HP(HP), m_power(power) {}
    virtual void eatFood(std::shared_ptr<Food> food);
    virtual void attack(std::shared_ptr<Animal> animal);
    int getHP() const
    {
        return m_HP;
    }
    int getPower()const
    {
        return m_power;
    }
protected:
    int m_HP;
    int m_power;
};

class People;
class Pet :public Animal
{
public:
    Pet(int HP, int power) :Animal(HP, power) {};
    virtual void eatFood(std::shared_ptr<Food> food);
    virtual void attack(std::shared_ptr<Animal> animal);
    void addPeopleHP(People* people);
};

class People :public Animal
{
public:
    People(int HP, int power) :Animal(HP, power) {};
    virtual void eatFood(std::shared_ptr<Food> food);
    virtual void attack(std::shared_ptr<Animal> animal);
    void addHP()
    {
        if (petPtr == nullptr)
        {
            return;
        }
        petPtr->addPeopleHP(this);
    }
private:
    void setHP(int HP)
    {
        m_HP = HP;
    }
private:
    std::shared_ptr<Pet> petPtr;//Pet设计为指针,当people的对象进行拷贝或者移动可以减少消耗,并且不需要直接计算
};

int main()
{
    return 0;
}

其中addPeopleHP的方法不能在头文件声明里直接实现,要在cpp里,引用people.hpp然后再实现