Virtual Function in C++ With Example Program

What is Virtual Function?

Virtual means existing in appearance but not in reality. When virtual functions are used, a program that appears to be calling a function of one may in reality be calling a function of a different class.

Why are virtual functions needed? Suppose you have a number of objects of different classes but you want to put them all in an array and perform a particular operation on them using the same function call. For example, suppose a graphics program includes several different shapes: a triangle, a ball, a square, and so on, as in the MULTSHAP program, “Inheritance”. Each of these classes has a member function draw() that causes the object to be drawn on the screen.

#include<iostream>
using namespace std;
class base
{
public:
void show()
{ cout << "Base\n"; }
};
class Derv1 : public Base {
public:
void show()
{ cout <<"Derv1\n";}
};
class Derv2: public Base
//derived class 2
{
public:
void show()
{ cout<< "Derv2\n";}
};
int main()
{
Derv1 dv1;
Derv2 dv2;
Base* ptr;
ptr = &dv1;
ptr->show();
ptr = &dv2;
ptr->show();
}
  • Important points to remember : Only the Base class Method’s need the virtual keyword, 1. not the definition.
  • 2. If a function is declared as virtual in the base class, it will be virtual in all its derived classes.
  •  The address of the virtual functions is placed in the VTABLE and the compiler uses VPTR (vpointer) to point to the virtual function.
#include<iostream>
using namespace std;
class base
{
public:
void show()
{ cout << "Base\n"; }
};
class Derv1 : public Base {
public:
void show()
{ cout <<"Derv1\n";}
};
class Derv2: public Base
//derived class 2
{
public:
void show()
{ cout<< "Derv2\n";}
};
int main()
{
Derv1 dv1;
Derv2 dv2;
Base* ptr;
ptr = &dv1;
ptr->show();
ptr = &dv2;
ptr->show();
}

function

Late Binding

  • Compiler ptr->show();
  • It always compilers a call to the show() function in the base class
  • But VIRT the compiler doesn’t know what class the contents of ptr may contain.
  • It could be the address of an object of the Derv1 class or of the Derv2 class. Which version of draw() does the compiler call
  • In fact the compiler doesn’t know what to do, so it arranges for the decision to be deferred until the program is running.
  • At runtime, when it is known what class is pointed to by ptr,
  • The appropriate version of draw will be called. This is called late binding or dynamic binding

Abstract Classes and Pure Virtual Functions

When we will never want to instantiate objects of a base class, we call it an abstract class.

Such a class exists only to act as a parent of derived classes that will be used to instantiate objects. It may also provide an interface for the class hierarchy.

Pure virtual (abstract) functions

So far, all of the virtual functions we have written have a body (a definition). However, C++ allows you to create a special kind of virtual called a pure virtual function (or abstract function) that has no body at all! A pure virtual function simply acts as a placeholder that is meant to be redefined by derived classes.

Example Program

class Base
{
public:
  const char* SayHi() {reurn "Hi";} // a normal non- virtual function

  virtual const char* GetName() {return "Base";} // a normal virtual function
   
  virtual int GetValue() = 0; // a pure virtual function };
class Base
{
public:
virtual void show() = 0; //pure
virtual function
};
/
class Derv1 : public Base
{
public:
void show()
{ cout<<"Derv1\n";}
};

class Derv2 : public Base
{
public:
void show()
{ cout<< "Derv2\n";}
};
int main()
{
//Base bad; //cant't make object from abstract class
Base* arr[2];//array of pointer to base class
Derv1 dv1;
Derv2 dv2;
arr[0] = &dv1;
arr[1] = &dv2;
arr[0]->show();
arr[1]->show();
return 0;
}

Virtual Base Classes

class parent
{
protected:
int basedata;
};
class Child1 : public parent
{};
class child2 : public parent
{};
class Grandchild : public child1, public child2
{
public:
int getdata()
{return basedata; }// ERROR: ambiguous
};
// virtbase.cpp
// virtual base classes

class parent
{
protected:
int basedata;
};
class child1 : virtual public parent // shares copy of parent
{};
class child2 : virtual public parent // shares copy of parent
{};
class Grandchild : public1, public child2
{
public:
int getdata()
{return basedata;} // OK: only one copy of parent
};

Virtual Function

  • Virtual means existing in effect but not in reality.
  • It provide a way for a program to decide, when it is running, what function to call.
  • It allows greater flexibility in performing the same kinds of action on different kinds of objects.
  • It actually holds pointers to a variety of derived types
  • It is defined and declared in the base class and is then used by several derived functions by getting up pointers.
Read More Topics
Program Testing and Debugging in C
Common Programming Errors in C
Realloc in C
Calloc in C
Malloc in C

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply