C++11中的随机数与模板函数,模板类 ゝ一纸荒年。 2022-05-27 02:17 152阅读 0赞 **本篇博客涉及C++11中的随机数与模板函数,模板类** **下面有详细解释** ** if(!a),表示a为假时执行后续语句** 例如: bool Mouse::isAlive() \{ if (hp <= 0) return false; else return true; \} if (!mouse.isAlive()) break; 下面是猫鼠互打代码 class Cat:增加20%概率连击的攻击技能 class Mouse:增加80%概率回避的防御技能 class Goat,以及10%概率一击必杀的攻击技能,自行修改main函数以测试功能 \#include<iostream> \#include<random> \#include<string> using namespace std; class Cat; class Mouse \{ private: string name; int hp, atk; public: Mouse(string, int, int); string getName(); void showIntro(); void destroy(); void reduceHP(int); template<typename T> void attack(T &); bool isAlive(); \}; class Cat \{ private: string name; int hp, atk; public: Cat(string, int, int); string getName(); void showIntro(); void destroy(); void reduceHP(int); **template<typename T> void attack(T &); //声明处也必须用template<typename T>** bool isAlive(); \}; **template<typename T> class Goat** \{ private: T name1, name2; public: Goat(T name1, T name2) :name1(name1), name2(name2) \{ cout << name1 << "一击杀死" << name2 << endl; \} \}; Mouse::Mouse(string name1, int hp1, int atk1) \{ name = name1; hp = hp1; atk = atk1; \} void Mouse::showIntro() \{ cout << name << "\\t血量:" << hp << "\\t攻击力:" << atk << endl; \} void Mouse::destroy() \{ hp = 0; cout << name << "被消灭" << endl; \} void Mouse::reduceHP(int r) \{ random\_device rd; int avoid = rd() % 101 + 1; if (avoid > 80) \{ cout << "躲避失败" << name << "受到了" << r << "点伤害" << endl; hp -= r; if (hp <= 0) destroy(); \} else if (avoid <= 80) \{ cout << name << "躲避成功" << endl; \} \} **template<typename T> void Mouse::attack(T &enemy) //声明函数模板与声明类模板不同** \{ cout << name << "对" << enemy.getName() << "发起了攻击,"; enemy.reduceHP(atk); \} bool Mouse::isAlive() \{ if (hp <= 0) return false; else return true; \} string Mouse::getName() \{ return name; \} Cat::Cat(string name1, int hp1, int atk1) \{ name = name1; hp = hp1; atk = atk1; \} void Cat::showIntro() \{ cout << name << "\\t血量:" << hp << "\\t攻击力:" << atk << endl; \} void Cat::destroy() \{ hp = 0; cout << name << "被消灭" << endl; \} void Cat::reduceHP(int r) \{ cout << name << "受到了" << r << "点伤害" << endl; hp -= r; if (hp <= 0) destroy(); \} **template<typename T> void Cat::attack(T &enemy)** \{ random\_device rd; int a; a = rd() % 101 + 1; cout << name << "对" << enemy.getName() << "发起了攻击,"; enemy.reduceHP(atk); if (a < 20) \{ cout << name << "再次发起攻击" << endl; enemy.reduceHP(atk); \} \} bool Cat::isAlive() \{ if (hp <= 0) return false; else return true; \} string Cat::getName() \{ return name; \} int main() \{ Cat cat("Tom", 100, 20); Mouse mouse("Jerry", 50, 10); random\_device rd, rb; int a, b; a = rd() % 101 + 1; b = rb() % 101 + 1; while (cat.isAlive() && mouse.isAlive()) \{ cat.showIntro(); mouse.showIntro(); cat.attack(mouse); if (!mouse.isAlive()) break; mouse.attack(cat); if (a <= 10) \{ **Goat<string> x("Tom", "Jerry"); //类模板的不同** break; \} if (b <= 10) \{ **Goat<string> x("Jerry", "Tom");** break; \} cout << endl; \} return 0; \} 下面详细讲下模板定义 定义方法类似于模板函数 template<typename T> class ClassName \{ … \}; 当用到多个模板参数时 template<typename T1, typename T2> 当一个模板类形如以下定义时 template<typename T> class ClassName \{ … \}; 使用该模板类定义实体或者实体指针需要这样定义 ClassName<具体的类型> ObjectName; ClassName<具体的类型> \*ObjectPointer; 具体的类型例如int、double等类型或者自定义的class,表示用该具体的类型替代模板类定义中的T。 例:定义一个模板类,属性为两个某种相同类型数据,两个方法分别返回其中的较大值和较小值 \#include<iostream> using namespace std; template<typename T> class Compare \{ private: T x, y; public: Compare(T a, T b) \{ x = a; y = b; \} T getMax() \{ return (x > y) ? x : y; \} T getMin() \{ return (x < y) ? x : y; \} \}; int main() \{ Compare<int> c(8, 9); cout << "较大数是:" << c.getMax() << endl; cout << "较小数是:" << c.getMin() << endl; return 0; \} **使用模板类定义实体时,需要明确规定模板使用的类型,例如Compare<int> 表示Compare类中的T用int替换**。 下篇发外联如何定义及模板类节点和模板链表类 **麻烦大佬点个赞或评论下,谢谢!**
还没有评论,来说两句吧...