473,383 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

polymorphism

Can anyone explain polymorphism?(very simply).
thanks
richard
Jul 23 '05 #1
11 5052
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
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
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

"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


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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.