当前位置: 首页 > wzjs >正文

聚来宝网站建设哈尔滨百度网络推广

聚来宝网站建设,哈尔滨百度网络推广,配资网站开发是什么意思,一分钟建设网站一、练习题 【编写一个如下场景】: 有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的set,get 方法; 编写一个 武器 Weapon 类&…

一、练习题
        【编写一个如下场景】:
        有一个英雄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); // 调用基类的 equiphero.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,生命值 +100Blade blade;blade.set_attack(15);blade.set_speed(10); // 匕首:攻击力 +15,速度 +10Axe 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;
}

http://www.dtcms.com/wzjs/172643.html

相关文章:

  • 响应式网站用什么做搜索引擎提交入口大全
  • 合肥建设集团信息网站建立自己的网站平台
  • 做网站的费用是多少手机优化软件哪个好用
  • 物流网站怎么做网站交易平台
  • 深圳房产 网站建设电商培训机构有哪些哪家比较好
  • 建设大型网站网络推广与推广
  • 武汉网站建设公司怎样精准搜索关键词
  • 手表大全网站怎么在百度上添加自己的店铺地址
  • 企业备案网站名称怎么填bing搜索引擎国际版
  • 做网站多少钱角西宁君博特惠西安关键词排名推广
  • 搜索引擎推广与优化seo关键字怎么优化
  • 虚拟电脑可以做网站吗seo优化关键词
  • 做彩票网站要多少钱网站怎么优化推广
  • 网站建设与运营课程视频剪辑培训班学费一般多少
  • win10 做网站服务器软件定制
  • 做网站违法嘛seo自动优化软件下载
  • 虚拟空间做网站推广策划方案
  • 做毕业证教育网站杭州关键词排名工具
  • 对亚马逊网站做简要分析与评价优化大师免费下载安装
  • 宁波专业网站推广平台便宜郑州百度seo网站优化
  • 微信公众号服务号网站开发流程图全网营销软件
  • 头条号链接其他网站怎么做云计算培训
  • 聊城做网站的公司资讯企业培训内容
  • b2c网站的主要功能域名历史查询工具
  • 直接用apk 做登陆网站今日头条网页版入口
  • 中国建设网官网网站seo难不难
  • 用什么网站可以做电子书2345浏览器网址
  • 网站开发技术要求网上的推广公司
  • 国务院 网站建设 文件百度网盘客服中心电话
  • 外贸平台哪个网站最好知乎广州seo站内优化