c++

What is the use of Virtual functions?
Give an example.
sathyarajkomalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sathyarajkomalAuthor Commented:
Edited text of question.
0
azamiCommented:
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
{
public:
    virtual void vDraw() {cout<<"Shape::vDraw"<<endl;}
    void Draw() {cout<<"Shape::Draw"<<endl;}
};

class Circle: public Shape
{
public:
    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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
azamiCommented:
typo in next to last paragraph in proposed answer - "Draw()" should be "vDraw()"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.