If a function present in Base class is overridden in derived class then Inheritance has ambiguity .So virtual keyword is used to overcome this and at run time which function to call can be decided by using Base class pointer to point to either Base class object or Derived class object like below:

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display()
{
cout<<"Inside Base\n";
}
};

class Derived:public Base
{
public:
void display()
{
cout<<"Inside derived\n";
}
};

int main()
{
Derived d;
Base b;
Base *bp;
bp=&b;
bp->display();
bp=&d;
bp->display();

}

Same can be achieved by using scope resolution operator which function to call is solved like the example givenn beow:

#include <iostream>
using namespace std;

class Base
{
public:
void display()
{
cout<<"Inside Base\n";
}
};

class Derived:public Base
{
public:
void display()
{
cout<<"Inside derived\n";
}
};

int main()
{
Derived d;
d.Base::display();//invokes display() in Base
d.Derived::display();//invokes display in Derived
return 0;
}

So my question is why virtual functions are needed if same task can be achieved by scope resolution operator?

Imagine you have a vector full of pointers to a Polygon type. Now there are many types of polygons and they all derive themselves from Polygon. Some of those derived classes are then turned into base classes for other classes. With virtualization you don’t have to worry about the number of levels there is. You can call "display()" and you can be sure you will get the correct "display()" called.

Number of levels generally are not more in projects ,so this should not be a good reason for virtual concept.Please explain me further!!!!

virtual functions allow derived class to override functions in base class(s). When the base class calls a virtual function the top-most derived class function is called. If you have a base class called Animal and a derived class called Dog, when Animal calls virtual function Speak() the function in Dog will get called. You can have another derived class called Cat, in that case Speak() from Cat will get called.

There also is something called "pure virtual" function. Its the same as simple virtual function except the base class does not implement it, one of the derived classes is required to implement pure virtual functions.

Edited 4 Years Ago by Ancient Dragon

ancient dragon ..i know how to use virtual function,but my question is not how touse it.Please look at my orignal post.

So my question is why virtual functions are needed if same task can be achieved by scope resolution operator?

I'll answer your question with another question: If you're given nothing but a pointer to the base, how do you know which type to use for the scope resolution operator?

hi deceptikon ,

If you have only base pointer then also you cannot achieve objective of calling different function(for base and derived classes) without having derived class object. So either choose scoper resolution or virtual ,,you have to use derived class object.

The example you showed contains a vector of base pointer that contains address of all derived classes and later base pointer is used to call the same fucntion that can belong to either of derived classes or base class ,so while calling no need to know function belong to which class.

But my friend while assigning addresses to vector of base pointers, we need to create derived class objects and assign address.So need to know the type of object at that time .

Is it("while calling no need to know function belong to which class.") the only advantage of virtual concept?

This article has been dead for over six months. Start a new discussion instead.