Virtual Methods vs. Polymorphism

Started by
15 comments, last by snk_kid 18 years, 1 month ago
Okay, I think I am grasping oop concepts from this article at developer.com :http://www.developer.com/tech/article.php/3488176 But I was wondering what virtual methods were and what they are used for :http://newdata.box.sk/bx/c/htm/ch12.htm [Edited by - Tradone on March 14, 2006 5:37:57 PM]
Advertisement
They are used to implement polymorphism.
Basically what you want to do when using virtual functions is call a method of an object without concern for the object type and the proper implementation of that method, as long as that method exists. This is what polymorphism employs in OOP : a function with multiple implementations, depending on which object it is applied to. You need not know how an object does a certain modification, as long as it does it and it has the same name (so you cand access it through a pointer).
Hope this makes sense.
-= wolfwood :: rpg =-
I understand the basics of polymorphism, but having trouble distinguishing between polymorphism and virtual methods.

are they essentially the same thing?
Quote:Original post by Tradone
I understand the basics of polymorphism, but having trouble distinguishing between polymorphism and virtual methods.

are they essentially the same thing?

Virtual methods enable polymorphism. Polymorphism is treating several different types as the same, but retaining their specific functionality. Virtual methods is the way you do that.

Objects vs object oriented programming. Stock cars vs NASCAR. And so forth.

CM
Quote:Original post by Tradone
I understand the basics of polymorphism, but having trouble distinguishing between polymorphism and virtual methods.

are they essentially the same thing?


Think of it this way. The virtual keyword is one way C++ implements polymorphism. Templates are another way that C++ implements polymorphism. Smalltalk implements polymorphism using messages. Haskell implements polymorphism through type inference.

For what it's worth:
#include <iostream>using namespace std;namespace WithoutVirtual {    class A            { public: void foo() { cout << "class A\n"; } };    class B : public A { public: void foo() { cout << "class B\n"; } };}namespace WithVirtual {    class A            { public: virtual void foo() { cout << "class A\n"; } };    class B : public A { public:         void foo() { cout << "class B\n"; } };}int main() {    WithoutVirtual::A  a;    WithoutVirtual::B  b;    WithoutVirtual::A *pa;    WithVirtual::A  va;    WithVirtual::B  vb;    WithVirtual::A *pva;    cout << "Without virtual:\n";    pa = &a;    cout << "Called foo() on object of class A and get foo() on ";    pa->foo();    pa = &b;    cout << "Called foo() on object of class B but get foo() on ";    pa->foo();    cout << "With virtual:\n";    pva = &va;    cout << "Called foo() on object of class A and get foo() on ";    pva->foo();    pva = &vb;    cout << "Called foo() on object of class B and get foo() on ";    pva->foo();}


This prints out:
Without virtual:Called foo() on object of class A and get foo() on class ACalled foo() on object of class B but get foo() on class AWith virtual:Called foo() on object of class A and get foo() on class ACalled foo() on object of class B and get foo() on class B

Without virtual, B can't really take on the form of A. Virtual allows B to take on the form of A (i.e. B has more than one form or is polymorphic).
Just to clarify something: polymorphism is a mecanism that binds a function to more than one definition. Virtual methods implements a special kind of polymorphism (inheritance (or subtyping) polymorphism). If you define 2 functions that have the same name and a different list of parameters, you are using another kind of polymorphism called "ad hoc polymorphism".

More informations here (again, on wikipedia)

Regards,
Question, is it "fake" polymorphism when you program more than one object to have the same function name (which does basically the same thing), but with no inheritence involved?

Like say, a renderable object with a render function..

We have milkshape models, they have a draw function
we have user defined geometry, they have draw functions..


etc etc..

----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
Quote:Original post by Shamino
Question, is it "fake" polymorphism when you program more than one object to have the same function name (which does basically the same thing), but with no inheritence involved?

Polymorphism is a very general term in computer science. In short, yes, having multiple types with member functions of the same name can be considered polymorphism depending on how they are used. In addition to that, simple overloaded functions can be considered polymorphism, and even common built-in language operations such as +, -, etc. can be considered a form of polymorphism if they can work with different built-in types such as int and float in c++.

The difference is that, in a statically-typed language, those are all forms of compile-time polymorphism whereas virtuality is usually a runtime language facility. That means that you can use virtuality to give to polymorphic behaviour when the choice is made at runtime, whereas with the other forms of polymorphism just described must determine the implementation to be used at compile-time.
Quote:Original post by Shamino
Question, is it "fake" polymorphism when you program more than one object to have the same function name (which does basically the same thing), but with no inheritence involved?


If by "fake" you mean "parametric", then yes.

(Go read the article Emmanuel Deloget linked to.)

This topic is closed to new replies.

Advertisement