Results 1 to 8 of 8

Thread: Polymorphism

  1. #1
    NOMADMAN
    Guest

    Polymorphism

    What is it? Where can I find an example of it? Is it related to Inheritence? Is it related to Virtual functions?

    I need a tutorial!
    NOMAD

  2. #2

  3. #3
    NOMADMAN
    Guest

    So is this polymorphism?

    So is this polymorphism?

    Code:
    class BaseClass
    {
    protected:
         int a;
         int b;
    public:
         virtual Product(void)
              { return (0); }
    };
    
    class Type1Class: public BaseClass {
      public:
        int Product(void)
          { return (a*b); }
    };
    
    class Type2Class: public BaseClass {
      public:
        float Product(void)
          { return (a / b); }
    };
    Is this polymorphism? I keep finding these internet files that say polymorphism but only seem to do some inheritence and/or virtual function deal...

    NOMAD?

    Am I close?

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Yes, because you can use the Product function in two different class doing two different things. So in your base class, you're using the Product function do nothing but in the derieved class, you can doing some multiplication.
    Baaaaaaaaah

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Polymorphism -- many shapes (or similar ).

    What abdul has told you is correct. Polymorphism is equivalent to using virtual functions; you can have a base class pointer pretending to be a derived class (the many shapes bit).

    For example:
    Code:
    class Shape {
    public:
        virtual ~Shape(); // needed for proper cleanup
    
        // pure virtual, you don't need to give it any
        // code, and the compiler won't let you actually
        // create one, but you can use it as a pointer
        virtual void draw() = 0;
    };
    
    class Circle : public Shape {
    public:
        void draw() { /* do something */ }
    };
    
    class Square : public Shape {
    public:
        void draw() { /* something else */ }
    };
    The main benefit here, is that you can have a std::list<Shape*>, and you can keep adding circles or squares, without needing to worry when the time comes to draw them all. Just keep calling ->draw() on the Shape* pointers, and the compiler will insert the necessary code to look up in the class what *actual* type it is.

    If that sounds complicated...it is...sadly
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    NOMADMAN
    Guest
    Ok so, now that I know a little more about polymorphism.

    Heres why I wanted to know:
    I have an array, if I make the array of say Shape (go with parksie example) how would it know if it was a Circle or Square. I see parksie used ->draw... should my array be of pointers of type Shape?

    Also Parksie, you wrote 'virtual void' does this mean that they all need to return the same type? Does it have to bee this way?

    Thanks guys!
    This is REALLY helping!

    NOMAD

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep, they're of type Shape*. How the compiler determines the type is implementation-dependent, so you can ignore it for now (when you get more used to the effects, you can look it up, it's to do with virtual function tables -- if you've ever seen references to vfptr, that's it).

    The virtual tells it to use it as a virtual function, and this is reflected through all the derived classes (you don't need to specify it).

    And yes, they must all return the same type (well, you can return different polymorphic types, within certain limits which I won't try and go into here).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    NOMADMAN
    Guest
    Thanks! I think I got my Polymorphism question out of the way! Now only 9,999,999,999 questions left!

    NOMAD!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width