Advertisement

Polymorphism??

Started by November 22, 2002 09:54 PM
6 comments, last by MattS423 21 years, 11 months ago
What is polymorphim? i might know what it is, just not the name, but what is it?
Programmers of the world, UNTIE!
Liskov Substitution Principle: "Derived class objects can be used where base class objects are expected."

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Polymorphism is an abstraction that let''s one piece of code treat different cases (including the dreaded special cases) in a uniform manor.

Usually when one talks about polymorphism they refer to the run-time polymorphism that object-oriented programming engenders.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

    class CDog{public:   virtual void Bark(void)   {      printf("Bark!");   }};class CStBernard : public CDog{public:   virtual void Bark(void)   {      printf("WOOF!");   }};class CTeacupPoodle : public CDog{public:   virtual void Bark(void)   {      printf("Yip!");   }};void main(void){   CDog *dog1 = new CDog;   CDog *dog2 = new CStBernard;   CDog *dog3 = new CTeacupPoodle;   // Even though they are all CDog pointers, they act differently   dog1->Bark();   // Bark!   dog2->Bark();   // WOOF!   dog3->Bark();   // Yip!   // Yeah, ok, clean up the memory!   delete dog1;   delete dog2;   delete dog3;}    


Not even sure if this source works, but I think, hopefully, it makes the point.

[edited by - Waverider on November 22, 2002 11:24:12 PM]
It's not what you're taught, it's what you learn.
In the context of programming languages, polymorphism (poly == many, morph == forms) means, that expressions having the same syntax can be applied over different types of entities to produce a useful but differentiated meaning.

C++ provides various types of polymorphism, namely parametric, inclusion and ad-hoc. Parametric polymorphism is achieved through use of template mechanisms:

  template<typename T>T add1(const T& t){  return T+1;}//...float f1 = 10.0;float f2 = add1(f1);int i1 = 5;int i2 = add1(i2);  

Here, the function add1 can be called for any type for which binary operator+ makes sense. The fact that adding means a different physical operation takes place for floating point and integral values, for instance, and different again for strings, means that the function is polymorphic (and so is addition for that matter). I.e. the syntax doesn''t change no matter what type you call add1 with.

Inclusion polymorphism is also known as subtype polymorphism and Waverider''s example would have been good if he wasn''t invoking C++ undefined behaviour.

Ad-hoc polymorphism includes both overloading and coercion. Hopefully, overloading is fairly obvious, and it is similar to the add1 example, but with explicit overloads. Here''s one example of how coercion polymorphism works:


  class C{public:	C(int x)		: x_(x)	{}private:	int x_;};void foo(C c){}int main(){	int i = 10;	foo(i);  	// foo actually takes a C object, but will			// coerce an int into a C due to the available conversion ctor}  


You can call foo with either a "C" or an "int", since there is a conversion from int to C, so the compiler coerces the type to become a C.
I''ve never heard of coersion before, can you explain a little more? I thought if you sent a type that wasn''t in the functions original parameters the compiler would flag it as a syntex error, saying something like can not convert parameter one to type class C

Can you explain further please?
Advertisement
quote: Original post by ph33r
I've never heard of coersion before, can you explain a little more?

It might help to think of C's type-casting and standard conversion sequences. For example, if you have a function with this signature:
void foo(int);  

You can call foo with an int, but you could also call it with a char type, since the compiler know how to coerce a char into an int. That means the syntax for calling foo with either type is identical - see my earlier definition of polymorphism.
quote:
I thought if you sent a type that wasn't in the functions original parameters the compiler would flag it as a syntex error, saying something like can not convert parameter one to type class C

That's true if it cannot perform a conversion. However, C++ allows us to create our own conversion sequences via either a conversion constructor or a conversion operator. My earlier example demonstrates the conversion constructor, which tells the compiler how to coerce an int to become a C, and so functions taking a C can also accept an int without the programmer having to use any special call syntax. Note that conversion constructors can cause unexpected problems, so the C++ language defines the keyword "explicit", which allows you to create a conversion constructor without simultaneously defining an implicit conversion.

[edited by - SabreMan on November 24, 2002 10:32:20 AM]
Yeah...i knew what that was...just not the name...

thanks guys.
Programmers of the world, UNTIE!

This topic is closed to new replies.

Advertisement