Author: Volker Böhm
Date: 04:24:34 08/01/04
Go up one level in this thread
On August 01, 2004 at 05:28:36, Uri Blass wrote:
>On August 01, 2004 at 04:17:35, Volker Böhm wrote:
>
>>Hi Uri,
>>
>>I read your questions twice but I am still not sure what you are asking for.
>>Nevertheless I try an answer.
>>
>>The "variables" of the class are called "attributes". The "functions" of the
>>class are called "members" or "member-functions".
>
>I will be more specific and I will use the terms that you mention(I know nothing
>about C++ and I have only code that Dann Corbit sent me to help me when the code
>is not new varaibles or new functions but only converting the old code that I
>used to class).
>
>In my case suppose that the time control is 40 moves/120 minutes+20 moves/60
>minutes+30 minutes/rest of the game.
>
>I have num_moves_1=40
>num_moves_2=20
>num_moves_3=Infinit(defined to be 9999)
>
>I get all this varaibles based on the numbers that I get after the level command
>Is it logical to use them as attributes when the function of the level command
>is a member function?
Yes, num_moves_1,num_moves_2,num_moves_3 should be attributes
The level function is then a method that sets the num_moves
either:
class time
{
private:
int num_moves1;
int num_moves2;
int num_moves3;
public:
time()
{
num_moves1 = 0;
num_moves2 = 0;
num_moves3 = 0;
}
void level(int moves1, int moves2, int moves3)
{
num_moves1 = moves1;
num_moves2 = moves2;
num_moves3 = moves3;
}
};
or (more to write, I prefer the above)
class time
{
private:
int num_moves1;
int num_moves2;
int num_moves3;
public:
void SetNum_Moves1(int moves)
{
num_mvoes1 = moves;
}
...
time()
{
SetNum_Moves1(0);
...
}
void level(int moves1, int moves2, int moves3)
{
SetNum_Moves1(moves1);
...
}
};
>
>Is it logical to use the number of moves to the next time control also as
>attribute that is calculated from other attributes by a member function?
>
No.
Better recalculate everytime you need it (if speed does not matter):
class time
{
private:
int MovesPlayed;
public:
int GetNumberOfMoves()
{
int Result = 0;
if (MovesPlayed < num_moves1)
{
Result = num_moves1 - MovesPlayed;
}
else if ....
return Result;
}
};
>
>>
>>Don´t use attributes as parameters to members as they have direct access to
>>those attributes. This works very well if classes don´t get too large.
>>
>>To get "perfect":
>>If ever you access an attribute use "getter" and "setter". A getter is a member
>>that gets the value of an attribute, the setter sets it´s value. I use this
>>method in larger classes only.
>>
>>Greetings Volker
>
>This was exactly my idea to use getters so the member function that calculate
>the number of moves for the next time control can practically get attributes as
>parameters but I also thought that I do not need parameters like you suggest.
>
>I still thought that maybe it is better to use them because I saw that other
>chess programs have a lot of varaibles in their functions so I thought it may be
>bad programming not to mention the varaibles that the function is using even if
>they are attributes.
Not bad at all, but exactly the right thing to do. The main idea behind objects
is that objects are black boxes that know about their own state (attributes).
Members are functions that uses or modify their own state.
Volker
>
>Uri
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.