Thread: what's the purpose of virtual functions

  1. #1
    vfunction1
    Guest

    Question what's the purpose of virtual functions

    I understand how virtual functions work, my question is what's the purpose of a virtual function.

    Is it just so you can more easily toggle between the same function in base and derived classes?

    And what differs virtual functions from just creating seperate functions in the base and derived classes alone.

    such as
    Code:
    class base
    {
    public:
         virtual func()
         {
              what's better about having a func in the 
              derived class?
         }
    };
    
    class derived
    {
    public:
         func2()
         {
              why not have this instead of a virtual           function?
         }
    };

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It all stems from the concept that a parent object pointer can point to a child object. A virtual function is dynamically bound -- you can call different versions of the same function on an object through a pointer or reference to a base object type and depending on how the function is defined in the child class, you will get the differing results that you want.

    Here's a much longer explanation -- I mention virtual functions around the middle.

  3. #3
    thank_you
    Guest

    Talking

    polymorph, you seem to know a lot, thanks.

    Where did you learn this all

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You just find that you learn a lot when you have no life other than programming.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Here's a shorter explanation. It is a summary of what's in Ivor Horton's Beginning Visual C++ 5.0 book, chapter 10. Suppose you have the following class that contains info on a box's dimensions and methods for outputting the volume of the box to the screen:
    Code:
    class CBox
    {
        public:
            void ShowVolume(void)
            {
                cout << endl << "CBox usable volume is " << Volume();
            }
            double Volume(void)
            {
                return m_Length * m_Height * m_Breadth;
            }
            CBox( double lv=1.0, double bv=1.0, double hv=1.0) : m_Length(lv),
                                                                 m_Breadth(bv),
                                                                 m_Height(hv)
            {
            }
        protected:
            double m_Length;
            double m_Breadth;
            double m_Height;
    }
    Now you create a derived class to contain glassware where the volume will be 15% less than the usual volume given by a box with the same dimensions:
    Code:
    class CGlassBox: public CBox
    {
        public:
            double Volume(void)
            {
                return 0.85 * m_Length * m_Breadth * m_Height;
            }
    }
    If you create and output the volumes of two different boxes using the following program fragment:
    Code:
    CBox myBox(2.0,3.0,4.0);
    CGlassBox myGlassBox(2.0,3.0,4.0);
    myBox.ShowVolume();
    myGlassBox.ShowVolume();
    You get the following results:
    CBox usable volume is 24
    CBox usable volume is 24

    The first result is correct but the second should be 15% less. This is because the call to the Volume() function is being set once by the compiler for the version defined in the base class. This is called static linkage and the call gets fixed to only that version when the code gets compiled. What is needed is to have the actual version of the Volume() function that is called by ShowVolume() function to be determined based on the kind of object being processed in what is know as dynamic linkage. This is why you need virtual functions. Simply adding the word virtual prior to the Volume() function definition in the base class will solve this problem although it is good practice to also put it in the derived class as well. You would then get the proper results:
    CBox usable volume is 24
    CBox usable volume is 20.4
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by hk_mp5kpdw
    What is needed is to have the actual version of the Volume() function that is called by ShowVolume() function to be determined based on the kind of object being processed in what is know as dynamic linkage.
    Watch out, it's dynamic binding, not dynamic linking. Dynamic linking is when the location of the function isn't stored until the executable is run.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Watch out, it's dynamic binding, not dynamic linking. Dynamic linking is when the location of the function isn't stored until the executable is run.
    I was doing quite a bit of paraphrasing and summarizing of what was in Ivor Horton's book, the exact mention of this is on page 387 and states as follows:
    What we were hoping for in this example was that the question of which Volume() function call to use in any given instance would be resolved when the program was executed. This sort of operation is referred to as dynamic linkage, or late binding. We want the actual version of the function Volume() called by ShowVolume() to be determined by the kind of object being processed, and not arbitrarily fixed by the compiler before the program is executed.
    You think he made a mistake?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by hk_mp5kpdw
    You think he made a mistake?
    yup

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    I dunno man. From the reading I've done "binding" and "linking" are often used interchangably. I don't think he made a mistake. It would mean many many other highly respected programmers make that same mistake all the time.

    Personally, I don't know for sure. But thats the way I've seen it written.

  10. #10
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Bjarne Sroustrup makes the distinction between the two in his book "The design and evolution of C++". The man who created the language keeps the definitions quite seperate.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Yeah, they are entirely different.

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    Bjarne Sroustrup created C++...which has nothing to do with the use of the terms binding or linking. They are not under c++ or any language. Yes, linking is a word used for something much different than binding. Many programmers also use it to express binding. Words can mean more than one thing, and are generally defined by their usage.

    Anyway...for my money I am going with the experianced programmers who have published books and made a name for themseleves over someone who has been programming in c++ for a little over a year.

    You come to your own conclusion.


    Edit:

    http://www.pcwebopaedia.com/TERM/b/bind.html

    bind:
    To assign a value to a symbolic placeholder. During compilation, for example, the compiler assigns symbolic addresses to some variables and instructions. When the program is bound, or linked, the binder replaces the symbolic addresses with real machine addresses. The moment at which binding occurs is called bind time or link time.

    http://www.pcwebopaedia.com/TERM/L/link.html
    link:
    To bind together.

    I think this demonstartes why many programmers use the terms interchangably.
    Last edited by dynamic_cast; 01-10-2003 at 09:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 11:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 04:13 PM
  3. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM