Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!

# Virtual Functions

Virtual function is a member function that is declared within a base class and redefined by a derived class. When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual.

The application of virtual function is through base class pointer. A base class pointer can be used to point to objects of base class as well as to objects of any class derived from that base class .But a derived class pointer can point to objects of derived class only.It cannot point to objects of base class because a base class object is not an object of derived class.When a base class pointer points to a derived class object that contains a virtual function, C++ determines which version of that function to call based upon the type of object pointed to by the pointer.And this determination is made at run time.Thus, by making the base class pointer to point to different objects we can execute virtual functions.

Rules for declaring Virtual Functions:

1. A virtual function should be defined in the base class or in which it appears first.

2. A virtual function should not be a static member

3. A virtual function can be a friend function for another class.

4. A constructor function cannot be a virtual function.

5. A destructor function may be a virtual function.

6. A pointer to a derived class cannot be used to access the object of the base class.

7. Prototypes of a virtual function in the base class and derived class must be exactly same.

8. The virtual function defined in base class is overridden by that defined in the derived class.

Now, let us the consider following example in order to understand virtual functions.:

Let A be class containing two member functions read() and displays().
Let B be a class derived from A. Assume that B be a class derived from A. Assume that B contains two functions read() and display().
Let p be a pointer of type of type A.since A is the base class p may be known as base pointer. To a base pointer we can assign address of an object of type base class and address of an object of derived class. Let a1 be an object of type A.Let b1 be an object of type B.

Let p=&a1;

p -> display();

Now read() and display() of A will be executed.

let p= &b1;

p ->display();

Now, also read() and display() of class A will be executed since the functions will be selected considering the type of p, not by checking the content of p. This can be changed by declaring read() and display() functions of class as virtual functions as follows:

{
. . .
}
Virtual void display()
{
. . .
}

Let p=&b1;

p -> display();

Since read() and display() are declared as virtual functions in base class A, now the functions will be selected by checking the contents of p not by checking the type of p i.e. read() and display() of class will be executed.

CACKLE comment system  