带武器的角色
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;
}
*/
运行结果:
知识点总结:
类成员函数的调用
学习心得
进一步巩固了对类函数的了解
/*
*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;
}
*/
运行结果:
知识点总结:
类成员函数的调用
学习心得
进一步巩固了对类函数的了解
相关文章推荐
- 深度信念网络Deep Belief Networks资料汇总
- 医学图像分割研究思路
- Expedition---poj2431(优先队列-堆的实现)
- WEB API系列(一):WEB API的适用场景、第一个实例
- 编译错误:A class file was not written. The project may be inconsistent, if so try refreshing this projec
- VS2015 scanf() scanf_s
- 快速排序和快速选择
- DEV-C++对c文件提示无法编译的问题
- 公网IP与私网IP区别
- 虚拟机Vmware上安装Ubuntu操作系统,如何配置Vmware tools
- CentOS运行C++出现内存错误——double free or coruption
- 抽奖系统数据库设计
- 关于Go语言,自己定义结构体标签的一个妙用.
- WHOJ 1606 - Funny Sheep【水】
- 学习编程经典书籍
- 神经单元
- 欢迎使用CSDN-markdown编辑器
- myeclipse如何修改Web项目名称
- 九度 1010
- 使用Google浏览器必备的插件