Syntax Help, class interactions and functions..

This is a discussion on Syntax Help, class interactions and functions.. within the C++ Programming forums, part of the General Programming Boards category; Um, theyre as good as any, but thanks for the tip on the reference thing i forgot totally about that. ...

  1. #16
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Um, theyre as good as any, but thanks for the tip on the reference thing i forgot totally about that.

    I thought that when you called the attack function and did something like this...

    Code:
    class Creature
    {
    public:
    
    	Creature(int health, int attack, int defense)
    	{
    		statistics.Health = health;
    		statistics.Attack = attack;
    		statistics.Defense = defense;
    	}
    
    	struct Stats
    	{
    		int Health;
    		int Attack;
    		int Defense;
    	};
    
    	void Attack(Creature c)
    	{
    		c.statistics.Health -= statistics.Attack;
    	}
    
    	Stats statistics;
    
    };
    
    class Human : Creature
    {
    };
    
    
    int main()
    {
    	Creature C1(100, 25, 10);
    	Creature C2(100, 25, 10);
    
    	C1.Attack(C2);
    	return 0;
    }
    it would work as planned

    Guess not...
    Last edited by Shamino; 05-17-2007 at 12:50 AM. Reason: mispasted the code
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    why do you not have your attack function return something, so you can know if it was successful? Also, what I did was not any different than a nested struct it just kept that little bit of clutter out of the class. Although it is all really in preference. That and if you like to have multiple scope levels in your class.

    I think that
    Code:
    bool Attack(Creature& target);
    
    {
    if ( C1.Attack(C2) == false )
       return ERROR;
    }
    this would be a little more "logical" in such a situation. That and it help to keep bugs narrowed down.
    Also, read some books. The patience of people on these boards has been limited these last few months. I know you used to know alot, but now just start over, and dont try to get in over your head. I know this is flame bait. Also, there was a "&" reference symbol in my previous post but there must have been a Copy/Paste error
    Last edited by Raigne; 05-17-2007 at 07:51 AM.

  3. #18
    Registered User
    Join Date
    May 2007
    Posts
    88
    >Nah stats inheriting creature is illogical, because creatures have stats, but are not stats themselves.

    Actually, if they share all the same properties, then a Creature is a Stat

    Also, you generally shouldn't have mutable public data members in a class. It basically negates the purpose for having the class in the first place. But you probably already knew that.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Actually, if they share all the same properties, then a Creature is a Stat
    If they share the same interface then a Creature is a Stat. However, in this case Creature is-implemented-in-terms-of Stats is likely a more accurate description of the relationship, which is why composition should be preferred.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21