Archived

This topic is now archived and is closed to further replies.

Jonus

Pseudo_Code OOP question

Recommended Posts

Jonus    170
I'm not sure if I'm quite approaching this question correctly, but here goes: Let's say I'm working on a fighter. I have a character class, and an attack class.
class Character
{
public:
     Character();//constructor
     ~Character();//destructor
     ...
     ...
     ...//some other members/member functions

     Attack attackone;//character's first attack
     
     ...
     ...

     int strength;//The character's strength
}//class Character

class Attack
{
public:
     Attack();//constructor
     ~Attack();//destructor
     ...
     ...
     ...//insert crap here

     void perform();//Perform the attack
}//class Attack
    
For the sake of the question, we'll say that Attack::perform() is dependant on its character's strength. It needs to somehow use the value of "strength" from the character object that it exists in to determine the damage that will be inflicted. I have no idea how much sense the previous sentence makes. Anyway, how would I go about doing this? This post is not writen by me(Jonus), the author of this post is Pseudo_Code. Edited by - Jonus on March 15, 2001 7:29:50 AM

Share this post


Link to post
Share on other sites
Kylotan    9857
Er... surely you can just add a parameter to the Attack function for the strength... such as void Perform(int strength); Am I missing something? Or, if you will need more thant just strength, pass in a pointer to the Character in Attack''s parameter and access the owning Character that way. Make Attack a friend class of Character if you need. This is just standard composition rules, really.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I''ll add that you probably want a calling function in the character class to make it simpler elsewhere in your program. It''s not strictly necessary, but it looks cleaner.

Character::strike()
{
attackone.perform(strength);
}

Now whenever you want the character to attack something it''s a simple function call *.strike() rather than a long string to retype.


I''m going to refactor it further though.

If the data members of each attack class are unique to each character then you don''t really need a separate class. Just make it part of the larger character class.

Your example suggests to me, by use of a separate class, that the attack class may be shared by things other than characters, or perhaps the same attack data shared by different types of characters.

If attacks are going to be common data objects, then perhaps using a pointer to an attack class rather than a member object would be a way to go.

class Character {

attack * attackone;
}

If, however, the attacks are going to be a constant for each character, and tend to be unique for each character, or at least unchanging,then consider folding the attack class into the character class completely.

class Character {

int strength;

int attackParamOne, attackParamTwo;
void perform();
}

I''d say either separate the classes further or bring them closer together.

Share this post


Link to post
Share on other sites