第六周 用多文件组织多个类的程序(带武器的角色类)

本文介绍了一个简单的游戏程序设计案例,其中包括角色(Role)和武器(Weapon)两个类的定义与实现。通过这两个类,实现了角色间的攻击行为,并展示了如何在主函数中使用这些类来模拟战斗场景。
  1. #ifndef SADF  
  2. #define SADF  
  3. #include<string>  
  4. using namespace std;  
  5.   
  6. class Weapon  
  7. {  
  8. public:  
  9.     Weapon(string wnam, int f);  
  10.     int getForce();  
  11. private:  
  12.     string wname;   //名称  
  13.     int force;       //威力  
  14. };  
  15. #endif  
  16. /*因为重复包含头文件, 
  17. 所以要在开头结尾加上#ifndef SADF  
  18.   #define SADF    #endif 
  19. */  
  20. #ifndef SADF
    #define SADF
    #include<string>
    using namespace std;
    
    class Weapon
    {
    public:
        Weapon(string wnam, int f);
        int getForce();
    private:
        string wname;   //名称
        int force;       //威力
    };
    #endif
    /*因为重复包含头文件,
    所以要在开头结尾加上#ifndef SADF 
      #define SADF    #endif
    */

//文件2,类的定义,Role.h

  1. #include"Weapon.h"  
  2. class Role  
  3. {  
  4. public:  
  5.     Role(string nam, int b, string wnam, int f); //构造函数  
  6.     ~Role(); //析构函数  
  7.     void eat(int d); //吃东西,涨d血  
  8.     void attack(Role &r); //攻击别人,自己涨血,同时失血  
  9.     bool isAlived(); //是否活着  
  10.     void show(); //显示  
  11. private:  
  12.     string name;  
  13.     int blood;  
  14.     Weapon weapon;  
  15.     bool life;  
  16. };  
#include"Weapon.h"
class Role
{
public:
    Role(string nam, int b, string wnam, int f); //构造函数
    ~Role(); //析构函数
    void eat(int d); //吃东西,涨d血
    void attack(Role &r); //攻击别人,自己涨血,同时失血
    bool isAlived(); //是否活着
    void show(); //显示
private:
    string name;
    int blood;
    Weapon weapon;
    bool life;
};
//文件3,类的实现,Weapon.cpp

  1. #include<iostream>  
  2. using namespace std;  
  3. #include"Weapon.h"  
  4. Weapon::Weapon(string wnam, int f):wname(wnam),force(f) {}  
  5. int Weapon::getForce()  
  6. {  
  7.     return force;  
  8. }  
#include<iostream>
using namespace std;
#include"Weapon.h"
Weapon::Weapon(string wnam, int f):wname(wnam),force(f) {}
int Weapon::getForce()
{
    return force;
}
//文件4,类的实现,Role.cpp

  1. #include<iostream>  
  2. #include"Weapon.h"  
  3. #include"Role.h"  
  4. using namespace std;  
  5. Role::Role(string nam, int b, string wnam, int f):name(nam),blood(b),weapon(wnam,f)  
  6. {  
  7.     if(blood>0)  
  8.         life=true;  
  9.     else  
  10.         life=false;  
  11. }  
  12. Role::~Role()  
  13. {  
  14.     cout<<name<<"退出江湖..."<<endl;  
  15. }  
  16. void Role::eat(int d) //吃东西,涨d血  
  17. {  
  18.     if(isAlived())  
  19.         blood+=d;  
  20. }  
  21. void Role::attack(Role &r) //攻击别人,涨1血  
  22. {  
  23.     if(isAlived())  
  24.     {  
  25.         blood+=weapon.getForce();  
  26.         r.blood-=weapon.getForce();  
  27.         if(r.blood<=0)  
  28.             r.life=false;  
  29.     }  
  30. }  
  31.   
  32. bool Role::isAlived() //是否活着  
  33. {  
  34.     return life;  
  35. }  
  36.   
  37. void Role::show() //显示  
  38. {  
  39.     cout<<name<<" has "<<blood<<" blood, it is ";  
  40.     if(isAlived())  
  41.         cout<<"alived.";  
  42.     else  
  43.         cout<<"dead.";  
  44.     cout<<endl;  
  45. }  
#include<iostream>
#include"Weapon.h"
#include"Role.h"
using namespace std;
Role::Role(string nam, int b, string wnam, int f):name(nam),blood(b),weapon(wnam,f)
{
    if(blood>0)
        life=true;
    else
        life=false;
}
Role::~Role()
{
    cout<<name<<"退出江湖..."<<endl;
}
void Role::eat(int d) //吃东西,涨d血
{
    if(isAlived())
        blood+=d;
}
void Role::attack(Role &r) //攻击别人,涨1血
{
    if(isAlived())
    {
        blood+=weapon.getForce();
        r.blood-=weapon.getForce();
        if(r.blood<=0)
            r.life=false;
    }
}

bool Role::isAlived() //是否活着
{
    return life;
}

void Role::show() //显示
{
    cout<<name<<" has "<<blood<<" blood, it is ";
    if(isAlived())
        cout<<"alived.";
    else
        cout<<"dead.";
    cout<<endl;
}
文件5,主函数,demo.cpp

  1. #include <iostream>  
  2. using namespace std;  
  3. #include"Role.h"  
  4. int main( )  
  5. {  
  6.     Role mary("Mary", 500, "TuLong",200);  
  7.     Role jack("Jack", 10, "YiTian", 180);  
  8.     cout<<"---begin---"<<endl;  
  9.     mary.show();  
  10.     jack.show();  
  11.     cout<<"---1st round---"<<endl;  
  12.     jack.attack(mary);  
  13.     mary.show();  
  14.     jack.show();  
  15.     cout<<"---2nd round---"<<endl;  
  16.     mary.attack(jack);  
  17.     mary.show();  
  18.     jack.show();  
  19.     cout<<"---end---"<<endl;  
  20.     return 0;  
  21. }  
#include <iostream>
using namespace std;
#include"Role.h"
int main( )
{
    Role mary("Mary", 500, "TuLong",200);
    Role jack("Jack", 10, "YiTian", 180);
    cout<<"---begin---"<<endl;
    mary.show();
    jack.show();
    cout<<"---1st round---"<<endl;
    jack.attack(mary);
    mary.show();
    jack.show();
    cout<<"---2nd round---"<<endl;
    mary.attack(jack);
    mary.show();
    jack.show();
    cout<<"---end---"<<endl;
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值