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

c++作业7

 

模拟一个游戏场景
    有一个英雄:初始所有属性为1
    atk,def,apd,hp

    游戏当中有以下3种武器
    长剑Sword: 装备该武器获得 +1atx,+1def
    短剑Blade: 装备该武器获得 +1atk,+1spd
    斧头Axe:   装备该武器获得 +1atk,+1hp

    要求英雄写一个函数:
    equipweapon 装备武器
    实现效果:装备不同武器英雄获得不同的属性加成

/*有一个英雄:初始所有属性为1
atk,def,spd,hp
游戏当中有以下3种武器
长剑Sword:装备该武器获得+1atk,+1def
短剑Blade:装备该武器获得+1atk,+1spd
斧头Axe:装备该武器获得+1atk,+1hp*/
#include <iostream>using namespace std;
class Hero;
class Weapon
{
public:int atk;Weapon():atk(1){}virtual void update(Hero &h);int get_atk1();virtual ~Weapon(){}
};class Sword:public Weapon
{int def;
public:Sword():def(1){}void update(Hero &h);
};class Blade:public Weapon
{int spd;
public:Blade():spd(1){}void update(Hero &h);
};class Aex:public Weapon
{int hp;
public:Aex():hp(1){}void update(Hero &h);
};class Hero
{int atk;int def;int spd;int hp;public:Hero():atk(1),def(1),spd(1),hp(1){}void set_atk(int atk);void set_def(int def);void set_spd(int spd);void set_hp(int hp);int get_atk();int get_def();int get_spd();int get_hp();void updateWeapon(Weapon *w);void show();
};
void Hero::set_atk(int atk)
{this->atk=atk;
}
void Hero::set_def(int def)
{this->def=def;
}
void Hero::set_spd(int spd)
{this->spd=spd;
}
void Hero::set_hp(int hp)
{this->hp=hp;
}
int Hero::get_atk()
{return this->atk;
}
int Hero::get_def()
{return this->def;
}
int Hero::get_spd()
{return this->spd;
}
int Hero::get_hp()
{return this->hp;
}
void Weapon::update(Hero &h)
{int new_atk=h.get_atk()+this->atk;h.set_atk(new_atk);
}void Sword::update(Hero &h)
{Weapon::update(h);int new_def=h.get_def()+this->def;h.set_def(new_def);
}void Blade::update(Hero &h)
{Weapon::update(h);int new_spd=h.get_spd()+this->spd;h.set_spd(new_spd);
}void Aex::update(Hero &h)
{Weapon::update(h);int new_hp=h.get_hp()+this->hp;h.set_hp(new_hp);
}
void Hero::updateWeapon(Weapon *w)
{w->update(*this);delete w;
}
void Hero::show()
{cout<<"atk="<<atk<<endl;cout<<"def="<<def<<endl;cout<<"spd="<<spd<<endl;cout<<"hp="<<hp<<endl;
}
int main()
{Hero h1,h2,h3;h1.updateWeapon(new Sword);h2.updateWeapon(new Blade);h3.updateWeapon(new Aex);h1.show();h2.show();h3.show();return 0;
}

http://www.lryc.cn/news/504936.html

相关文章:

  • vue 上传组件 vxe-upload 实现拖拽调整顺序
  • Windows 环境实战开源项目GFPGAN 教程
  • UE5 做简单的风景观光视频
  • k8s服务搭建与实战案例
  • JavaScript学习难点
  • Qt WORD/PDF(一)使用 QtPdfium库实现 PDF 预览
  • 解决创建laravel项目,使用国外镜像超时,国内镜像缺包的问题
  • Java泛型设计详解
  • 用ue5打开网址链接
  • 【大数据】-- 读放大和写放大
  • 【前端】JavaScript 抽取字符串特定部分题目详解与实现思路
  • CNCF云原生生态版图-分类指南(一)- 观测和分析
  • 热更新解决方案3 —— xLua
  • 如何让ai在游戏中更像一个人?
  • websocket_asyncio
  • 如何在NGINX中实现基于IP的访问控制(IP黑白名单)?
  • Y3编辑器文档4:触发器1(界面及使用简介、变量作用域、入门案例)
  • echarts图表自定义配置(二)——代码封装
  • 02、10个富士胶片模拟的设置
  • 鸿蒙系统-前端0帧起手
  • 211-基于FMC的1路1.5G ADC 1路 2.5G DAC子卡
  • 获取微信用户openid
  • MultiRECloudSim使用
  • 智能设备安全-固件逆向分析
  • 【小白包会的】使用supervisor 管理docker内多进程
  • 使用navicat新旧版本,连接PostgreSQL高版本报错问题图文解决办法
  • IDEA 未启用lombok插件的Bug
  • 认识GO--gRPC的metadata
  • 2024年安徽省职业院校技能大赛信息安全管理与评估
  • Perl 引用