polymorphism

Discussion in 'C++' started by richard pickworth, Jun 5, 2005.

  1. Can anyone explain polymorphism?(very simply).
    thanks
    richard
     
    richard pickworth, Jun 5, 2005
    #1
    1. Advertising

  2. richard pickworth

    Guest

    Polymorphism means that, given a single interface, you can observe
    different behavior from parameters of different (but related) types.
    The two main types of polymorphism are run-time (implemented as
    inheritance and virtual functions), and compile-time (implemented as
    templates). /david
     
    , Jun 5, 2005
    #2
    1. Advertising

  3. richard pickworth

    andy E Guest

    wrote:
    > Polymorphism means that, given a single interface, you can observe
    > different behavior from parameters of different (but related) types.
    > The two main types of polymorphism are run-time (implemented as
    > inheritance and virtual functions), and compile-time (implemented as
    > templates). /david
    >


    A crow is a bird.
    A parrot is a bird.
    Tell the bird to squawk and it makes a sound dependent on it's type.

    Andy
     
    andy E, Jun 5, 2005
    #3
  4. richard pickworth wrote:

    > Can anyone explain polymorphism?(very simply).


    According to The American Heritage Dictionary of the English Language

    http://www.bartleby.com/61/

    polymorphism

    SYLLABICATION: pol·y·mor·phism
    PRONUNCIATION: pl-môrfzm
    NOUN: 1. Biology The occurrence of different forms, stages,
    or types in individual organisms or in organisms of the same
    species, independent of sexual variations. 2. Chemistry
    Crystallization of a compound in at least two distinct forms.
    Also called pleomorphism.
    OTHER FORMS: poly·morphic, poly·morphous —ADJECTIVE
    poly·morphous·ly —ADVERB


    According to Wikipedia

    In computer science, polymorphism is the idea
    of allowing the same code to be used with different types,
    resulting in more general and abstract implementations.

    The concept of polymorphism applies to functions
    as well as types. A function that can evaluate to
    and be applied to values of different types
    is known as a polymorphic function.
    A datatype that contains elements of an unspecified type
    is known as a polymorphic datatype.

    There are two fundamentally different kinds of polymorphism.
    If the range of actual types that can be used is finite and
    the combinations must be specified individually prior to use,
    it is called ad-hoc polymorphism. If all code is written
    without mention of any specific type
    and thus can be used transparently with any number of new types,
    it is called parametric polymorphism.

    Programming using the latter kind is called generic programming,
    particularly in the object-oriented community. However,
    in many statically typed functional programming languages
    the notion of parametric polymorphism is so deeply ingrained
    that most programmers simply take it for granted.

    Polymorphism gained most of its momentum
    when object-oriented programming became a buzzword.


    Polymorph literally means many form.
    Lots of things in C++ can be described as polymorphs or polymorphisms
    including function and operator overloading, inheritance and templates
    but, usually, when C++ programmers use the term polymorphism,
    they mean run-time polymorphism, late binding and dynamic dispatch --
    virtual functions which may be overridden in derived types.
    This is, of course sloppy [ab]use of the English language.
    The term is much to vague and general to communicate useful information.
    I try to avoid using this word whenever possible.
    If you decide to use it, please elaborate a little when you first use it
    and explain exactly what it means to *you*.
     
    E. Robert Tisdale, Jun 5, 2005
    #4
  5. richard pickworth

    Peter Julian Guest

    "andy E" <> wrote in message
    news:...
    > wrote:
    > > Polymorphism means that, given a single interface, you can observe
    > > different behavior from parameters of different (but related) types.
    > > The two main types of polymorphism are run-time (implemented as
    > > inheritance and virtual functions), and compile-time (implemented as
    > > templates). /david
    > >

    >
    > A crow is a bird.
    > A parrot is a bird.
    > Tell the bird to squawk and it makes a sound dependent on it's type.
    >
    > Andy


    Not bad.

    Why make a cage for a crow and another cage for a parrot when you can make a
    birdcage that can hold any type of bird.
     
    Peter Julian, Jun 6, 2005
    #5
  6. andy E wrote:
    > wrote:
    > A crow is a bird.
    > A parrot is a bird.
    > Tell the bird to squawk and it makes a sound dependent on it's type.
    >
    > Andy


    Yeah!! I like your explaination. I really do.
     
    Prawit Chaivong, Jun 6, 2005
    #6
  7. richard pickworth

    osmium Guest

    "Peter Julian" writes:

    > "andy E" <> wrote in message
    > news:...
    >> wrote:
    >> > Polymorphism means that, given a single interface, you can observe
    >> > different behavior from parameters of different (but related) types.
    >> > The two main types of polymorphism are run-time (implemented as
    >> > inheritance and virtual functions), and compile-time (implemented as
    >> > templates). /david
    >> >

    >>
    >> A crow is a bird.
    >> A parrot is a bird.
    >> Tell the bird to squawk and it makes a sound dependent on it's type.
    >>
    >> Andy

    >
    > Not bad.


    Keep trying. A parrot needs to be told what to say, not just to say
    something. So you need function overloading to solve this in a clean,
    hassle-free way.
     
    osmium, Jun 6, 2005
    #7
  8. Suresh wrote:
    >
    > - Every variable has two types associated with it. Static type and
    > dynamic type.Static type is based on the way it was declared. In above
    > example static type of b is base*. Dynamic type is comes from the
    > context. In the above example base class pointer b is pointing derived
    > class object. So dynamic type of the b is derived *.


    You mean the right thing, but you express it wrongly.

    In your example

    > "circle is a shape"
    > class base {};
    > class derived {};
    > base *b = new derived();


    the static type of b is "pointer to base". But the dynamic type
    of b is also "pointer to base". b is a pointer!
    What you mean applies to the object, b points to:
    The static type of *b is "base"
    But the dynamic type of *b is "derived" (in your example).

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 8, 2005
    #8
  9. I think I understand inheritance.
    I've heard of (and read about) virtual functions, but it's not making sense.
    Compile time - no idea.
    yours
    Richard
    <> wrote in message
    news:...
    > Polymorphism means that, given a single interface, you can observe
    > different behavior from parameters of different (but related) types.
    > The two main types of polymorphism are run-time (implemented as
    > inheritance and virtual functions), and compile-time (implemented as
    > templates). /david
    >
     
    richard pickworth, Aug 8, 2005
    #9
  10. richard pickworth

    Suresh Guest

    You said that you understand inheritance... That's great...
    Here I try to explain some things to you...
    - with inheritance base class variable can point to any of derived
    class object...
    "circle is a shape"
    class base {};
    class derived {};
    base *b = new derived();
    - Every variable has two types associated with it. Static type and
    dynamic type.Static type is based on the way it was declared. In above
    example static type of b is base*. Dynamic type is comes from the
    context. In the above example base class pointer b is pointing derived
    class object. So dynamic type of the b is derived *.
    - When you are invoking any function on *b what would you expect?
    You expect the function that belongs to the object the b is currently
    pointing to be should get invoked.

    But without polymorphysm it will not be done as c++ strict on type. The
    function is invoked based on static type of the object.
    - virtual functions allow us to do everything that cannot be done
    above.

    example:

    class shape {
    void fun1 () {
    cout<<"I am shape";
    }
    };

    class circle:public shape {
    void fun1 () {
    cout<<"I am circle";
    }
    };

    int main() {
    shape * s = new circle();
    s->fun1();
    }

    it prints "I am shape" as static type of s is shape *.
    But it is not good to see...

    If you define fun1 in shape class as virtual function then you get
    "I am circle"
    output...
     
    Suresh, Aug 8, 2005
    #10
  11. I wan't expecting such a good response.
    If two objects share the same interface, does that make them polymorphic?
    Richard
    "richard pickworth" <> wrote in message
    news:d7v0tj$l40$-infra.bt.com...
    > Can anyone explain polymorphism?(very simply).
    > thanks
    > richard
    >
     
    richard pickworth, Aug 26, 2005
    #11
  12. richard pickworth

    Ben Pope Guest

    richard pickworth wrote:
    > I wan't expecting such a good response.
    > If two objects share the same interface, does that make them polymorphic?


    If you codify the interface as a pure abstract base class, and inherit from it, then yes.

    Ben
    --
    I'm not just a number. To many, I'm known as a String...
     
    Ben Pope, Aug 27, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Lawrence

    Polymorphism in GridView (ASP.Net 2.0)

    Kevin Lawrence, Feb 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    847
    Kevin Lawrence
    Feb 16, 2006
  2. Sony Antony

    Polymorphism in xml ??

    Sony Antony, Aug 26, 2003, in forum: Java
    Replies:
    0
    Views:
    560
    Sony Antony
    Aug 26, 2003
  3. Osaman
    Replies:
    2
    Views:
    447
    John C. Bollinger
    Jan 5, 2004
  4. Tony Morris

    polymorphism

    Tony Morris, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    727
    sonali
    Mar 7, 2004
  5. Krivenok Dmitry
    Replies:
    13
    Views:
    1,443
    Axter
    Jun 1, 2006
Loading...

Share This Page