网站制作教程及流程/优化seo厂家
实验报告
- 题目2
【实验名称】 实验五 数据的共享与保护
【实验内容】
1.为某个自己设定的游戏设定玩家类Player,设计必要的成员变量/成员函数/构造函数等。在类中增加静态成员,用于记录当前已经注册的玩家数量,并设计静态成员函数用于访问该数据。在主函数中生成对象并进行测试。
源代码:Player.h
#ifndef _Player_H_
#define _Player_H_
#include "Player.h"
#include <time.h>
using namespace std;
enum Sex{male,female};class Player{
public:Player();Player(string name , string password , int age = 0, Sex sex = male, string e_mail = NULL);static int getPlayerNumber(){ return playerNumber; } //返回当前注册玩家数量string getTime(){ return registerTime; } //返回注册时间void displayInformation(); //显示玩家信息void changeInformation(); //修改玩家信息void play(); //开始游戏
private:void setTime(); //设置注册时间static int playerNumber; //所有注册玩家的数量string registerTime; //注册时间static int IDgrowth; //ID号的增长int ID; //唯一玩家ID号int grade; //等级int experience; //经验string name; //用户名string password; //密码int age; //年龄Sex sex; //性别string e_mail; //邮箱
};#endif
Player.cpp
#include<iostream>
#include<stdlib.h>
#include"Player.h"
using namespace std;int Player::playerNumber = 0;
int Player::IDgrowth = 10000;Player::Player(){IDgrowth = IDgrowth + rand()%11;ID = IDgrowth;grade = 0;experience = 0;setTime();playerNumber++;}Player::Player(string name , string password , int age , Sex sex , string e_mail){this->name = name;this->password = password;this->age = age ;this->sex = sex;this->e_mail = e_mail;IDgrowth = IDgrowth + rand()%11;ID = IDgrowth;grade = 0;experience = 0;setTime();playerNumber++;}void Player::setTime() //获取当前时间,作为注册时间{time_t timep;time (&timep);char tmp[64];strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );registerTime = tmp;}void Player::displayInformation(){cout <<ID<< "号玩家信息如下:" <<endl;cout << "玩家姓名:" << name <<endl;cout << "唯一ID号: " << ID <<endl;cout << "注册时间: " << registerTime <<endl;cout << "玩家等级: " << grade <<endl;cout << "玩家目前的经验: " << experience <<endl;cout << "玩家年龄: " << age <<endl;cout << "玩家性别: ";switch(sex){case 0:cout<< "男生" <<endl;break;case 1:cout<< "女生" <<endl;break;}cout << "玩家的邮箱: " << e_mail <<endl;}void Player::changeInformation(){int n = 1;while(n){cout << "请输入您需要修改的信息编号:0、用户名 1、密码: 2、性别 3、邮箱" <<endl;int m;string str;cin >>m;switch(m){case 0:cout<<"请输入新的用户名:"<<endl;cin >>str;name = str;break;break;case 1:cout<<"请输入新的密码:"<<endl;cin >>str;password = str;break;case 2:cout<<"请输入性别:男生 or 女生"<<endl;cin >>str;if(str == "男生") {sex = male;cout <<"性别修改为男生"<<endl;}if(str == "女生") {sex = female;cout <<"性别修改为女生"<<endl;}break;case 3:cout<<"请输入新的邮箱:"<<endl;cin >>str;e_mail = str;cout <<"邮箱修改为:"<< e_mail <<endl;}cout <<"是否要继续修改信息: 0、退出修改 1、继续修改"<<endl;cin>>n;}}void Player::play(){cout << "尊贵的 " <<name<< " 玩家进入游戏" <<endl;}
main.cpp
#include<iostream>
#include"Player.h"
#include"Player.cpp"
using namespace std;int main(){Player array[100];cout<<"array[0]访问的玩家数量,目前的注册玩家数量为:"<<array[0].getPlayerNumber()<<endl;Player player1("大灬白","A12345678",18,male,"123456789@qq.com");cout<<"player1访问的玩家数量,目前的注册玩家数量为:"<<player1.getPlayerNumber()<<endl;player1.displayInformation();player1.changeInformation();player1.displayInformation();player1.play();return 0;
}
【实验结果】
题目2
设计两个道具类Helm和Armor,
设计必要的成员变量/成员函数/构造函数等,其中要求必须有成员变量valueOfDefense.在外部定义一个友元函数,可以读取该数据成员。在主函数中生成对象并进行测试。
Armor.cpp
#include<iostream>using namespace std;class Armor{
public:Aemor(){}Armor(string name , int price,int durability,int physicalResist,int magicResist,int valueOfDefense){this->name = name;this->price = price;this->durability = durability;this->physicalResist = physicalResist;this->magicResist = magicResist;this->valueOfDefense = valueOfDefense;}string getName(){ return name; }int getPrice(){ return price; }int getDurability(){ return durability; }int getPhysicalResist(){ return physicalResist; }int getMagicResist(){ return magicResist; }void setPrice(int price){ this->price = price; }void setDurability(int durability){ this->durability = durability; }void setPhysicalResist(int physicalResist){ this->physicalResist = physicalResist; }void setMagicResist(int magicResist){ this->magicResist = magicResist; }void setValueOfDefense(int valueOfDefense){ this->valueOfDefense = valueOfDefense; }void show(){ //显示装备信息cout << name << "的价格是:" << price <<endl;cout << name << "的耐久度是:" << durability <<endl;cout << name << "的物理抗性是:" << physicalResist <<endl;cout << name << "的魔法抗性是:" << magicResist <<endl;}void effect(){cout<<"特殊效果:装备这件护甲,在血量低于30%时会获得一个随等级提高的护盾。"<<endl;}friend int getValueOfDefense(Armor &h);
private:string name; //装备名称int price; //价格0~3000int durability; //耐久度0~100int physicalResist; //物理抗性0~100int magicResist; //魔法抗性0~100int valueOfDefense; //防御值0~1000
};int getValueOfDefense(Armor &a){ return a.valueOfDefense; }
Helmet.cpp
#include<iostream>using namespace std;class Helmet{
public:Helmet(){}Helmet(string name , int price,int durability,int physicalResist,int magicResist,int valueOfDefense){this->name = name;this->price = price;this->durability = durability;this->physicalResist = physicalResist;this->magicResist = magicResist;this->valueOfDefense = valueOfDefense;}string getName(){ return name; }int getPrice(){ return price; }int getDurability(){ return durability; }int getPhysicalResist(){ return physicalResist; }int getMagicResist(){ return magicResist; }void setPrice(int price){ this->price = price; }void setDurability(int durability){ this->durability = durability; }void setPhysicalResist(int physicalResist){ this->physicalResist = physicalResist; }void setMagicResist(int magicResist){ this->magicResist = magicResist; }void setValueOfDefense(int valueOfDefense){ this->valueOfDefense = valueOfDefense; }void show(){ //显示装备信息cout << name << "的价格是:" << price <<endl;cout << name << "的耐久度是:" << durability <<endl;cout << name << "的物理抗性是:" << physicalResist <<endl;cout << name << "的魔法抗性是:" << magicResist <<endl;}void effect(){cout<<"特殊效果:装备这件头盔,可以为你抵挡一次致命攻击"<<endl;}friend int getValueOfDefense(Helmet &h);
private:string name; //装备名称int price; //价格0~3000int durability; //耐久度0~100int physicalResist; //物理抗性0~100int magicResist; //魔法抗性0~100int valueOfDefense; //防御值0~1000
};int getValueOfDefense(Helmet &h){ return h.valueOfDefense; }
Main.cpp
#include<iostream>
#include"Armor.cpp"
#include"Helmet.cpp"
using namespace std;int main(){Armor armor("锁子甲",1000,80,50,40,120);Helmet helmet("防爆头盔",800,60,74,43,160);armor.show();cout<< armor.getName() <<"的防御值:"<<getValueOfDefense(armor)<<endl;armor.effect();helmet.show();cout<< helmet.getName() <<"装备的防御值:"<<getValueOfDefense(helmet)<<endl;helmet.effect();return 0;
}
【实验结果】
【小结或讨论】
本次实验是实验五 数据的共享与保护,实验的主要目的是第一题在类中增加静态成员,用于记录当前已经注册的玩家数量,并设计静态成员函数用于访问该数据。在第一题中也为玩家设置了注册时间(通过strftime函数获取系统时间,以年-月-日 xx:xx:xx的格式记录)、唯一的ID号(通过rand函数的累加)、玩家等级、玩家经验等系统设置的成员变量,以及姓名、密码、年龄、性别、邮箱等可以个人设置的成员变量。实验二必须有成员变量valueOfDefense.在外部定义一个友元函数,可以读取该数据成员。还设置了装备的价格、耐久度、魔法抗性、物理抗性等,之后再主文件中通过友元函数读取了两件装备的防御值valueOfDefense。这次实验不难,主要就是实现了静态成员变量和函数,以及友元函数的使用,以此来实现对数据的共享与保护。