我正在研究一个cpp项目,这是我的一个班级的作业,我遇到了上述问题。它的意思基本上是我有这两个类(至少现在):
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class Player
{
string name, gender, jobOutside;
unsigned int age, numberOfWins;
float skill, tiredness, popularity;
public:
Player();
Player(string n, string g, string j, unsigned int a, unsigned int nw, float s, float t, float p);
~Player();
void setName(string n);
void setGender(string g);
void setJobOutside(string j);
void setAge(unsigned int a);
void setNumberOfWins(unsigned int w);
void setSkill(float s);
void setTiredness(float t);
void setPopularity(float p);
string getName();
string getGender();
string getJobOutside();
unsigned int getAge();
unsigned int getNumberOfWins();
float getSkill();
float getTiredness();
float getPopularity();
void sleeps(Player p);
void socializes(Player p);
void develops(Player p);
void competes(Player p);
void wins(Player p);
void getsTired(Player p);
void plrStatus();
};
#endif // PLAYER_H_INCLUDED
和
#ifndef TEAM_H_INCLUDED
#define TEAM_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class Team
{
string colour;
unsigned int numberOfPlayers, numberOfWins, mealsPerWeek;
Player plrArr[11];
public:
Team();
Team(string c, unsigned int p, unsigned int w);
~Team();
void setColour(string c);
void setNumberOfPlayersAndMeals(unsigned int p);
void setNumberOfWins(unsigned int w);
string getColour();
unsigned int getNumberOfPlayers();
unsigned int getNumberOfWins();
unsigned int getMealsPerWeek();
void tmStatus();
};
#endif // TEAM_H_INCLUDED
我想知道我们如何在课堂上初始化团队类的plrArr [11]属性。构造函数。另外,我将如何制作相应的集合并获取其他属性的功能? 非常感谢那些回答!!
答案 0 :(得分:0)
如果你想在自动内存中使用数组Player plrArr[11];
,你应该在以后的方法中添加方法:
class Player
{
string name, gender, jobOutside;
unsigned int age, numberOfWins;
float skill, tiredness, popularity;
public:
void Init(string n, string g, string j, unsigned int a, unsigned int nw, float s, float t, float p);
void Init(const Player& plCopy);
或者只使用数组指针(Player **plrArr;
)或std::vector<Player *>
以及使用复制构造函数的init播放器。
我认为你应该使用抽象工厂模式来初始化玩家,它会使你的代码变得非常灵活。我将向您展示Player plrArr[11];
的示例。您可以稍后在Player上重写它。
class ITeamInitializer
{
public:
virtual void InitTeam(Player players[], int count) = 0;
virtual ~ITeamInitializer(){}
};
class HardcordeTeamInitializer : public ITeamInitializer
{
public:
virtual void InitTeam(Player players[], int count) override
{
//players[0].Init(string n, string g, string j, unsigned int a, unsigned int nw, float s, float t, float p) //Some hardcoded data init Goalkeeper))
//players[1].Init(string n, string g, string j, unsigned int a, unsigned int nw, float s, float t, float p) //Some hardcoded data init
//...
//players[9].Init(string n, string g, string j, unsigned int a, unsigned int nw, float s, float t, float p) //Some hardcoded data init
//players[10].Init(string n, string g, string j, unsigned int a, unsigned int nw, float s, float t, float p) //Some hardcoded data init
}
};
class FileTeamInitializer : public ITeamInitializer
{
public:
FileTeamInitializer(const std::string& filename);
virtual void InitTeam(Player players[], int count) override
{
//Read team from file
}
};
class RandomTeamInitializer : public ITeamInitializer
{
public:
virtual void InitTeam(Player players[], int count) override
{
for ( int i = 0; i < count; i++ )
{
players[i].Init( GenarateRandomPlayer() ) //Generating random player
}
}
private:
Player GenarateRandomPlayer();
};
您还需要将工厂参数传递给Team构造函数:
class Team
{
string colour;
unsigned int numberOfPlayers, numberOfWins, mealsPerWeek;
Player plrArr[11];
public:
Team(ITeamInitializer& teamInitializer)
{
teamInitializer.initTeam(plrArr, 11);
//...
}
Team(string c, unsigned int p, unsigned int w, ITeamInitializer& teamInitializer)
{
teamInitializer.initTeam(plrArr, 11);
//...
}
因此,借助此模式,您可以在开发过程中切换初始化方法,并在具体情况下选择最佳方法。我展示了3个工厂的例子(简单的硬编码值,从文件初始化,生成随机播放器),你可以更好地提出其他人的意见。 祝你好运!