I read the tutorial on polymorphism in this site.
But I could imagine nothing that would actually become easier if I used Polymorphism.
So what is the point in their use?
When is it actually beneficial?
class Enemy
{
public:
virtual ~Enemy() { }
virtualvoid Update() = 0; // pure virtual 'Update' function
};
//...
class Goblin : public Enemy // a Goblin "is an" Enemy
{
public:
virtualvoid Update()
{
// ... do things that a Goblin would do
}
};
//...
class Orc : public Enemy // an Orc "is an" Enemy
{
public:
virtualvoid Update()
{
// ... do things that an Orc would do
}
};
You could write different AI code for each different enemy type.
The trick is, because all enemies derive from a common 'Enemy' base class, you can treat them all the same:
1 2 3 4 5 6 7 8 9 10
std::vector<Enemy*> enemies; // our array of enemies
// create some enemies -- 2 goblins and an Orc
enemies.push_back( new Goblin );
enemies.push_back( new Goblin );
enemies.push_back( new Orc );
// now when we want to update all enemies, we just call their Update function
for(int i = 0; i < enemies.size(); ++i)
enemies[i]->Update();
Calling Update like this will automatically do the Goblin's AI if the enemy is a goblin, or the Orc's AI if the enemy is an Orc.
Another nice this is this makes it easy to add new enemy types. All you have to do is derive a new class from Enemy. You don't need to change anything else.
you came and you want to change the implementation of nameChanged() to something else, and you want that it should draw a rectangle now.. so what you did is this:
class nisheethNameBase : public nameBase
{
private:
public:
nisheethNameBase(const std::string& name) : nameBase(name){}
void __CALLBACK nameChanged()
{
//nisheethNameBase's own code here
std::cout << "draw rectangle" << std::endl;
}
};
int main ()
{
nameBase *obj1 = new nisheethNameBase("");
obj1->setName("obj1");
}
see how automatically your callback will be called without doing anything in the base classes. Although this is just an idea and not a perfect example, but in big code this idea gives a good design to the whole application.
Disch example is a good example and can be used to implement kind of observer pattern. So you learned a pattern also.