C++ day6

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

        武器 Weapon 类中,要求有一个多态函数,叫做 equip 函数;
        英雄 Hero 类中,要求有一个公开函数,equipWeapon(Weapon* w);
        

        【实现功能】:

        英雄既可以装备长剑,也可以装备匕首,也可以装备斧头。但是要求装备不同的武器后,英雄需要获得不同的属性加成。


【代码】:

#include <iostream>
using namespace std;

class Hero {
private:
    int attack;      // 攻击力
    int defense;     // 防御力
    int speed;       // 速度
    int health;      // 生命值

public:
    Hero() : attack(0), defense(0), speed(0), health(0) {}

    int get_attack() const { return attack; }
    int get_defense() const { return defense; }
    int get_speed() const { return speed; }
    int get_health() const { return health; }

    void set_attack(int a) { attack = a; }
    void set_defense(int d) { defense = d; }
    void set_speed(int s) { speed = s; }
    void set_health(int h) { health = h; }

    // 装备武器
    void equipWeapon(class Weapon* w);
};

// 武器基类
class Weapon {
private:
    int attack;

public:
    Weapon() : attack(0) {}

    int get_attack() const { return attack; }
    void set_attack(int a) { attack = a; }

    // 多态函数:装备武器
    virtual void equip(Hero& hero) {
        hero.set_attack(hero.get_attack() + attack);
    }
};

// 长剑类
class Sword : public Weapon {
private:
    int health;

public:
    Sword() : health(0) {}
    int get_health() const { return health; }
    void set_health(int h) { health = h; }

    // 重写 equip 函数
    void equip(Hero& hero) {
        Weapon::equip(hero); // 调用基类的 equip
        hero.set_health(hero.get_health() + health);
    }
};

// 匕首类
class Blade : public Weapon {
private:
    int speed;

public:
    Blade() : speed(0) {}
    int get_speed() const { return speed; }
    void set_speed(int s) { speed = s; }

    void equip(Hero& hero) {
        Weapon::equip(hero);
        hero.set_speed(hero.get_speed() + speed);
    }
};

// 斧头类
class Axe : public Weapon {
private:
    int defense;

public:
    Axe() : defense(0) {}
    int get_defense() const { return defense; }
    void set_defense(int d) { defense = d; }

    void equip(Hero& hero) {
        Weapon::equip(hero);
        hero.set_defense(hero.get_defense() + defense);
    }
};

// Hero 类的 equipWeapon 函数实现
void Hero::equipWeapon(Weapon* w) {
    w->equip(*this); // 调用武器的 equip 函数
}

int main() {
    Hero hero;
    hero.set_attack(100);
    hero.set_defense(50);
    hero.set_speed(30);
    hero.set_health(500);

    cout << "初始状态:" << endl;
    cout << "攻击力: " << hero.get_attack() << endl;
    cout << "防御力: " << hero.get_defense() << endl;
    cout << "速度: " << hero.get_speed() << endl;
    cout << "生命值: " << hero.get_health() << endl;
    cout << "-------------------" << endl;

    // 创建武器
    Sword sword;
    sword.set_attack(20);
    sword.set_health(100); // 长剑:攻击力 +20,生命值 +100

    Blade blade;
    blade.set_attack(15);
    blade.set_speed(10); // 匕首:攻击力 +15,速度 +10

    Axe axe;
    axe.set_attack(30);
    axe.set_defense(20); // 斧头:攻击力 +30,防御力 +20

    // 装备长剑
    hero.equipWeapon(&sword);
    cout << "装备长剑后:" << endl;
    cout << "攻击力: " << hero.get_attack() << endl;
    cout << "防御力: " << hero.get_defense() << endl;
    cout << "速度: " << hero.get_speed() << endl;
    cout << "生命值: " << hero.get_health() << endl;
    cout << "-------------------" << endl;

    // 装备匕首
    hero.equipWeapon(&blade);
    cout << "装备匕首后:" << endl;
    cout << "攻击力: " << hero.get_attack() << endl;
    cout << "防御力: " << hero.get_defense() << endl;
    cout << "速度: " << hero.get_speed() << endl;
    cout << "生命值: " << hero.get_health() << endl;
    cout << "-------------------" << endl;

    // 装备斧头
    hero.equipWeapon(&axe);
    cout << "装备斧头后:" << endl;
    cout << "攻击力: " << hero.get_attack() << endl;
    cout << "防御力: " << hero.get_defense() << endl;
    cout << "速度: " << hero.get_speed() << endl;
    cout << "生命值: " << hero.get_health() << endl;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值