Public Group

Class Declaration for a sports player

This topic is 4463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys, i am relatively new to this and am trying to write a declaration for a sports player. I am trying to keep it simple so please don't worry about whats not there per se, more could you just check to see that i am doing the right things, details can be added later. My first assumption is that all the attributes and details aere private fields to protect them from being accidently changed at any point, if that is wrong feel free to let me know... anyway here goes

class Player
{

private:
**************************
string Name;
float Height;
int Age;
string Position;
float Weight;
int Defence_Ability;
int Attack_Ability;
int Phyisical_Ability;
int Technical_Ability;
int Mental_Ability;
**************************
public:
**************************
Player();
~Player();

string getPlayerName() const {return Name;}
void setPlayerName (string PlayerName){Name = PlayerName;}

int getPlayerAge() const {return Age;}
void setPlayerAge (int PlayerAge) {Age = PlayerAge;}
**************************
etc for all private variables...


So what do you guys think, all help will be greatly appreciated. Neil

Share on other sites
I think your constructor should take as arguments values for all the private members. Consider removing all the setFoo() functions, and replacing them with command-like functions. For example your could decide that a players name isnt going to change (why should it?) so you could omit any attempt to change it. For the players stats, if you only allow players to increase a stat by one at a time, you could replace setDefence...() with increaseDefense() or a similar function, it indicates what you are doing, not how you are doing it (you are leveling up the player, not taking its defence and adding to it ). If you want to be able to increment stats by more than one, just make the function take an int as a parameter. This allows some handy checks, like if you decided thats stats were from 1 - 100, then your increseDefense() function could enfoce that rule, saving you the bother in the calling code:

// old style:int defense = player.getDefense();defense += amountToIncrease;if( defense > 100 ){   player.setDefense( 100 );}else{   player.setDefense( defense );}// new style:player.increaseDefense( amountToIncrease );// in player.cppconst int MAX_STAT = 100;void Player::increaseDefense( int amount ){    Defence_Ability += amount;    if( Defence_Ability > MAX_STAT  )    {        Defence_Ability = MAX_STAT;    }}

Likewise a player is only ever going to get 1 year older at a time, so a function like increaseAge wouldn't need to take an argument, and you could have some code in the function that notifies you when a player gets too old to play, if you like.

The main point is that not only the variables, but all the logic associated with the variables is located in pretty much one source file. Other files can only use those functions, so it is harder for them to break the rules ( setting players skills to high levels ) by accident.

Your current method is not much better than giving your variables public access, which can lead to hard to track down bugs.

Share on other sites

So instead of the setwhatevers in the code, if it is a value that can be incremented replace it with an increasewhatever instead that could take an integer as an arguement.

Following on from this, if i wanted to decrease the value is it best just to add a decrease function to the class aswell?

Share on other sites
I used to apply the same approach with a 'get' and 'set' method for each member variable but that eventually seemed pointless for the reason that rip-off pointed out - they just end up behaving like public variables.

I've since begun to change things around a little and the best example would be the movement of the player in my football game. I would have my Game object determine which direction the player would run depending on keypresses and then call setAngle and setSpeed. This seemed counter-intuitive and it now simply calls a single moveDirection(int dir) function which tells the player object which direction they will be moving. The player object itself figures out the details of speed and angle necessary when it updates.

I'm guessing that's closer to the true intentions of object orientation - although textbook accuracy is not my aim here - as the Game object simply tells the Player object "the player wants you to move this way" The Player object itself deals with the particulars of the action and the effect on all it's own variables.

The differences are minimal really but I feel better trying to avoid a 'get' and 'set' for every single variable.

Share on other sites
its a problem with the way i learnt - a lot of time spent doing things to achieve a goal rather than the best way to do it - means that once i found something that worked it was best to stick with it even if it wasn't the best option.

Share on other sites
In the end, you'll probably end up wanting to define the sports players in data, such as XML. Thus, you might want to expose the attributes all as a collection, rather than having to bind each of them explicitly.

Thus, return a single XmlNode (or similar) for the attributes, and let the user pick out the data he wants from there. You can also implement this using something like a std::map<std::string, boost::variant>.

In addition, if you support temporary changes in stats (drinking Gatorade, say? :-) then you probably want each visible property to be derived from a base property and a number of active modifiers. Those modifiers, in turn, are probably also data-driven (so you can easily change and add them).

Share on other sites
Quote:
 Original post by neilski_2003its a problem with the way i learnt - a lot of time spent doing things to achieve a goal rather than the best way to do it - means that once i found something that worked it was best to stick with it even if it wasn't the best option.Thanks for the reply.

It's exactly the same with me. I learnt that format from the start and saw no point in changing it. In time however I've come to my own conclusions about making things a little more readable and less repetitive hence the slight adjustments to my practices. It's all part of the learning process - making your own mind up about how you want it to work.

Best of luck with it.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632620
• Total Posts
3007497
• Who's Online (See full list)

There are no registered users currently online

×