polymorphism

  • Thread starter richard pickworth
  • Start date
D

davidrubin

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

andy E

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
 
E

E. Robert Tisdale

richard said:
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*.
 
P

Peter Julian

andy E said:
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.
 
P

Prawit Chaivong

andy said:
(e-mail address removed) 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.
 
O

osmium

Peter Julian said:

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.
 
K

Karl Heinz Buchegger

Suresh said:
- 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).
 
R

richard pickworth

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
 
S

Suresh

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...
 
R

richard pickworth

I wan't expecting such a good response.
If two objects share the same interface, does that make them polymorphic?
Richard
 
B

Ben Pope

richard said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top