By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

polymorphism

P: n/a
Can anyone explain polymorphism?(very simply).
thanks
richard
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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

Jul 23 '05 #2

P: n/a
da********@warpmail.net 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
Jul 23 '05 #3

P: n/a
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[s].
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*.

Jul 23 '05 #4

P: n/a

"andy E" <an********@yahoo.mail.com> wrote in message
news:11*************@corp.supernews.com...
da********@warpmail.net 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.

Jul 23 '05 #5

P: n/a


andy E wrote:
da********@warpmail.net 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.

Jul 23 '05 #6

P: n/a
"Peter Julian" writes:
"andy E" <an********@yahoo.mail.com> wrote in message
news:11*************@corp.supernews.com...
da********@warpmail.net 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.

Jul 23 '05 #7

P: n/a
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
<da********@warpmail.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Aug 8 '05 #8

P: n/a
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...

Aug 8 '05 #9

P: n/a
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
kb******@gascad.at
Aug 8 '05 #10

P: n/a
I wan't expecting such a good response.
If two objects share the same interface, does that make them polymorphic?
Richard
"richard pickworth" <ri***************@btopenworld.com> wrote in message
news:d7**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
Can anyone explain polymorphism?(very simply).
thanks
richard

Aug 26 '05 #11

P: n/a
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...
Aug 26 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.