Results 1 to 7 of 7

Thread: virtual function

  1. #1

    virtual function

    what is the use of virtual function in c++


  2. #2

    Re: virtual function

    the virtual function can be defined in base class and redefined by the derived classes


  3. #3

    Re: virtual function

    [QUOTE=abhijithvardhan;21822]the virtual function can be defined in base class and redefined by the derived classes.and it is used to control the technic of acquarism of properties.
    the general syntax is
    class d_class name:virtual b_class name
    where,
    d_class name, is name of the derived class and
    b_class name, is name of the parent (or) base class name


  4. #4
    Contributing Member
    Join Date
    Nov 2007
    Answers
    46

    Re: virtual function

    virtual function:

    - share the properties of classes.

    example:
    there are two super classes:
    parent and mother and son
    now son is the subclass of the two classes i.e parent and mother.
    so son share the properties of both


  5. #5
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: virtual function

    C++ virtual function is member function of a class, Declared with virtual keyword and Usually has a different functionality in the derived class and
    this function call is resolved at run-time .
    The Reason why a C++ virtual function will be used is to have a different functionality in the derived class.


  6. #6
    Junior Member
    Join Date
    May 2008
    Answers
    2

    Thumbs up Re: virtual function

    I would like to explain briefly how it actually works

    Whenever a program has a virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes that contain one or more virtual functions. The object of the class containing the virtual function contains a virtual pointer that points to the base address of the virtual table in memory. Whenever there is a virtual function call, the v-table is used to resolve to the function address. An object of the class that contains one or more virtual functions contains a virtual pointer called the vptr at the very beginning of the object in the memory. Hence the size of the object in this case increases by the size of the pointer. This vptr contains the base address of the virtual table in memory. Note that virtual tables are class specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains. This virtual table in turn contains the base addresses of one or more virtual functions of the class. At the time when a virtual function is called on an object, the vptr of that object provides the base address of the virtual table for that class in memory. This table is used to resolve the function call as it contains the addresses of all the virtual functions of that class. This is how dynamic binding is resolved during a virtual function call.



  7. #7
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: virtual function

    Here is an example of virtual function

    class India
    {
    public:

    virtual void country()
    {
    cout<<"BaseClass: I am in India\n";
    }
    };

    class AP : public India
    {
    public:
    void country()
    {
    cout<<"DerivedClass: I am in AP\n";
    }

    };

    void main()
    {
    India *x,*y;
    x = new India();
    x->country();

    y = new AP();
    y->country();

    }

    Output:
    BaseClass: I am in India
    DerivedClass: I am in AP

    If the function had not been declared as VIRTUAL ,then base class function would have been called all the time.Because the function address would have been statically bounded during compile time.But due to the virtual keyword,linking is done during runtime and hence derived class function is invoked for the second time in this example.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact