Monday, March 26, 2012

Virtual functions in C++!!!

A Virtual function  is a special type of function, which resolve to most-derived function with the same prototype. To make a function virtual, just use virtual keyword before the function. This is also called function overriding or runtime polymorphism.

Lets see the use of virtual functions. Assume that there is a base class with one member function. And one derived class with another member function with same name as in base class. If you try to call that member function as shown in the below sample code.

#include <iostream>
using namespace std;
struct base
{

    public:
        void display()
        {
            cout<<"this is base class\n";
        }

};

class derived: public base
{
    public:
        void display()   // same function there in base class also
        {
            cout<<"this is derived class\n";
        }
};

int main()
{
    derived d;
    base &b = d; // refering base object to derived object
    b.display();
}


Output:
this is base class


From the above sample code, the expected result should be from derived function as base object is referring to the derived object. But we got the base function. Even though , base object is referring to derived object, it wont resolves to derived class. To avoid this we need to use the virtual  keyword. See the same sample code with virtual keyword.

Example With Virtual keyword:

#include <iostream>
using namespace std;
struct base
{

    public:
        virtual void display()   // using virtual keyword
        {
            cout<<"this is base class\n";
        }

};

class derived: public base
{
    public:
        void display()  //same function is there in base class
        {
            cout<<"this is derived class\n";
        }
};

int main()
{
    derived d;
    base &b = d; // refering base object to derived object
    b.display();

}

Output:
this is derived class


Now we got the expected output, this is because of the virtual  keyword. If you use virtual keyword, it will check for resolving function for the derived classes, starting from the base class to referred derived class.

Another Example with more derived classes:
#include <iostream>
using namespace std;
struct base
{

    public:
        virtual void display()
        {
            cout<<"this is base class\n";
        }

};

class derived: public base
{
    public:
        void display()
        {
            cout<<"this is derived class\n";
        }
};

class derived1: public derived
{
    public:
        void display()
        {
            cout<<"this is derived1 class\n";
        }
};
class derived2: public derived1
{
    public:
        void display()
        {
            cout<<"this is derived2 class\n";
        }
};
int main()
{
    base b1;
    derived1 d;
    base &b = d; // base object refering to direved1 object
    b.display();

}

Output:
this is derived1 class


In the above sample code, base object is refering to derived1 object, so resolving process starts from the base class, it checks for the virtual keyword, if it finds the keyword, it goes to the derived class, this process goes on til it reaches the refered object, here it is derived1.

Use of Virtual keyword: To implement virtula functions, we need to use the virtual keyword in base class. And its optional to use in derived class. But its better to use in derived class also, so that you are informing that that function is a virtual function. Generally most base class function need to be used virtual.

No comments:

Popular Posts