【无标题】

发布于:2025-02-26 ⋅ 阅读:(93) ⋅ 点赞:(0)

作业

作业:
编写一个如下场景:
有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的set get 方法
编写一个 武器 Weapon 类,拥有私有成员攻击力,以及set get 方法
编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值,以及set get 方法
编写一个 匕首Blade类,继承自武器类,拓展属性 速度,以及set get 方法
编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力,以及set get 方法

武器Weapon类里面,要求有一个多态函数,叫做 equip 函数
英雄Hero类里面,要求有一个公开函数,equipWeapon(Weapon* w)
实现功能:英雄既可以装备长剑,也可以装备短剑,也可以装备斧头,但是要求装备不同的武器,英雄需要获得不同的属性加成

思路

类A中调用类B,类B中调用类A的解决办法:

代码

class Weapon;
/*********************** *********************** 英雄类 *********************** ***********************/
class Hero
{
private:
    int attack=5;     //攻击
    int defend=5;     //防御
    int speed= 5;      //速度
    int health=5;     //生命值
public:
    //设置 接口
    void set_attack(int index){ attack=index; }
    void set_defend(int index){ defend=index; }
    void set_speed (int index){ speed =index; }
    void set_health(int index){ health=index; }
    //获取 接口
    int get_attack(){ return attack; }
    int get_defend(){ return defend; }
    int get_speed (){ return speed;  }
    int get_health(){ return health; }

    //此时 Weapon类 还未声明 , 所以需要将Weapon提前声明,
    //这里 仅放声明,具体代码,放在在Weapon声明之后
    void equipWeapon(Weapon* w);  

    void show_attr()
    {
        cout << "attack = " << attack << "  ";
        cout << "defend = " << defend << "  ";
        cout << "speed  = " << speed  << "  ";
        cout << "health = " << health << endl;
    }
};


/*********************** *********************** 武器类 *********************** ***********************/
//武器类    基类
class Weapon
{
private:
    int attack;     //攻击
public:
    //构造
    Weapon():attack(5){}
    //接口
    void set_attack(int index){ attack=index; }
    int get_attack(){ return attack; }
    //装备
    virtual void equip(Hero* hero) = 0;     //基类 并且设置禁止调用
};

//长剑类    派生类
class Sword:public Weapon
{
private:
    int health;     //额外属性 生命值
public:
    //构造
    Sword():Weapon(),health(5){}
    //接口
    void set_health(int index){ health=index; }
    int get_health(){ return health; }
    //装备
    void equip(Hero* hero)
    {
        //英雄新属性
        int attack_new = hero->get_attack() + get_attack();
        int health_new = hero->get_health() + health;
        //设置新属性
        hero->set_attack(attack_new);
        hero->set_health(health_new);
    }
};

//匕首类    派生类
class Blade:public Weapon
{
private:
    int speed;      //额外属性 速度
public:
    //构造
    Blade():Weapon(),speed(5){}
    //接口
    void set_speed (int index){ speed =index; }
    int get_speed (){ return speed;  }
    //装备
    void equip(Hero* hero)
    {
        //英雄新属性
        int attack_new = hero->get_attack() + get_attack();
        int speed_new = hero->get_speed() + speed;
        //设置新属性
        hero->set_attack(attack_new);
        hero->set_speed(speed_new);
    }
};


//斧头类    派生类
class Axe:public Weapon
{
private:
    int defend;     //额外属性 防御
public:
    //构造
    Axe():Weapon(),defend(5){}
    //设置
    void set_defend(int index){ defend=index; }
    int get_defend(){ return defend; }
    //装备
    void equip(Hero* hero)
    {
        //英雄新属性
        int attack_new = hero->get_attack() + get_attack();
        int defend_new = hero->get_defend() + defend;
        //设置新属性
        hero->set_attack(attack_new);
        hero->set_defend(defend_new);
    }
};


int main(int argc,const char** argv)
{
    Hero man;
    man.show_attr();

    Weapon* weapon = new Sword;     //长剑
    cout << endl << "装备长剑" << endl;

    // Weapon* weapon = new Blade;     //匕首
    // cout << endl << "装备匕首" << endl;

    // Weapon* weapon = new Axe;       //斧头
    // cout << endl << "装备斧头" << endl;

    man.equipWeapon(weapon);
    man.show_attr();
	return 0;
}

// 声明完毕 class Weapon 之后,
// 此时能够识别 w->equip()
void Hero::equipWeapon(Weapon* w){ w->equip(this); }

效果


网站公告

今日签到

点亮在社区的每一天
去签到