Question about Inheritence

This is a discussion on Question about Inheritence within the C++ Programming forums, part of the General Programming Boards category; OK this is a theoretical question. Say that I am creating a game that has a Hero() class, Monster(), and ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    Question about Inheritence

    OK this is a theoretical question. Say that I am creating a game that has a Hero() class, Monster(), and Fight() class. Say that the fight class takes hp/str/defense from the monster and hero class and has a fight with the two. Would you have the fight class inherent from both the Monster and Hero class? Also would you have Hero() and Monster() inherent from a base class as well?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Is Fight an object? That's the first question you have to ask yourself.
    Secondly, you must ask yourself is what the class another derives from is of that type.
    Say, is a Monster a Hero (in case Monster would inherit from Hero)? If not, then you should not derive Monster from Hero. The same applies to all classes.

    And if Hero and Monster shares properties, they can indeed derive from a base class. It's a good thing, actually, and probably a good choice, along with some virtual functions for polymorphism.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    I guess that the Fight class would not be of that type but it would be using the attributes from both the Monster and Hero class. So i guess it would not need to inherent from these classes. Would you just set the attributes needed from these two classes to public and let the Fight class use it from there?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think the point Elysia was making is that Fight() sounds like it should be a function, not a class; it might even be a function inside your Hero class, on the assumption that your player is the Hero and can choose who to fight. But the Hero and the Monster would get passed into the function, and then the function takes over and decides the outcome. If you pass things by reference, you can change the state of Hero and Monster inside the function.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Something like this...

    Entity
    Hero : Entity
    Monster : Entity

    Fight
    Fight.processFight(Hero& h, Monster& m)


    Have accessors and setters in Hero and Monster (the common ones can be defined in Entity) to adjust the attribute values.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Or:

    IEntity
    - public: virtual void Fight(IEntity *pEntity) = 0;
    - public: virtual void Fight(unsigned int EntityID) = 0;

    Hero : IEntity
    Monster : IEntity

    Fighting seems to be an action that an IEntity would perform against another IEntity object.
    The second Fight() would fit nicely into a paradigm where IEntity objects were managed by a central resource manager. To get the IEntity pointer you would ask the resource manager for it.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by Perspective View Post
    Fight
    Fight.processFight(Hero& h, Monster& m)
    Nay, nay, nay. Absolutely not.
    Fight is not an object. It is not a thing, thus it should not be a class.
    It is an action, which should be implemented as a function.
    Bubba has the right idea of implementing it in the base class, so both Monster and Hero can fight.

    Quote Originally Posted by chadmandoo View Post
    Would you just set the attributes needed from these two classes to public and let the Fight class use it from there?
    That's an even worse design.
    Never expose attributes from your classes. It breaks encapsulation (very important).
    If anything, for such a situation, you would declare a function or class a friend to access the class internals, and even this should be avoided because it creates dependencies and breaks encapsulation if you're not careful.
    Typically, the best designs avoids the need of friends and does absolutely not expose internals publically.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by Elysia
    Fight is not an object. It is not a thing, thus it should not be a class.
    It is an action, which should be implemented as a function.
    That said, in more complex situation, we may model a family of actions or behaviours as a (parallel) hierarchy of classes (e.g., an instance of the Strategy pattern).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Never heard of that one, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Strategy pattern

    I've never used it in code I've written myself, but it does make sense that if you have a rather complex set of combined actions, it would be a good thing to put the actions into a class of their own, and interfacing through a base-class representing the object(s) that the actions correspond to.

    Whether that is the right or wrong solution in this particular case is a different matter - I can't say for sure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,169
    Edit: I'm just dropping my logic into the kettle. The questions, all of them, are offered to give the OP better ideas about how to model this situation. I'm just playing off of what has already been presented. This being here to save time and trouble later.

    Would you have the fight class inherent from both the Monster and Hero class?
    Is the fight a monster? *
    Is the fight a hero? *

    Also would you have Hero() and Monster() inherent from a base class as well?
    Do they both eat? *
    Do they both sleep? *
    Do they both die? *
    Do they share the same race? *
    Do they have a common heritage? *

    Is Fight an object?
    Is "Dance" an object? *
    Is "Waltz" an object? *
    Is "Waltz" a noun? *
    Is "Dance" a noun? *
    Is "Fight" a noun? *

    Would you just set the attributes needed from these two classes to public and let the Fight class use it from there?
    Does "Fight" know the nature of the hero? *
    Does "Fight" know the nature of the monster? *
    Does "Fight" know the nature of every hero? *
    Does "Fight" know the nature of every monster? *

    Fight.processFight(Hero& h, Monster& m)
    Does "Fight" know the nature of "Flee"? *
    Does "Fight" know the nature of "Terrain"? *
    Does "Fight" know the nature of "Inventory"? *

    virtual void Fight(IEntity *pEntity) = 0;
    virtual void Fight(unsigned int EntityID) = 0;
    Do all "IEntity" "Fight"? *
    Do all "IEntity" "Fight" the same way"? *

    Fighting seems to be an action that an IEntity would perform against another IEntity object.
    Is "Fighting" an action to be taken? *
    Is "Fight" an action to be taken? *
    If there is "Fighting", is there not also "NotFighting"? *
    Isn't there also "Sleeping" and "Eating"? *
    Are "Sleeping" and "Eating" not simply words used to describe our current status? *

    It is an action, which should be implemented as a function.
    Despite my best efforts I couldn't think of a series of questions that might reveal my intent here. Instead I offer the Webster definition of "Action": The event or connected series of events, either real or imaginary, forming the subject of a play, poem, or other composition; the unfolding of the drama of events.".

    Soma

    * Rhetoric... I don't care about the answers. (Yep, I'm still doing that.)

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by phantomotap View Post
    Do all "IEntity" "Fight" the same way"? *
    Btw, for this...
    If it would be a virtual function, child classes could override the behavior of the basic "Fight", so that different entities could fight different ways.
    The only real problem would be if a Hero would fight a Monster differently than other Entities.
    But this could be solved by making it a template function and specializing for the types where it might fight differently.

    It's a good question and indeed and to be asked to oneself, but I believe that this approach might be a good one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,169
    I'm actually about to watch a movie with my parents so I've no time for cute. You'll have to forgive me.

    If it would be a virtual function, child classes could override the behavior of the basic "Fight", so that different entities could fight different ways.
    No. This is a problem. This is not a solution. Actually, this is the worst possible model for any simulation involving abstractions of actors, actions, and environment. (Without the specifics of what you envisioned, it also requires coupling of sickening levels for adding a new actor, action, or environment means recoding every class that might benefit from specific handling of the new actor, action, or environment.)

    Consider only four types of actors: 'entity::hero::warrior', 'entity::hero::wizard', 'entity::monster::lion, and 'entity::monster::bat':

    It is safe to say that 'warrior' and 'wizard' will fight 'lion' differently.
    It is safe to say that 'warrior' and 'wizard' will fight 'bat' differently.
    It is safe to say that 'lion' and 'bat' will fight 'warrior' differently.
    It is safe to say that 'lion' and 'bat' will fight 'wizard' differently.

    You have four different match possibilities. Specifically, you have four different match possibilities that require absolute knowledge about both actors.

    What you have is a multiple dispatch problem.

    You can't solve the problem with templates: templates are only a means by which to implement the required multiple dispatch.

    You can't solve the problem with inheritance: inheritance is only a means by which to implement the required multiple dispatch.

    You can join templates and inheritance, but you'll still only be implementing the mechanic for multiple dispatch. Feel free to add RTTI, but at the end of the day you'll still be implementing the required multiple dispatch. Add the strategy pattern as well--which was a good hint regardless, but the requirements don't change.

    (There is the obvious way of using only compile time polymorphism, but that is a greater issue.)

    This suggestion means, regardless of your skill or intelligence, you will still have to write code dealing with each possible beneficial pairing. (Obviously, not every pairing will be beneficial. One kind of environment probably doesn't need to know about another kind of environment.) I'm ignoring the actual "Fight" code as it is obvious. Ignoring the implementation and maintenance overhead of the multiple dispatch mechanic itself is not an option. Even my version, and my version is extremely good, requires considerable maintenance overhead in the face of a new pairing tree.

    We are only talking about a simulation. Let's pretend the language naturally supported dynamic multiple dispatch, is this level of precision really required? Ask yourself the most important question of all: how exact does the simulation need be? This is important because there is nothing wrong with any suggestion made so far. Really. Whether or not the individual suggestions have value depend on what one hopes to achieve with the model. With this scenario, if you sacrifice some precision, there are much better ways to model the simulation.

    A bonus question: how important is it that actor A to know the exact details of actor B if actor A knows that actor B is a familiar entity?

    Soma

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    phantomotap, what's your point?

    EDIT:
    Is this your main point? Frankly, all that rambling about multiple dispatch makes it hard to see what exactly is the point that you are trying to get across.
    Quote Originally Posted by phantomotap
    Whether or not the individual suggestions have value depend on what one hopes to achieve with the model.
    Not that that rambling is necessarily bad, but I daresay that we need to keep chadmandoo a little more focussed.
    Last edited by laserlight; 11-28-2008 at 07:47 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    I see a single dispatch:
    Code:
    template<typename Hero, typename Monster> void Fight(Hero& hero, Monster& monster)
    {
        // Let's just assume the hero attacks first
        monster.SetHP( monster.GetCurrentHP() - (hero.BaseAttack() - monster.Defense()) );
        hero.ShowAnimation(monster);
    
        if (monster.GetCurrentHP() <= 0)
        {
            monster.DeathAnimaton();
            return;
        }
    
        hero.SetHP( hero.GetCurrentHP() - (monster.BaseAttack() - hero.Defense()) );
        monster.ShowAnimation(hero);
    
        if (hero.GetCurrentHP() <= 0)
        {
            hero.DeathAnimaton();
            return;
        }
    }
    
    class Warrior
    {
    public:
        void ShowAnimation(Lion& lion);
        void ShowAnimation(Bat& bat);
        // ...
    };
    
    // etc
    Overloading and static polymorphism works well since you would have to have overloaded functions for every enemy or a common one when there aren't any special animations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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