Game.h
#pragma once
class person
{
int m_id;
const char* m_name;
int m_score;
int m_money;
private:
static int m_count;
static int m_averagescore;
public:
person(int id, char* name);
static int getcount();
static int getaveragescore();
void setplayer(int id, char* name);
void showplayer();
void chaegemoney(unsigned int money);
void usemoney(unsigned int money);
~person() { delete[]m_name; };
};
玩家.cpp
#include<iostream>
using namespace std;
#include"Gamer.h"
person::person(int id = 0,char * name = "default")
{
m_id = id;
m_score = 0;
m_money = 0;
m_count++;
m_name = name;
}
int person::m_count = 0;
int person::m_averagescore = 0;
int person::getcount()
{
return m_count;
}
int person::getaveragescore()
{
return m_averagescore;
}
void person::setplayer(int id, char* name)
{
m_id = id;
m_name = name;
}
void person::showplayer()
{
cout << "id:[" << m_id << "]\n" << "name:[" << m_name << "]\n"
<< "score:[" << m_score << "]\n" << "money:[" << m_money << "]\n"
<< "cout:[" << m_count << "]\n" << "averagescore:[" << m_averagescore << "]" << endl;
}
void person::chaegemoney(unsigned int money)
{
m_money += money;
}
void person::usemoney(unsigned int money)
{
m_money -= money;
m_score = money;//积分获取方式,累计消费
m_averagescore += (m_score / m_count);//所有玩家得分平均值
}
int main()
{
person _A, _B;
_A.setplayer(4, "course04");
_A.chaegemoney(100);
_A.usemoney(90);
_A.showplayer();
_B.setplayer(44, "course044");
_B.chaegemoney(444);
_B.usemoney(111);
_B.showplayer();
system("pause");
return 0;
}
不知道怎么改,help
C++建议用string代替C字符数组,用char指针有很多雷,比如你的析构函数,字符串的内存不是你new出来的,你这么delete,会有问题,如果new出内存,strcpy字符串,那为了不产生浅拷贝,你需要写拷贝构造和拷贝赋值函数,或者禁用这两者。如果不用析构delete,那意味着你必须保证所有传入的都是字面量字符串,程序存在期间永远存在,不能通过交互gets得到的字符串传递,否则name可能会来回的变,或者保证此字符串只用一次。
#pragma once
#include <string>
class person
{
int m_id;
std::string m_name;
int m_score;
int m_money;
private:
static int m_count;
static int m_averagescore;
public:
explicit person(int id = 0, const std::string &name = "default");
static auto getcount() -> int;
static auto getaveragescore() -> int;
void setplayer(int id, const std::string &name);
void showplayer();
void chaegemoney(unsigned int money);
void usemoney(unsigned int money);
~person() = default;
};
#include <iostream>
using std::cout;
using std::endl;
using std::string;
person::person(int id, const string &name)
{
m_id = id;
m_score = 0;
m_money = 0;
m_count++;
m_name = name;
}
int person::m_count = 0;
int person::m_averagescore = 0;
auto person::getcount() -> int
{
return m_count;
}
auto person::getaveragescore() -> int
{
return m_averagescore;
}
void person::setplayer(int id, const string &name)
{
m_id = id;
m_name = name;
}
void person::showplayer()
{
cout << "id:[" << m_id << "]\n"
<< "name:[" << m_name << "]\n"
<< "score:[" << m_score << "]\n"
<< "money:[" << m_money << "]\n"
<< "cout:[" << m_count << "]\n"
<< "averagescore:[" << m_averagescore << "]" << endl;
}
void person::chaegemoney(unsigned int money)
{
m_money += static_cast<int>(money);
}
void person::usemoney(unsigned int money)
{
m_money -= static_cast<int>(money);
m_score = static_cast<int>(money); //积分获取方式,累计消费
m_averagescore += (m_score / m_count); //所有玩家得分平均值
}
int main()
{
person A;
person B;
person C;
A.setplayer(4, "course04");
A.chaegemoney(100);
A.usemoney(90);
A.showplayer();
B.setplayer(44, "course044");
B.chaegemoney(444);
B.usemoney(111);
B.showplayer();
C = B;
system("pause");
return 0;
}