/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:郭永恒
*完成日期:2016年4月6日
*版 本 号:v1.0
*
*问题描述:在第五周上机时间项目“游戏中的角色类”上拓展,添加一个武器类
*/
#include <iostream>
#include <string>
class Weapon//武器
{
public:
Weapon(std::string tname = " ",int tforce = 0,int tweight = 0,int tlength = 0)//构造函数
:name(tname),force(tforce),weight(tweight),length(tlength){}
void SetData()
{
std::cout << "请输入武器的名字、威力、重量、长度:" << std::endl;
std::cin >> name >> force >> weight >> length;
}
void ShowData(){std::cout << "武器名称:" << name << "\n武器威力:" << force << "\n武器重量:" << weight << "\n武器长度:" << length << std::endl;}
int suck_blood(){return force;}
~Weapon(){}//析构函数
private:
std::string name = " ";//武器名称
int force = 0;//武器威力
int weight = 0;//武器质量
int length = 0;//武器长度
};
class Role
{
public:
Role(std::string nam = " ",int blo = 0, std::string tname = " ",int tforce = 0,int tweight = 0,int tlength = 0):
name(nam),blood(blo),arm(tname,tforce,tweight,tlength){life = blo > 0 ? true : false;}
~Role(){std::cout << name << "退出江湖..." << std::endl;}
void setRole();
void show();
void attack(Role& Rname);
void eat(int food);
int RtRoforce(){return arm.suck_blood();}
void beAttack(Role& Rname);
private:
std::string name;
int blood;
bool life;
Weapon arm;
};
void Role::setRole()
{
std::cout << "请输入角色名称、血量:"<< std::endl;
std::cin >> name >> blood;
life = blood > 0 ? true : false;
arm.SetData();
}
void Role::show()
{
std::cout << name << " has" << blood << " blood,it is " << (blood > 0 ? "alived." : "died.") << std::endl;
arm.ShowData();
}
void Role::attack(Role& Rname)
{ //被攻击血量 被攻击血量 武器威力
Rname.blood = Rname.blood - RtRoforce();
}
void Role::beAttack(Role& Rname)
{
blood = blood - Rname.RtRoforce();
}
void Role::eat(int food){blood = blood + food;}
int main()
{
Role Jack;
Role Mark;
Jack.setRole();
Mark.setRole();
Jack.show();
Mark.show();
Jack.attack(Mark);
Mark.beAttack(Jack);
Jack.show();
Mark.show();
return 0;
}
运行结果: