CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Location
    Bangalore, INDIA
    Posts
    20

    Need for Virtual Function?

    Hi,

    I have some doubts in Virtual functions.


    What is the use of Virtual Functions?

    In which scenario, virtual functions will be useful ?

    For what real time problems, virtual function is preferred?

    Please clear my doubt.


    Thanks,
    Arun AC
    ++++++++++++++++++++++++++++++++++++++++++++++++++++

    I'm not smart, but I like to observe. Millions saw the apple fall,
    but Newton was the one who asked why.

    -- William Hazlitt
    ++++++++++++++++++++++++++++++++++++++++++++++++++++

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Need for Virtual Function?

    What is the use of Virtual Functions?
    Read Inheritance — virtual functions.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Need for Virtual Function?

    Quote Originally Posted by sunkingac
    What is the use of Virtual Functions?
    Virtual functions are defined in base classes and overridden in derived classes.

    Say the base class is Fruit and it has a virtual print() function. This function can now be overridden in derived classes like Apple and Banana which supplies the specific details about how each fruit looks.

    Now say you have a Fruit variable f. By doing f->print() a different fruit will be printed depending on which derived object f points to.

    This allows you to write general code using Fruit variables only and it will work with any derived object. If you want to extend your design with say an Orange the old code will work without changes. Orange just overrides print and an orange will appear when f->print() is called (and f is pointing to an Orange object).
    Last edited by _uj; July 23rd, 2007 at 06:28 AM.

  4. #4
    Join Date
    Jun 2006
    Posts
    437

    Re: Need for Virtual Function?

    Hi all.
    A classical example, the devolopment of a graphical library to draw on the screen.
    We can think to our library as a set of classes where each class implements a shape: for example, CSquare implements squares, CCircle implements circles, and so on. Each class has some private data members to define the features of the shape (for example, CCircle has center coordinates and radius), some public methods to initialized the graphical mode and a public method "draw" to display the shape on the screen. The initializing methods are common for all graphical classes, so we can make a class CShape that defines these methods, then derive all classes from CShape: this is obvious; now, we add the public method draw to CShape and declare it as virtual, so we can use dynamic binding.
    Well, now we can write the code that draws on screen using our graphical classes. What is a drawing? It's a set of shapes, and we get the drawing when we draw all shapes; so we can declare a drawing as an array of pointers to CShape objects. Remenber that a pointer to a class can hold the address of a derived class:

    Code:
    CShape *pCircle = new CCircle;
    is right. So the array of pointers to CShape can holds CCircle, CRectangle, and so on, and if we apply the draw method to all objects of the array we'll get our masterpiece:

    Code:
    CShape* aMyDrawing[3];  // The painting
    
    // Build an example of contemporary art
    aMyDrawing[0] = new CCircle(...);
    aMyDrawing[1] = new CTriangle(...);
    aMyDrawing[2] = new CSquare(...);
    
    // Now draw it
    for( int i = 0; i < 3; i++ )
    	aMyDrawing[i]->draw();
    Well, this simply and powerful approach works fine because we've used virtual method with dynamic binding.

    I hope that this simply example is useful to understand these OOP key concepts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured