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

11.25c++继承、多态

练习:

编写一个 武器类
class Weapon{int atk;
}编写3个武器派生类:短剑,斧头,长剑
class knife{int spd;
}class axe{int hp;
}class sword{int def;
}编写一个英雄类
class Hero{int atk;int def;int spd;int hp;
public:所有的get set 方法void equipWeapon(Weapon*)根据传入的武器不同,英雄获得不同的属性加成
}

代码实现: 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <myhead.h>
using namespace std;  class Weapon;
class Hero{
private:int atk;int def;int spd;int hp;
public:Hero(int atk=0,int def=0,int spd=0,int hp=0):spd(spd),hp(hp),def(def),atk(atk){}void setAtk(int atk){this->atk=atk;	}void setSpd(int spd){this->spd=spd;}void setHp(int hp){this->hp=hp;}void setDef(int def){this->def=def;}int getAtk(){return this->atk;}int getSpd(){return this->spd;}int getHp(){return this->hp;}int getDef(){return this->def;}void equipweapon(Weapon* w);void showProptery(){cout << "h p=" << hp << endl;cout << "spd=" << spd << endl;cout << "def=" << def << endl;cout << "atk=" << atk << endl;cout << "-------------" << endl;}
};class Weapon{
private:int atk;
public:Weapon(int atk=0):atk(atk){}void setAtk(int atk){this->atk=atk;}int getAtk(){return this->atk;}virtual void addProptery(Hero& hero){}
};
class Knife:public Weapon{
private:int spd;
public:Knife(int atk=1,int spd=1):Weapon(atk),spd(spd){}void setSpd(int spd){this->spd=spd;}int getSpd(){return this->spd;}virtual void addProptery(Hero& hero){int atk=hero.getAtk()+this->getAtk();int spd=hero.getSpd()+this->spd;hero.setAtk(atk);hero.setSpd(spd);}};
class Axe:public Weapon{
private:int hp;
public:Axe(int atk=1,int hp=1):Weapon(atk),hp(hp){}void setHp(int hp){this->hp=hp;}int getHp(){return this->hp;}virtual void addProptery(Hero& hero){int atk=hero.getAtk()+this->getAtk();int hp=hero.getHp()+this->hp;hero.setAtk(atk);hero.setHp(hp);}
};
class Sword:public Weapon{
private:int def;
public:Sword(int atk=1,int def=1):Weapon(atk),def(def){}void setDef(int def){this->def=def;}int getDef(){return this->def;}virtual void addProptery(Hero& hero){int atk=hero.getAtk()+this->getAtk();int def=hero.getDef()+this->def;hero.setAtk(atk);hero.setDef(def);}
};
void Hero::equipweapon(Weapon* w){w->addProptery(*this);
}int main(int argc,const char **argv){Hero h1,h2,h3;Knife k;Sword s;Axe a;h1.equipweapon(&k);h2.equipweapon(&s);h3.equipweapon(&a);h1.showProptery();h2.showProptery();h3.showProptery();return 0;
}

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

相关文章:

  • STM32F103外部中断配置
  • 阿里电商大整合,驶向价值竞争新航道
  • 等保测评在云计算方面的应用讲解
  • QML TableView 实例演示 + 可能遇到的一些问题(Qt_6_5_3)
  • SpringBoot(三十九)SpringBoot集成RabbitMQ实现流量削峰添谷
  • 前端 Vue 3 后端 Node.js 和Express 结合cursor常见提示词结构
  • 类和对象(下):点亮编程星河的类与对象进阶之光
  • 42.接雨水
  • 使用Java代码操作Kafka(五):Kafka消费 offset API,包含指定 Offset 消费以及指定时间消费
  • Ubuntu安装不同版本的opencv,并任意切换使用
  • 突破内存限制:Mac Mini M2 服务器化实践指南
  • 【排版教程】Word、WPS 分节符(奇数页等) 自动变成 分节符(下一页) 解决办法
  • 【在Linux世界中追寻伟大的One Piece】多线程(二)
  • flink学习(8)——窗口函数
  • 「实战应用」如何用图表控件LightningChart .NET实现散点图?(一)
  • 鸿蒙Native使用Demo
  • 29.UE5蓝图的网络通讯,多人自定义事件,变量同步
  • Scala—列表(可变ListBuffer、不可变List)用法详解
  • 【论文复现】偏标记学习+图像分类
  • C嘎嘎探索篇:栈与队列的交响:C++中的结构艺术
  • AIGC-----AIGC在虚拟现实中的应用前景
  • Django 路由层
  • 《硬件架构的艺术》笔记(八):消抖技术
  • Spring 与 Spring MVC 与 Spring Boot三者之间的区别与联系
  • 【算法】连通块问题(C/C++)
  • 如何选择黑白相机和彩色相机
  • Rust 力扣 - 740. 删除并获得点数
  • OpenCV从入门到精通实战(七)——探索图像处理:自定义滤波与OpenCV卷积核
  • Docker核心概念总结
  • 环形缓冲区