Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    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 :D ). 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... thanks

    ps. i did some google searches and cant find anything that i cant do myself
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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?
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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 03:36 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    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.
  12. #7
  13. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Originally posted by 7stud
    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."

    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?
    -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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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.

    :cool:
  18. #10
  19. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    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.
  22. #12
  23. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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!!!!
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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:
    Polymorphism is such a powerful feature of object orientated programming that you will use it in the majority of your C++ programs.
    Later in the chapter it says:
    ...the behavior that is appropriate to the object pointed to by the pointer is selected at runtime.(It's as though the pointer has a built-in switch statement that tests for the type, and selects the function to be called accordingly.)

    This is an extremely powerful mechanism....It's commonly used with interactive applications, where the type of the input is up to the whim of the user.
    We'll get to the bottom of those statements sooner or later.
    Last edited by 7stud; March 20th, 2003 at 02:44 AM.
  26. #14
  27. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    Originally posted by MJEggertson

    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"....
    i was just going from what 7stud said:
    Originally posted by 7stud

    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?
    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:

    polymorphism is another technique that enables the reuse of software objects. polymorphism allows the software that uses an object to work regardless of the specific class of the object. in other words, polymorphism enables an object to send the same message to different receivers without knowing precisely what behavior will be invoked by the message when it is recieved.

    all messages in objective-c use polymorphism. in many cases it is not possible for the sender of an objective-c message to know the class of the object that receives the message. each receiver will invoke different behaviors upon receipt of the message. java and c++ also support polymorphism to various degrees. along the spectrum of flexibility, c++ compilers require detailed knowledge about all objects whose behaviors are used. objective-c does not require any knowledge about the objects that are used at compile time. java falls between the two extremes.
    ('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 08:02 AM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    What in the heck is objective-c?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo