第七周项目3 角色类组合多文件
2016-04-13 19:22
169 查看
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></span><pre name="code" class="cpp">/* *Copyright(c) 2016,烟台大学计算机学院 *All rights reserved. *作 者:刘金石 *完成日期:2016年4月10日 *版本 号:v1.0 *问题描述:设计游戏中的角色类,并把武器作为一个单独的类,使用多文件处理 *输入描述:角色。 *输出描述:输出模拟战斗过程。 */
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">game.h //头文件</span>
#ifndef GAMESS_H_INCLUDED #define GAMESS_H_INCLUDED using namespace std; class Weapon { string name;//武器名字 int force;//武器威力 public: Weapon(string mname,int mforce):name(mname),force(mforce){}//构造函数 int getcomForce();//获取普通攻击数值 int getbigForce();//获取大招攻击数值 }; class Role { public: Role(string jsname,int xblood,string wqname,int wqf);//角色类的构造函数 ~Role();//析构函数 void show(); bool isAlived(); void comattack(Role &r);//普通攻击 void finalhit(Role &r);//大招攻击 void eat(int bloo);//吃东西,获取血量 private: string name;//角色名字 int blood; bool life;//角色状态 Weapon weapon; }; #endif // GAMESS_H_INCLUDEDrole.cpp:定义角色类的成员函数
#include<iostream> #include"gamess.h" using namespace std; bool Role::isAlived() //是否活着 { return life; } Role::Role(string jsname,int xblood,string wqname,int wqf):name(jsname),blood(xblood),weapon(wqname,wqf) { if(blood>0) life=true; else life=false; } Role::~Role() { cout<<name<<"退出江湖..."<<endl; } void Role::comattack(Role &r) { cout<<r.name<<" "<<"was"<<" "<<"be"<<" "<<"attack"<<endl; if(isAlived()) { //blood+=weapon.getcomForce(); r.blood-=weapon.getcomForce(); if(r.blood<=0) r.life=false; } } void Role::finalhit(Role &r) { cout<<r.name<<" "<<"was"<<" "<<"be"<<" "<<"attack"<<endl; if(isAlived()) { //blood+=weapon.getbigForce(); r.blood-=weapon.getbigForce(); if(r.blood<=0) r.life=false; } } void Role::show() //显示 { cout<<name<<" has "<<blood<<" blood, it is "; if(isAlived()) cout<<"alived."; else cout<<"dead."; cout<<endl; } void Role::eat(int blo) { blood=blood+blo; cout<<name<<" "<<"eat"<<" "<<"something,"<<"blood"<<" "<<"is"<<" "<<blood<<endl; }
weapon.cpp:武器类的成员函数的实现
#include<iostream> #include"gamess.h" using namespace std; int Weapon::getcomForce() { return force; } int Weapon::getbigForce() { return (force*2); }
main.cpp//测试文件
#include<iostream> #include"gamess.h" using namespace std; int main() { cout<<"---begin---"<<endl; Role mary("Mary",4,"yitian",2); Role jack("Jack",6,"tulong",3); mary.show(); jack.show(); cout<<"---1st round---"<<endl; mary.comattack(jack); mary.eat(2); mary.show(); jack.show(); cout<<"---2nd round---"<<endl; jack.finalhit(mary); mary.show(); jack.show(); cout<<"---end---"<<endl; return 0; }运行结果:
相关文章推荐
- Hbase总结(六)hbase37个笔试题
- 14. Longest Common Prefix (截取字符串)
- 【Android】Mainifest文件手动添加Activity
- Spring事务之如何保证同一个Connection对象
- QT读取access是数据库表名中文乱码
- 内容提供者 ContentProvide简单示例
- MyBatis-Spring-TransactionManager
- C++多态
- BaiduSdk3.0之后无法使用BMapManager的问题
- 团队项目会议第一次
- 【C#】delegate(委托) 将方法作为参数在类class 之间传递
- 《linux内核设计与实现》第四章
- 拾遗与填坑《深度探索C++对象模型》3.2节
- ArcGIS Engine 10.2开发环境搭建
- 拾遗与填坑《深度探索C++对象模型》3.2节
- ubuntu nfs挂载
- 电脑usb负载过多,导致usb供电不足的问题
- 倒计时三天——13:人民币支付
- HA专题: Corosync+Pacemaker+drbd实现MySQL高可用 推荐
- HDU 4971 A simple brute force problem. 最小割 最大权闭合