I recently learn about the polymorphism and I don't know what is the purpose to use this feature. From http://www.cplusplus.com/doc/tutorial/polymorphism/ , it uses the child class rather than the parent class. I thought most of the time we inherit and we only use the parent class to access the child. Why would he create a pointer of child class and point to parent class then modify the value on the parent inherit class members?

Can I get some example to on using polymorphism?

Polymorphism is using overloaded functions to achieve different results based on the parameters used in the function call. If you have a parent class, say it's called Calendar or something with an abstract function called convertTime(), and you might have child classes called Day and Month. Day and Month would both use a function called convertTime, but Day might convert smaller amounts of time into hours and minutes while Month converts it into days and weeks. Creating the convertTime function locally and with a different list of parameters overloads the convertTime function, allowing the compiler to choose the one that fits the situation. You also do it with constructors in classes. The default constructor might take no arguments, but a separate constructor might take 2 or 3, or any other number. They have the same function name, but they do different things.

I'm bad at explaining things, but I think I explained it correctly. >.> Someone else can call me an idiot if I didn't.

When used correctly, polymorphism is easily the most useful feature in all of C++. It's more particularly useful when you start getting into game design, where you often have large linked lists of actors, all belonging to different subclasses but all needing to behave differently from one another. In such a case, you'd often give them different "thinker functions" so they can reliably execute even though the programmer doesn't have to keep track of which actor belongs to which class.

#include <iostream>

using std::cout;
using std::endl;

//The parent class upon which two polymorphic classes
//will be defined, dog and cat.
class animal
{
    public:
        bool tailwagging;

    //This is a polymorphic function. What this line does is
    //tell the compiler that all subclasses that inherit from
    //"animal" will have an is_angry() function that returns a bool
    //and takes no arguments.

    //This " = 0" at the end tells us that the "animal" class itself
    //has no is_angry() function, so "animal" is an abstract class.
        virtual bool is_angry(void) = 0;
};

//A cat class that is an animal. It has its own definition of whether it's
//angry or not, so it should have its own implementation of the "is_angry()" function.
class cat : public animal
{
    public:
        virtual bool is_angry(void)
        {
            //If a cat's tail is wagging, it's angry.
            return tailwagging;
        }
};

//A dog class that is an animal. We use different logic here for determining whether
//or not it is angry. It's still an animal, though, so it will still have an is_angry() function.
class dog : public animal
{
    public:
        virtual bool is_angry(void)
        {
            //If a dog's tail is wagging, it's happy.
            return !tailwagging;
        }
};

int main()
{
    //Let's declare some objects here, a dog and a cat.
    cat whiskers;
    dog fido;

    //They both have a "tailwagging" feild because they inherited from their parent class, "animal".
    whiskers.tailwagging = true;
    fido.tailwagging = true;

    //Even though both of these classes have an is_angry() function, they actually behave differently.
    //This is actually the point of polymorphism --- you want to have certain member functions to behave
    //certain ways, depending on what subclass it is.
    if(whiskers.is_angry())
        cout << "Whiskers is angry!" << endl;

    if(fido.is_angry())
        cout << "Fido is angry!" << endl;

    //Even if you, the programmer, don't know what class it belongs to, the program will still be able
    //to call the right version of the is_angry() function. For example:
    animal* mystery = &whiskers;

    if(mystery->is_angry())
        cout << "I don't know what animal this one is, but it sure looks mad!" << endl;

    return 0;
}

Hmmm... The picture is clearer. Can I conclude as, overload function is able to have multiple same function name but different content inside a class. Where virtual keyword make thing able to have multiple same function name but different content between inheritance class? The virtual keyword mainly for versioning method or flexible implement/modification on class's function?

I still got one more question. From the link at my 1st post, he uses

  CPolygon * ppoly1 = &rect;
  CPolygon * ppoly2 = &trgl;
  CPolygon * ppoly3 = &poly;

I thought there are 2 different class and the class structure and class size is different. Then how can he point to the CPolygon class to Rectangle class? I thought must be pointing to the same class.

Just thinking how physically he point in memory. For example, we create an int pointer, and we must type cast the assign address is also an integer variable address. But how about on inherit class they are different structure right?

Just thinking how physically he point in memory.

A pointer is a single number, probably (depending on your system) 4 or 8 bytes. The number is a memory address. That's it. All pointers are the same size. They are all just a single number. There is nothing, absolutlely nothing, in that pointer to give any indication of what kind of object it is pointing to. The compiler will check at compile time that your pointers-to-int do actually appear to point to an int, and so on, but you as the coder can stomp all over that if you want.

I thought there are 2 different class and the class structure and class size is different. Then how can he point to the CPolygon class to Rectangle class?

So now you've read my words above, the answer to this is clear. The classes are different sizes and structures. The pointers are not. All pointers are identical in size and structure. If you really wanted to, you could make an int-pointer and force it to point to a CPolygon. You could make a pointer to anything point to anything (this is not recommended as it gets out of hand quickly and wrecks the point of having the compiler check for errors)

In the code above, CPolygon * ppoly1 = &rect; , ppoly1 is a pointer to an object of type CPolygon. Is rect an object of type CPolygon? Yes it is, so no problems. This is what inheritance does; the CRectangle class IS a CPolygon. It is also a CRectangle. So the compiler checks that &rect is a pointer to a CPolygon, and because it is, everything is fine.

Ok. I just know that after inherit, CRectangle also type of CPolygon. Ok. I will go deeper and read how actually inheritance do.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.