第7周项目3-用多文件组织多个类的程序
2016-04-21 08:41
288 查看
main.cpp:测试函数
game.h:类声明
2.role.cpp:定义角色类的成员函数
weapon.cpp:武器类的成员函数的实现
/* *Copyright(C) 2016,计算机与控制工程学院 *All rights reserved. *文件名:test.cpp *作者:张志新 *完成日期:2016年4月20日 *版本号:v1.0 * *问题描述:带武器的游戏类,分在多个文件。 */ #include <iostream> #include "game.h" using namespace std; int main( ) { Role qiangu("花千骨", 10000, "断念",1000); Role mantian("霓漫天", 10000, "屠龙", 500); qiangu .show(); mantian.show(); cout<<"霓漫天攻击花千骨"<<endl; mantian.attack(qiangu); qiangu.show(); mantian.show(); cout<<"花千骨攻击霓漫天"<<endl; qiangu.attack(mantian); qiangu.show(); mantian.show(); cout<<"end......"<<endl; return 0; }
game.h:类声明
#ifndef GAME_H_INCLUDED #define GAME_H_INCLUDED using namespace std; class Weapon { public: Weapon(string weap,int f); int getForce(); private: string wName; int force; }; class Role { public: Role(string nm,int xie,string w,int f); ~Role(); void show(); void attack(Role &r); void eat(int food); bool isAlived(); private: Weapon weapon; string name; int blood; bool life; }; #endif // GAME_H_INCLUDED
2.role.cpp:定义角色类的成员函数
#include <iostream> #include "game.h" using namespace std; Role::Role(string nm,int xie,string weap,int f):name(nm),blood(xie),weapon(weap,f) { if(blood>0) life=true; else life=false; } Role::~Role() { cout<<name<<"退出江湖!"<<endl; } bool Role::isAlived() { if(blood>0) return true; else return false; } void Role::show() { cout<<name<<" blood is "<<blood<<" he is "; if(isAlived()) { cout<<"alived"<<endl; } else { cout<<"dead"<<endl; } } void Role::attack(Role &r) { if(isAlived()) { blood+=weapon.getForce(); r.blood-=weapon.getForce(); if(r.blood<=0) r.life=false; } } void Role::eat(int food) { if(isAlived()) blood=blood+food; }
weapon.cpp:武器类的成员函数的实现
#include <iostream> #include "game.h" using namespace std; Weapon::Weapon (string weap, int f):wName(weap),force(f) { } int Weapon::getForce() { return force; }
相关文章推荐
- 【搜索】哈密顿绕行世界问题
- C++走向远洋——36(数组做数据成员,工资)
- JavaScript charAt() 方法
- #Objective-C 经典字典数组排序 - 省市区
- java各种序列化性能测试
- 无废话ExtJs 入门教程一[学习方法]
- 每天laravel-20160722|Application-2
- 成员函数,友元函数和一般函数 有区别。
- c++实验4-分数的累加
- 每天laravel-20160722|Application-2
- Android全局异常统一处理
- 搞ACM的你伤不起(转)
- memcache使用及语法
- 第八周上机实践项目1(1):数组做数据成员
- sun.misc.unsafe类的使用
- 分布式系统常用思想和技术
- 【BZOJ1103】[POI2007]大都市meg【树链剖分】【线段树】【或 树状数组 + dfs序】
- 心灵鸡汤之20160421
- 无废话ExtJs系列教程
- 他不懂