Hi, what is polymorphism?
Any chance of a quick guide line please! maybe an example?
or a web page if you know one, but a guide line would be great...
Thank you so much for reading.
--------------------
from: desperate for help
Hi, what is polymorphism?
Any chance of a quick guide line please! maybe an example?
or a web page if you know one, but a guide line would be great...
Thank you so much for reading.
--------------------
from: desperate for help
oh sigh, it is easier to point you a good tutorial that write here n pages of text.
So here you go: http://firstpod.tripod.com/cpp21/ and chapter 12 and 13. It's really a good tutorial, nothing too long.
Making error is human, but for messing things thoroughly it takes a computer
That's the same reason I did not reply. Kudos to you for giving him a link. I'm just lazy.
Polymorphism is a number of things, I think, but what I got from it (sort of) is a bit like this:
int function(int a, int b)
and
int function(int c, double a)
are different
Try this website i found it very interesting
http://zeus.eed.usv.ro/misc/doc/prog/c/msvc12/vcl22.htm
I'm sure these are all great links, and I'm also sure polymorphism is in the faq, but here's my 2 cents worth.
Polymorphism is used when you want to call a function of the same name that accepts different kinds of inputs.
For example if I have a that accepts integers like this:
void afunction(int a, int b)
{
printf("a %d, b %d",a,b);
}
but I also want my function to accepts floating points, I just continue to declare the function as usual
void afunction(float a, float b)
{
printf("a %f, b%f",a,b);
}
so now, if I have integers or floating point numbers, I can call the same function... please note however, that for every different input you have, you have to declare another function... polymorphism allows you to use the same function to accept different or multiple inputs, or even process different inputs in different ways.
Maybe I'm totally outa whack here.. but I was under the impression that your examples above are simply overloaded functions? I always though polymorphism requires the use of a classes.
for ex:
Code:/* Header file #ifndef MAIN_H #define MAIN_H class CParent { public: virtual int foo() const { return 1; }; }; class CChild : public CParent { public: int foo() const { return 2; }; }; #endifCode:/* cpp file #include <iostream> #include "main.h" using namespace std; void polymorphic_func(CParent &); void non_polymorphic(CParent); int main() { CParent p; CChild c; non_polymorphic(p); non_polymorphic(c); polymorphic_func(p); polymorphic_func(c); return 0; } void polymorphic_func(CParent &p) { cout << p.foo() << endl; } void non_polymorphic(CParent p) { cout << p.foo() << endl; }
Polymorphism is calling virtual function from a class, but keep in mind you must use a pointer to call the function.
CuriousJay & SilasP are correct. Polymorphism is an OOP approach whereby code calls base class virtual functions with a base class pointer, which then calls the appropriate derived class function of the same name. E.g., code can call base class Shape virtual function Draw with a pointer, and the Draw function in the correct derived class, say Square or Circle, will be called.
This means that you don't need to know what derived function is called at compile time. A user might pick which derived Shape to draw at run time, it might be done randomly in a game, etc. New derived classes can be added more easily as well, since new class specific code doesn't need to be added to the program (at least as far as the new class working).
This type of thing could be done with switch statements or multiple if checks, but that is usually slower and much more code writing.
Yeah, they're right. That's what I get for trying to think C during class :P Thanks for the correction guys.
-Max
Poly - many
morph - shape
eg:
Class A {}
Class B: public A{}
Class C: public A{}
Class D: public C{}
void main(void)
{
A a;
B b;
C c;
D d;
A* ap;
ap = &a;
ap = &b; // valid because B is derived from A
ap = &c; // valid because C is derived from A
ap = &d; // valid because D is derived from C, which is derived from A
}
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.