RE: Polymorphism vs. Overloading

"John. M. Miano" <visualw@cnj.digex.net>
Mon, 31 Oct 1994 22:44:48 GMT

          From comp.compilers

Related articles
Polymorphism vs. Overloading gdevivo@conicit.ve (1994-10-22)
RE: Polymorphism vs. Overloading visualw@cnj.digex.net (John. M. Miano) (1994-10-31)
| List of all articles for this month |
Newsgroups: comp.compilers
From: "John. M. Miano" <visualw@cnj.digex.net>
Keywords: polymorphism
Organization: Compilers Central
References: 94-10-144
Date: Mon, 31 Oct 1994 22:44:48 GMT

gdevivo@conicit.ve (Gabriela O. de Vivo) writes:
>Now, I wonder if some of you could help me by stating more precisely (and
>rigourosly) the exact nature of the difference.


I think the best way to explain the difference is that in overloading a
function is bound to a class/structure.


With polymorphism a function is bound to an object (An object being a
the manifestation of some class/structure).


Since polymorphism means bound to an object it is restricted to member
functions/methods. In Ada/C++ you can create overloaded functions but
polymorphism is not applicable to these.


Overloading is generally resolved at compile-time while polymorphism must be
resolved at run time.


Example Polymorphism vs. Overloading


class X
{
public:
virtual void func (int x) {
cout << "I'm int in X " << x << endl << flush ; }
virtual void func (char *x} {
cout << "I'm char in X" << x << endl << flush ; }
} ;


class Y : public X
{
public:
virtual void func (int x) {
cout << "I'm int in Y " << x << endl << flush ; }
virtual void func (char *x) {
cout << "I'm char in Y " << x << endl << flush ; }
} ;


main ()
{
X xo, *xp1, *xp2 ;
Y yo ;


xp1 = &xo ;
xp2 = &yo ;


// Overloading relates to whether func (char *) or func (int) is
// called. Polymorphism relates to whether X::func or Y::func is
// called (or a better way to say this is whether xo's func is
// called or yo's func is called).


xp1->func (1) ; // calls X::func (int)
xp1->func ("some text") ; // calls X::func (char *)


xp2->func (1) ; // calls Y::func (int) ;
xp2->func ("some text") // calls Y:: func (char) ;


return 0 ;
}
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.