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:

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

A virtual function should not be a static member

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

A constructor function cannot be a virtual function.

A destructor function may be a virtual function.

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

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

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 -> read();
p -> display();

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

let p= &b1;

p-> read();
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 read()
{
. . .
}
Virtual void display()
{
. . .
}

Let p=&b1;

p -> read();
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.

About Us!

'Z Series Innovations' is an e-learning solution for learning all technical stuff online. It is an online portal that gives an enhanced way of learning and guidance in various fields of engineering which include robotics, electronics, communication,computer science, embedded systems and its real life applications.

Join Us

Do you feel like contributing to this website technically? Jump in!

Your technical expertise would be needed to create quality articles and content on this site and hence contribute to this growing global community of technical enthusiast.