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

Using polymorphism

This topic is 5024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to understand how to use polymorphism, but can''t think of any instance other than the following:
class PVCBeing // PVC = Pure Virtual Class

    string m_strName;
    D3DXVECTOR3* m_vPosition;
    int m_iHealth, m_iMaxHealth;
    int m_iSpeed;

    virtual void Render() = 0;
    virtual void Move() = 0;
    virtual void HitTaken() = 0;
    virtual void Hit() = 0; // As in you''ve hit someone

    virtual void UpdateAI() = 0;

class CLittleMan : public PVCBeing
    ITEM m_itmInventory;
    WEAPON m_wpnWeapons[MAX_WEAPONS];
    void Render(){ /* Render model */ }
    void Move(){ /* Move code here */ }
    void HitTake(){ /* Damn, I''ve taken a hit */ }
    void Hit(){ /* Die rebel bastard */ }
    void UpdateAI(){ /* He went that way */ }
// etc

PVCBeing* g_pCurrentMonster;
g_pCurrentMonster = new CLittleMan;
Thanks in advance! /* I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition) */

Share this post

Link to post
Share on other sites
Your example is pretty much why people use polymorphism. You have a base class that has common functionality with a bunch of derived classes. For example you could have a base class called SceneObject that has a virtual function update(), then you could derive a bunch of classes and use polymorphism to have a different update() function for each derived object. But you could still update all the SceneObjects by looping through and calling update().

Share this post

Link to post
Share on other sites
Polymorphism is used all the time in c++. For instance, operation overload.

string x;
int y;
float z;

cin >> x >> y >> z;

Also constructors:

class MyStringClass
MyStringClass(const MyStringClass&);
MyStringClass(const char*);

And other functions that can be considered the same operation. . .

void OutputFormatted(const string&);
void OutputFormatted(int);
void OutputFormatted(float);
void OutputFormatted(char);
void OutputFormatted(const SomeOtherType&);

And it may be a useful tool in other situations as well.

[edited by - ForeverStarlight on April 16, 2004 1:34:07 AM]

Share this post

Link to post
Share on other sites
Polymorphism is not the same as operator (or function) overloading.

With polymorphism, you're making classes which inherit from a common base class, and have virtual methods (at least one anyway)

Then you can make a function which operates on any kind of object (which extends the base class), without knowing which kind it is. Specficially, you can call a virtual method, safe in the knowledge that the right virtual method will be called, even if it is on a class that you hadn't thought of when you wrote it.

Overloading allows you to write specific functions for specific types - which is not the same a virtual method.

Here is an excerpt from a game I wrote which uses polymorphism:

ObjIterator objend = gameobjects.end();
for (ObjIterator it = gameobjects.begin(); it != objend; it++)
GameObject *obj = *it;

I am iterating through a vector (or list? or somethign like that) calling the tick() method on each one. without knowing its type.


[edited by - markr on April 16, 2004 7:04:57 AM]

Share this post

Link to post
Share on other sites
Original post by markr
Polymorphism is not the same as operator (or function) overloading.

No, there are multiple ways in which you can achieve polymorphism with C++. Remember that polymorphism means "many forms", and so function and operator overloading do count as forms of polymorphism, because you are creating "many forms" of a particular function or operator. But yes, usually when we speak of ''polymorphism'' in terms of C++, we immediately think of using virtual functions, but take note that this isn''t the only way to achieve polymorphism with C++.

Just thought I''d point that out.

Share this post

Link to post
Share on other sites