-->

Virtual Function

2 minute read
Virtual Function

1.             What is virtual function? When it is needed? Give the example of it. 
v When we use the same function name in both the base class and derived, the function in base class is declared as virtual using the keyword virtual preceding its normal declaration.
v When a function is made virtual, C++ determines which function to use/call at run time based on the pointer, that to which object it points, base class function or derived class pointer, rather than the type of the pointer.
v Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.
v Example:
class A
{
    public:
    void virtual display()
    {
             cout<<"subhash loves kids"<<endl;
    }
};
class B:public A
{
    public:
    void display()
    {
             cout<<"subhash loves Apples"<<endl;
    }
};
int main()
{
    clrscr();
    B b;
    A *ap;
    ap=&b;
    ap->display();
    getch();
    return 0;
}

2.             Explain the rules of virtual function.
·    The virtual functions must be non-static members.
·    The virtual functions are accessed by using pointers to objects.
·    A virtual function can be friend of another class.
·    Constructor cannot be made virtual, but destructor can be made.
·    Virtual function defined in the class need not be necessarily redefined in the derived class.
·    A base class pointer can refer to the derived class objects, but the reverse is not true.
·    Virtual functions should be declared in the public section of a class.

3.             Is it possible to declare the constructor as a virtual function? Justify your answer with example?
v Constructor cannot be declared as virtual function.
v Virtual function is used to resolve the ambiguity of calling the function have the same name as function in the base class.
v Constructors are called implicitly.
v It is not possible that the constructor of the derived class and the base class have the same name, so there is no need for the virtual constructor.
v When we create an object of the derived class, it invokes the base class constructor first then the constructor of the derived class is executed.
v Example:
class A
{
public:
A ( )
{
cout << “base class”;
}
};
class B : public A
{
public:
B ( )
{
cout << “derived class”;
}
};
int main()
{
                 B b;
                 return 0;
}
v Output:
base class
derived class

4.             
5.                What is vtble? 
When working with virtual functions in C++, it’s the vtable that’s being used behind the scenes to help achieve polymorphism.
Whenever a class itself contains virtual functions or overrides virtual functions from a parent class the compiler builds a vtable for that class. This means that not all classes have a vtable created for them by the compiler. The vtable contains function pointers that point to the virtual functions in that class. There can only be one vtable per class, and all objects of the same class will share the same vtable

7.             What are the Limitations of virtual functions.
·    Global functions cannot be declared virtual.
·    Defining a virtual function in a base class is a must, even though it may not be used.