您的位置:首页 > 其它

带武器的角色

2016-04-10 13:02 211 查看

/*

*Copyright (c) 2014,烟台大学计算机学院

*All right reserved.

*文件名称:test.cpp

*作 者:韩双志

*完成日期:2016年4月10日

*版本号:v1.0

*

/*

#include <iostream>

#include<string>

using namespace std;

class Weapon

{

public:

void intWeapon();

int force();

void showweapon();

private:

string name;

int shanghai;

};

class Role

{

public:

void inputRole();

void eat();

int attack();

int beattack();

bool isalive();

void show();

private:

string name;

bool life;

int blood=10;

Weapon weapon;

};

void Weapon::intWeapon()

{

int n;

cin>>n;

if(n==1) {shanghai=2;}

if(n==2) {shanghai=1;}

if(n==3) {shanghai=3;}

if(n==4) {shanghai=1;}

if(n==5) {shanghai=5;}

}

void Weapon::showweapon()

{

cout<<"1,刀-攻击力:2"<<endl;

cout<<"2,剑-攻击力:1"<<endl;

cout<<"3,弓-攻击力:3"<<endl;

cout<<"4,棍-攻击力:1"<<endl;

cout<<"5,方天画戟-攻击力:5"<<endl;

cout<<"请输入你要选择的武器"<<endl;

}

int Weapon::force()

{

return shanghai;

}

void Role::inputRole()

{

string nam;

cout<<"请输入游戏角色名字:"<<endl;

cin>>nam;

name=nam;

weapon.showweapon();

weapon.intWeapon();

}

void Role::eat()

{

int n;

cout<<"1,药瓶-加 2"<<endl;

cout<<"2,仙草-加 3"<<endl;

cout<<"3,馒头-加 1"<<endl;

cout<<"4,大力丸-加 5"<<endl;

cout<<"输入你要吃的东西:"<<endl;

cin>>n;

if(n==1) blood+=2;

if(n==2) blood+=3;

if(n==3) blood+=1;

if(n==4) blood+=5;

}

int Role::attack()

{

blood=blood+1+weapon.force();

return blood;

}

int Role::beattack()

{

blood=blood-weapon.force();

return blood;

}

bool Role::isalive()

{

if(blood>0)

return true;

else

return false;

}

void Role::show()

{

if(isalive())

cout<<"你还有 "<<blood<<"血"<<" 还活着"<<endl;

else

cout<<"你还有 "<<blood<<"血"<<"已阵亡"<<endl;

}

int main()

{

cout<<"-----begin-----"<<endl;

Role a;

a.inputRole();

a.isalive();

a.show();

a.attack();

a.beattack();

a.eat();

a.show();

cout<<"-----end-----"<<endl;

return 0;

}

*/

运行结果:



知识点总结:

类成员函数的调用

学习心得

进一步巩固了对类函数的了解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: