Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am writing a text based RPG, and I have three classes that inherit from a super class, they all have special attacks that they can perform, at the same time I have a class that holds the function which handles battles in my game.

Now how do I get the unique special abilities functions for whatever role the player chooses into the battle function?

Also I am using the vector.push_back method to handle how my sub classes are referenced.

share|improve this question
    
Have you seen circlemud.org? –  karlphillip Apr 1 '12 at 1:30
add comment

1 Answer

You just need a virtual member function in your superclass:

class Superclass
{
    virtual void specialAbility() = 0;
};

And implementations in your subclasses:

class Subclass1 : public Superclass
{
    void specialAbility()
    {
        // Do something specific to this subclass...
    }
};

class Subclass2 : public Superclass
{
    void specialAbility()
    {
        // Do something specific to this subclass...
    }
};

Now when you do your battles, you could do something like this (edited to put in class):

class BattleDoer
{
    void doBattle(std::vector<Superclass*> combatants)
    {
        for (unsigned int index = 0; index < combatants.size(); index++)
        {
            combatants.at(index)->specialAbility();
        }
    }
};

This will cause the appropriate member function defined in the subclass to be called on each of the combatant objects.

Edit:

To populate your combatant list and call the doBattle() function, you can do something like this:

int main()
{
    Superclass* combatant1 = new Subclass1;
    Superclass* combatant2 = new Subclass2;

    std::vector<Superclass*> combatants;
    combatants.push_back(combatant1);
    combatants.push_back(combatant2);

    BattleDoer battleDoer;
    battleDoer.doBattle(combatants);

    delete combatant1;
    delete combatant2;
}

It is important to use pointers here. Only pointers and references can be used to provide polymorphism. You cannot have variables of type Superclass because it cannot be instantiated, you can only have references and pointers of type Superclass. Smart pointers should probably be used but I thought I'd keep it simple.

share|improve this answer
    
If the vector is defined in main, and the void doBattle function is defined in a battle class, how do i code the call in main so i can pass the vector –  Tucker Morgan Mar 31 '12 at 16:35
    
I've edited my answer, hopefully it helps. –  Gary Buyn Mar 31 '12 at 20:48
    
Just a small nit: you have a memory leak. When a vector falls out of scope it calls the destructor of each object of the contained Type. Calling ~Type when Type is a Type* is a non-op. You must manually delete each object first. –  Casey Apr 1 '12 at 17:56
    
Thanks @Casey, I've added the deletes. I usually use smart pointers so I'm not used to manual deletion. –  Gary Buyn Apr 1 '12 at 21:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.