/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:刘涛 *完成日期:2016年4月7日 *版 本 号:v1.0 * *问题描述:在第五周上机时间项目“游戏中的角色类”上拓展,添加一个武器类 1 */
#include <iostream> using namespace std; class Weapon { public: Weapon(string wnam, int f); int getForce(); void setdata(); void showdata(); private: string wname; //名称 int force; //威力 }; Weapon::Weapon(string wnam, int forc):wname(wnam),force(forc) {} int Weapon::getForce() { return force; } void Weapon::setdata() { std::cout << "请输入武器的名字、威力:" << std::endl; std::cin >> wname >> force; } void Weapon::showdata() { std::cout<<"武器名称 "<<wname <<"威力 "<<force<<std::endl;
} class Role { public: Role(string name,int blo,int ran,string nati,string se,string wnam,int forc);//构造函数 ~Role(); void show(); void attack(Role&r); void eat(int medicine); void beAttack(Role&r); void range1(); private: string name; int blood; bool life; int range; string nation; string sex; Weapon weapon; }; Role::Role(string nam,int blo,int ran,string nati,string se,string wnam,int forc):name(nam),blood(blo),range(ran),nation(nati),sex(se),weapon(wnam,forc) { if(blood>0) life=true; else life=false; } Role::~Role() { std::cout<<name<<"已经退出江湖..."<<std::endl; }
void Role::show() { cout<<name << "has" << blood << "blood " <<range << "级 " <<nation << "族 " <<sex <<endl; if(blood>0) life=true; else life=false; weapon.showdata(); } void Role::attack(Role &r) { blood+=weapon.getForce(); r.blood-=weapon.getForce(); if(r.blood<=0) r.life=false; } void Role::beAttack(Role&r) { blood-=weapon.getForce(); r.blood+=weapon.getForce(); if(blood<=0) life=false; } void Role::eat(int medicine) { blood+=medicine; } void Role::range1() { if(blood>=10) range+=1; } int main() { Role James("james",8,2,"east","Man","TULONG",2); Role Curry("curry",7,3,"west","Feman","YITIAN",3); James.show(); Curry.show(); Curry.attack(James); James.beAttack(Curry); James.eat(5); James.attack(Curry); James.range1(); Curry.range1();
James.show(); Curry.show(); return 0;
}
运行及结果:
<img src="https://img-blog.csdn.net/20160407210221483?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />