项目3——多文件组织游戏类

问题及代码:

game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include<iostream>
using namespace std;
class Weapon
{
public:
    Weapon(string wname,int f);
    int getfocer();
private:
    string Wname;
    int focer;
};
class Role
{
public:
    Role(string na,int n,string wname, int f);
    ~Role();
    void show();
    void attack(Role &r);
    void eat(int n);
    int isLived();
private:
    string name;
    int blood;
    bool life;
    Weapon weapon;
};
#endif // GAME_H_INCLUDED

weapon.cpp

#include<iostream>
#include"game.h"
using namespace std;
Weapon::Weapon(string wname,int f)
{
    Wname=wname;
    focer=f;
}
int Weapon::getfocer()
{
    return focer;
}

role.cpp

#include<iostream>
#include"game.h"
using namespace std;
Role::Role(string na,int n,string wname, int f):name(na),blood(n),weapon(wname,f){}
Role::~Role()
{
    cout<<name<<"退出江湖。。"<<endl;
}
void Role::show()
{

   if(isLived())
    {
        cout<<name<<" has "<<blood<<",he is lived"<<endl;
    }
    else
        cout<<"he is died"<<endl;
}
void Role::attack(Role &r)
{
    blood+=weapon.getfocer();
    r.blood-=weapon.getfocer();
    if(r.blood>0)
        life=1;
    else
        life=0;
}
void Role::eat(int n)
{
    blood=blood+n;
}
int Role::isLived()
{
    if(blood>0)
        life=1;
    else
        life=0;
    return life;
}


main.cpp

/*copyright(c)2016.烟台大学计算机学院
 * All rights reserved,
 * 文件名称:text.Cpp
 * 作者:李一波
 * 完成日期:2016年4月17日
 * 版本号:vc++6.0
 *
 * 问题描述: Date Time类
 * 输入描述:
 * 程序输出: 输出结果
 */
#include<iostream>
#include"game.h"
using namespace std;
int main( )
{
    Role mary("Mary",4,"yitian",2);
    Role jack("Jack",4,"tulong",2);
    mary.attack(jack);
    mary.eat(2);
    jack.show();
    mary.show();
    return 0;
}

运行结果:

项目3——多文件组织游戏类_第1张图片

你可能感兴趣的:(项目3——多文件组织游戏类)