|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
virtual functions and polymorphism
I've been doin a lot of reading up about polymorphism and virtual functions. This is a topic that is very interesting to me, but i have a problem. I fully understand the concepts and implementation - I've made a bunch of console programs, some using Shape as base class and then deriving a bunch of shapes, another with Employee as a base class and then deriving a bunch of different Employee types, then using base class pointer arrays to execute the virtual functions polymorphically(ehh is that a word
). But my problem is that all this stuff is pretty boring and pointless. Does anyone have any code with Polymorphic functions actually doing something worthwhile and interesting?? Perhaps something in win32 api ?? If u do i'd like to take a look at it if u dont mind... thanksps. i did some google searches and cant find anything that i cant do myself |
|
#2
|
|||
|
|||
|
I can only give you some other theoretical idea about it, because the code is (c) by our company so i am not allowed to post it (it is several thousand lines anyways... )
I wrote a program that supports quite many graphical formats. It was done in Delphi, but this should not matter. I made a base class CImage with a virtual method SaveToFile(). From this, i derived CJPEGImage, CTIFFImage, CGIFImage, CBMPImage, ... . All implement the SaveToFile() method. In a workspace, if someone presses the "save all" button, i now loop through all images and call image[...].SaveToFile() so it always picks the right save function and in writing my code, i donīt have to take care of the specific image format. i.e.: (pseudocode) I donīt need : if (image[i] is CJPEGImage) (CJPEGImage*)image[i]->SaveToFile(...); if (image[i] is CTIFFImage) (CTIFFImage*)image[i]->SaveToFile(...); i just write: image[i]->SaveToFile(...); This is just one of many examples where i used polymorphism. Does this make clear what it is good for?
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
||||
|
||||
|
yes, thanks for ur response, i do understand the concept of what it is good for... perhaps i didnt really state my question correctly. I was wondering what other kind of uses polymorphism had besides the obvious. LIke, is it used in programming graphical animations a lot, or perhaps in AI? That kind of stuff is more interesting to me than computing the salary for a bunch of different Employee types derived from an abstract Employee class with a virtual CalcPay() function , (which is pretty simple and is all my teacher is teaching us about it).
|
|
#4
|
|||
|
|||
|
Graphical animation should be a good example too. I donīt know much about AI, so i canīt tell about that one.
Say you write a game. A 3D game. You have different types of animation, but all need to occur in the same time (eg. 25 frames per one second). 3D: the characters in the game move their heads, arms and legs (-> bone animation / mesh deformation) 3D: the characters move in the world depending on other influences (things falling, enemies moving, the player moving -> transformation and rotation) 2D: you have a scoreboard that has spinning wheels on it. (-> only switching pre-made bitmaps) 2D/pseudo-3D: you have flickering lights. (switching textures -> only switching pre-made bitmaps as above) In your Engineīs rendering loop, you loop through all graphical objects and call their animate() method. Not sure if this is what you were asking for. IMHO polymorphism is not that big of a deal... |
|
#5
|
|||
|
|||
|
infamous41md,
To me your question seems the same as asking: "I've been using integer variables to count apples or oranges in my programs, but that's pretty boring, so can anyone tell me if integers are used with interesting stuff like AI or graphical animations." How can anyone answer that question? I guess if somewhere in the AI program they used a loop, then it's possible they used an integer variable. An integer variable, or inheritance, or polymorphism, or pointers, are all tools for you to use as you see fit. Whether you use all of them or some of them or none of them is up to you. M.Hirsch(did I get it right this time?), It seems like polymorphism is just a substitute for a switch statement. I guess if you had 1 million cases, it would sure save you a lot of typing, but there must be more to it than that isn't there? Last edited by 7stud : March 18th, 2003 at 02:36 AM. |
|
#6
|
|||
|
|||
|
God damit. I had a response, then my isp's dns servers crashed and lost it...
Anyways, though polymorphism is nice in the sense that it allows derived classes to re-define existing methods and allows base class pointers to operate on derived class methods, I think probably more important is the ability to define abstract classes (classes that have one or more pure virtual functions). Code:
class Document {
virtual Document* GetDoc(void) = 0;
};
Now a Document class can't be instantiated since GetDoc isn't implemented, but it allows the Document class to define a protocol/interface that all derived classes must implement. So even though the GetDoc method makes not sense in the raw Document context, Document methods can still make calls to the method, knowing that any derived class must implement it. |
|
#7
|
||||
|
||||
|
Quote:
-yes that it is basically same thing i was asking...but the reason i asked is b/c i read and hear about people making this big deal about polymorphism and how great it is and how important it is and etc... but in the instances that i have used it, it just seemed pretty unspectacular. So, i was curious if someone more experienced than myself in programming could point out some instances that showed the "spectacularness" of polymorphism. perhaps problems that could only be solved using it, or problems that were made 10 times easier thru it. Yes, it is a substitute for Switch statements, but i also thought it had more to it than that since i hear and read so much about it. But apparently it is no more than a glorified switch statement, and i also have been told it takes a considerable amount of overhead, so perhaps M Hirsch is right in saying it is not a very big deal. |
|
#8
|
|||
|
|||
|
"But apparently it is no more than a glorified switch statement, and i also have been told it takes a considerable amount of overhead, so perhaps M Hirsch is right in saying it is not a very big deal."
It's not likely programmers use virtual functions and polymorphism with increased overhead if they don't provide added functionality. MJEggertson provided one example why polymorphism can be useful. I'm sure there are others. |
|
#9
|
|||
|
|||
|
Another thought:
If I want to add another graphics format one day (say, PSD). First I implement a new class for loading and displaying PSDs. But THEN: The additional work (besides implementing the new format) is: Without polymorphism: - find all functions that deal with format specific stuff - update 50 lines of code spread over 20 source files With polymorphism: - update the two load/save dialogs to show "PSD" files too. ![]() |
|
#10
|
|||
|
|||
|
i think this is more a pyschological than technical question - it's maybe how something allows someone to think, rather than how code allows the computer to operate? even though it does just amount to something that's pretty usual and standard. - it's a different way of looking at the same thing.
eg- object oriented programming - that pans out to be just procedual programming. technically that's what it ends up being. but oop allows people to think about that procedual code in quite a different way. - oop allows you to view procedual programming in a different way. so if polymorphism sparks your imagination and allows you to think more creatively and originally - great, use it. if it doesn't, don't. (i'm not sure what polymorphism is btw but it doesn't matter) |
|
#11
|
|||
|
|||
|
On the surface, it may seem so, but virtual functions and polymorphism aren't just another way of doing things. Virtual function calls are "late bound". The issue of late binding is definitely beyond my capability to discuss, but it does differ from the regular binding of function calls of objects, which are early bound.
Basicly though, late binding refers to resolving functions at the point of the function call, which is how polymorphism in C++ works. This is in contrast to early binding, where the compiler knows exactly what the context of an object is, what the function calls are, and can perform optimizations while building the executable. It is the overhead in maintaining late-bound objects (virtual base and virtual function pointers) that makes polymporphic objects inherrently slower than early-bound objects. Quite often though, the overhead involved is insignificant compared to the portability and flexibility of the generated objects. |
|
#12
|
||||
|
||||
|
Well, thanks for the various replies, it gave me a slighly firmer grasp on the usefullness of polymorphism. I do however still feel its kind of overrated- being that it is basically a glorified switch statement. I have found a use for it though in program i am writing, or beginning to write i should say. It's a win32 program that plays tictactoe with you. My idea is to make an abstract class that is going to be the AI for the game. Then im going to derive 3 separate child classes, each one for a progressively smarter playing engine, the smarter the engine the more options it will have when making moves. It's amazing how much work this is taking just to play a freaking tic tac toe game!!!!
|
|
#13
|
||||
|
||||
|
I was thinking some more about this switch statement idea as a substitute for polymorphism, but how do you determine the type of an object?
if (image[i] is CJPEGImage) (CJPEGImage*)image[i]->SaveToFile(...); if (image[i] is CTIFFImage) (CTIFFImage*)image[i]->SaveToFile(...); It's easy to write in english, but what about in C++. There's a typeof operator, but that will only return a string "object". The only way I could think of to test for different objects was to use the sizeof operator, but if you have objects that are the same size, it won't work. Finally, you can't store objects of different types in an array anyway. Any ideas? Is it even possible? infamous41md, Similar to what you've been hearing, my book starts off the chapter on virtual functions and polymorphism saying: Quote:
Later in the chapter it says: Quote:
We'll get to the bottom of those statements sooner or later. Last edited by 7stud : March 20th, 2003 at 01:44 AM. |
|
#14
|
|||||
|
|||||
|
Quote:
i was just going from what 7stud said: Quote:
it seems to me something that has 'looking at something in a different way' built into it can be surprisingly powerful and shouldn't be undervalued, even if it does just pan out to be much the same thing: allows you to tackle it differently because it naturally looks at it differently, therefore allows/forces you to look at it differently. i'm attempting, and starting to learn objective-c at the moment. here's what the book i've got says about polymorphism: Quote:
('message sending' being *kind of* akin to function calling. 'recievers' being objects that are being sent a message.) out of interest: are there many people on here that know objective-c? Last edited by balance : March 20th, 2003 at 07:02 AM. |