第六周上机实践项目-武器类

  1. 
    
  2. /* 
     *Copyright (c)2016,烟台大学计算机与控制工程学院 
     *All rights reserved. 
     *文件名称:main.cpp 
     *作    者:刘涛 
     *完成日期:2016年4月7日 
     *版 本 号:v1.0 
     * 
     *问题描述:在第五周上机时间项目“游戏中的角色类”上拓展,添加一个武器类 
    1 */  
  3. 
    
  4. #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;
  5. }
    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;
       }
  6.     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();
  7.     James.show();
        Curry.show();
        return 0;
  8. }
    
运行及结果:
<img src="https://img-blog.csdn.net/20160407210221483?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值