Posted on 1999-11-15
Medium Priority
Last Modified: 2010-04-16
What is the use of Virtual functions?
Give an example.
Question by:sathyarajkomal
  • 2

Author Comment

ID: 2207521
Edited text of question.

Accepted Solution

azami earned 20 total points
ID: 2209997
Virtual functions allow you to call a function of a specific type even when you have a reference/pointer to a more general type.  You can call the "correct" function implementation, according to the _actual_ type of an object, without knowing the actual type.

A classic example is Shapes.  Assume a base class, Shape, with a function, Draw(), which does nothing.  Derived from this are all the specific shapes - Square, Circle, etc.  Each shape overrides the base class Draw() function to draw the appropriate shape.  Now, assume you have a list of Shape objects (they are actually Squares, Circles, etc - but because of "abstraction", you can treat them as Shape objects).  To draw all the shapes in the list, you iterate over the list, and call Draw() on each object.  If Draw() is not virtual, this will cause Shape::Draw() to be called for each object - and that function does nothing.  If Draw() is virtual, this will cause the correct particular Draw() function - Square::Draw(), Circle::Draw(), etc. - to be called.

class Shape
    virtual void vDraw() {cout<<"Shape::vDraw"<<endl;}
    void Draw() {cout<<"Shape::Draw"<<endl;}

class Circle: public Shape
    virtual void vDraw() {cout<<"Circle::vDraw"<<endl;}
    void Draw() {cout<<"Circle::Draw"<<endl;}

void main()
    Circle *cp = new Circle();
    Shape *sp = cp;

    cp->Draw(); //Circle::Draw
    cp->vDraw(); //Circle::vDraw
    sp->Draw(); //Shape::Draw
    sp->vDraw(); //Circle::vDraw  !!!

I haven't tested this code (not for 10 points! :-) ).  The key point is in that last line though: we have a pointer to a Shape, but since it is _actually_ a Circle, the code calls the Circle implementation of Draw(), rather than the Shape implementation of it.

There's tons more to understanding virtual functions completely: pure virtual functions and abstract classes, late vs. early binding, and understanding the vtable come to mind.  But I think the above fairly thoroughly answers your question.

Expert Comment

ID: 2210002
typo in next to last paragraph in proposed answer - "Draw()" should be "vDraw()"

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, unlike web development, the mobile landscape is complex enough for a software engineer and Android is posing more challenging environment thanks to its fragmentation issues on hardware and software fronts.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question