| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Polymorphism ? |
Hello Neil!
Replying to a message of Neil Heller to andrew clarke:
ac>> Function overloading.
NH> It seems to me as though this is one type.
ac>> This can then can extend to operator overloading if the need arises.
NH> Are these intrinsically the same thing? I think that I forgot my
NH> brain in the laundry room the last time I washed my clothes.
If you can overload operators, then they are usually classified similarly -
but not all languages that allow function overloading also allow operator
overloading (e.g. Java).
NH>> Can anyone tell me a definitive and authoritative (and possibly
NH>> verifiable) source that I can use? The last four words are the
NH>> key to that sentence.
Me? ;-)
NH> Given the following scenario:
NH> Class A
NH> Class B derives from Class A
NH> Class C derives from Class A but does different stuff than Class B.
NH> Somewhere in the main program, a pointer (or referent, depending on
NH> the language) of type Class B is instantiated. That object does a
NH> few things and then for whatever reason, is coerced (typecast) to a
NH> Class A pointer. It does something with a Class A object and then
NH> resumes life again as a Class B pointer.
NH> Does this changing object represent a form of polymorphism? How many
No. Here's an example of polymorphism. Given the same three classes, I
give you a function foo:
int foo(A& a) { a.do_stuff(); }
You can call it as: foo(b) or foo(c), where b and c are of type B and C,
respectively (and obviously ;->). Since class A's do_stuff function may
be overridden by B or C or both, what happens when you call foo(a), foo(b),
and foo(c) could be drastically different things.
The canonical text book example is something like this:
class A { public: virtual void do_stuff() { cout << "In A's
do_stuff" << endl; } };
class B : public A { public: void do_stuff() { cout << "In B's
do_stuff" << endl; } };
class C : public A { };
int foo(A& a) { a.do_stuff(); }
int main()
{
A a;
B b;
C c;
foo(a); foo(b); foo(c);
return 0;
}
This should print out:
In A's do_stuff
In B's do_stuff
In A's do_stuff
This is due to the polymorphism of the A reference. While foo thinks it
has a reference to A, it actually can have a reference to any object
derived from A.
NH> different types of polymorphism are there? Is there an actual number
NH> I can get my hands on?
That I don't know...
Darin
---
* Origin: Tanktalus' Tower BBS (1:250/102)SEEN-BY: 633/267 270 @PATH: 250/102 99 10/345 106/1 2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.