|
-
Jun 19th, 2002, 01:07 PM
#1
Polymorphism
What is it? Where can I find an example of it? Is it related to Inheritence? Is it related to Virtual functions?
I need a tutorial!
NOMAD
-
Jun 19th, 2002, 01:23 PM
#2
PowerPoster
-
Jun 20th, 2002, 01:23 AM
#3
So is this polymorphism?
So is this polymorphism?
Code:
class BaseClass
{
protected:
int a;
int b;
public:
virtual Product(void)
{ return (0); }
};
class Type1Class: public BaseClass {
public:
int Product(void)
{ return (a*b); }
};
class Type2Class: public BaseClass {
public:
float Product(void)
{ return (a / b); }
};
Is this polymorphism? I keep finding these internet files that say polymorphism but only seem to do some inheritence and/or virtual function deal...
NOMAD?
Am I close?
-
Jun 20th, 2002, 11:43 AM
#4
PowerPoster
Yes, because you can use the Product function in two different class doing two different things. So in your base class, you're using the Product function do nothing but in the derieved class, you can doing some multiplication.
-
Jun 20th, 2002, 12:00 PM
#5
Monday Morning Lunatic
Polymorphism -- many shapes (or similar ).
What abdul has told you is correct. Polymorphism is equivalent to using virtual functions; you can have a base class pointer pretending to be a derived class (the many shapes bit).
For example:
Code:
class Shape {
public:
virtual ~Shape(); // needed for proper cleanup
// pure virtual, you don't need to give it any
// code, and the compiler won't let you actually
// create one, but you can use it as a pointer
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() { /* do something */ }
};
class Square : public Shape {
public:
void draw() { /* something else */ }
};
The main benefit here, is that you can have a std::list<Shape*>, and you can keep adding circles or squares, without needing to worry when the time comes to draw them all. Just keep calling ->draw() on the Shape* pointers, and the compiler will insert the necessary code to look up in the class what *actual* type it is.
If that sounds complicated...it is...sadly
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 20th, 2002, 02:25 PM
#6
Ok so, now that I know a little more about polymorphism.
Heres why I wanted to know:
I have an array, if I make the array of say Shape (go with parksie example) how would it know if it was a Circle or Square. I see parksie used ->draw... should my array be of pointers of type Shape?
Also Parksie, you wrote 'virtual void' does this mean that they all need to return the same type? Does it have to bee this way?
Thanks guys!
This is REALLY helping!
NOMAD
-
Jun 20th, 2002, 04:31 PM
#7
Monday Morning Lunatic
Yep, they're of type Shape*. How the compiler determines the type is implementation-dependent, so you can ignore it for now (when you get more used to the effects, you can look it up, it's to do with virtual function tables -- if you've ever seen references to vfptr, that's it).
The virtual tells it to use it as a virtual function, and this is reflected through all the derived classes (you don't need to specify it).
And yes, they must all return the same type (well, you can return different polymorphic types, within certain limits which I won't try and go into here).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 21st, 2002, 11:05 AM
#8
Thanks! I think I got my Polymorphism question out of the way! Now only 9,999,999,999 questions left!
NOMAD!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|