第6周项目2-角色武器装备
2016-04-14 08:58
162 查看
/* *Copyright(C) 2016,计算机与控制工程学院 *All rights reserved. *文件名:test.cpp *作者:张志新 *完成日期:2016年4月10日 *版本号:v1.0 * *问题描述:带武器的游戏类。 */ #include <iostream> #include <string> using namespace std; class Weapon { public: Weapon(string weap,int f); int getForce(); private: string wName; int force; }; Weapon::Weapon (string weap, int f):wName(weap),force(f) { } int Weapon::getForce() { return force; } class Role { public: Role(string nm,int xie,string w,int f); ~Role(); void show(); void attack(Role &r); void eat(int food); bool isAlived(); private: Weapon weapon; string name; int blood; bool life; }; Role::Role(string nm,int xie,string weap,int f):name(nm),blood(xie),weapon(weap,f) { if(blood>0) life=true; else life=false; } Role::~Role() { cout<<name<<"退出江湖!"<<endl; } bool Role::isAlived() { if(blood>0) return true; else return false; } void Role::show() { cout<<name<<" blood is "<<blood<<" he is "; if(isAlived()) { cout<<"alived"<<endl; } else { cout<<"dead"<<endl; } } void Role::attack(Role &r) { if(isAlived()) { blood+=weapon.getForce(); r.blood-=weapon.getForce(); if(r.blood<=0) r.life=false; } } void Role::eat(int food) { if(isAlived()) blood=blood+food; } int main( ) { Role qiangu("花千骨", 100, "断念",1000); Role mantian("霓漫天", 100, "屠龙", 500); qiangu .show(); mantian.show(); cout<<"霓漫天攻击花千骨"<<endl; mantian.attack(qiangu); qiangu.show(); mantian.show(); cout<<"花千骨攻击霓漫天"<<endl; qiangu.attack(mantian); qiangu.show(); mantian.show(); cout<<"end......"<<endl; return 0; }
相关文章推荐
- Eclipse启动正常,但在打开工程时会出未响应的情况
- linux中关闭报警音
- c++中vector用法
- Linux下缓冲区溢出攻击的原理及对策
- 项目2-有元类
- 45倍加速Spark的处理效率?!
- de_bruijn序列的一些补充
- c语言中的未定义行为
- Android系统介绍与框架
- 【chgrp】修改文件属组
- 【chage】修改账号或密码的有效期限
- 模拟实现字符串操作函数(strcpy,strstr,strcat,strcmp,strlen)与内存操作函数(memcpy,memmove)
- Maven3--基本配置--01
- Effective STL 条款8
- 【groupadd】 创建组
- 【gpasswd】给组加密码
- iOS之AFN文件上传(4)
- 问题:iis配置json;结果:如何配置iis支持.json格式的文件
- 【Android】Intent的显式隐式用法
- StreamDM:基于Spark Streaming、支持在线学习的流式分析算法引擎