Jump to content
  • Advertisement
Sign in to follow this  
Tradone

Virtual Methods vs. Polymorphism

This topic is 4506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
I understand the basics of polymorphism, but having trouble distinguishing between polymorphism and virtual methods.

are they essentially the same thing?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 A
Called foo() on object of class B but get foo() on class A
With virtual:
Called foo() on object of class A and get foo() on class A
Called 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).

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!