473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

polymorphism

Can anyone explain polymorphism?(v ery simply).
thanks
richard
Jul 23 '05 #1
11 5102
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********@warp mail.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?(v ery 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·l y —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********@yah oo.mail.com> wrote in message
news:11******** *****@corp.supe rnews.com...
da********@warp mail.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********@warp mail.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********@yah oo.mail.com> wrote in message
news:11******** *****@corp.supe rnews.com...
da********@warp mail.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********@war pmail.net> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
2843
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 use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
18
12597
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 about whether we should allow polymorphism. Our system is not embedded and does not need to be as real-time as, say, a pacemaker. But it does involve updating displays based on radar input. So a need for something close to real-time is desired...
3
7450
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 http://en.wikipedia.org/wiki/Polymorphism http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?query=polymorphism&action=Search
4
7394
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 of polymorphism, but he insisted on runtime. Did I miss a new buzzword while I was sick with the flu last week or something like that?
13
14617
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 Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
18
3862
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 different objects to respond to the same request message in their own unique way" I thought that it was: "the ability of same object to respond to different messages in
2
3389
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 whether it should not be used in certain cases. Consider the case when all the params to a template function/class are similar. My questions is that whatever can be acheived by a template in such a case, can be acheived by runtime polymorphism....
11
2976
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++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
17
3880
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9869
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.