C++实验五——数据的共享与保护

实验报告

【实验名称】 实验五 数据的共享与保护
【实验内容】

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~3000
    int durability;  //耐久度0~100
    int physicalResist;  //物理抗性0~100
    int magicResist;  //魔法抗性0~100
    int 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~3000
    int durability;  //耐久度0~100
    int physicalResist;  //物理抗性0~100
    int magicResist;  //魔法抗性0~100
    int 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。这次实验不难,主要就是实现了静态成员变量和函数,以及友元函数的使用,以此来实现对数据的共享与保护。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值