Virtual Function in C++

Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but different forms. An essential requirement of polymorphism is therefore the ability to refer to objects without any regard to their classes.

Virtual functions, one of advanced features of OOP is one that does not really exist but it appears real in some parts of a program. This section deals with the polymorphic features which are incorporated using the virtual functions.

Rules For Virtual Functions:

When virtual functions are created for implementing late binding, observe some basic rules that satisfy the compiler requirements.

  1. The virtual functions must be members of some class.
  2. They cannot be static members.
  3. They are accessed by using object pointers.
  4. A virtual function can be a friend of another class.
  5. A virtual function in a base class must be defined, even though it may not be used.
  6. The prototypes of the base class version of a virtual function and all the derived class versions must be identical. C++ considers them as overloaded functions, and the virtual function mechanism is ignored.
  7. We cannot have virtual constructors, but we can have virtual destructors.
  8. While a base pointer points to any type of the derived object, the reverse is not true. i.e. we cannot use a pointer to a derived class to access an object of the base class type.
  9. When a base pointer points to a derived class, incrementing or decrementing it will not make it to point to the next object of the derived class. It is incremented or decremented only relative to its base type. Therefore we should not use this method to move the pointer to the next object.
  10. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.

Syntax:

class point 
{
intx;
inty;
public:
virtual int length ( );
virtual void display ( );
};

Remember that the keyword virtual should not be repeated in the definition if the definition occurs outside the class declaration. The use of a function specifier virtual in the function definition is invalid.

Example:

class point 
{
intx ;
inty ;
public:
virtual void display ( );
};
virtual void point: : display ( ) //error
{
    Function Body
}

A virtual function cannot be a static member since a virtual member is always a member of a particular object in a class rather than a member of the class as a whole.

class point 
{
int x ;
int y ;
public:
virtual static int length ( ); //error
};
int point: : length ( )
{
   Function body
}

A virtual function cannot have a constructor member function but it can have the destructor member function.

class point 
{
int x ;
int y ;
public:
virtual point (int xx, int yy) ; // constructors, error
void display ( ) ;
int length ( ) ;
};

It is an error to redefine a virtual method with a change of return data type in the derived class with the same parameter types as those of a virtual method in the base class.

class base 
{
    int x,y;
public:
virtual int sum (int xx, int yy ) ;   //error
} ;
class derived: public base 
{
    int z;
public:
virtual float sum (int xx, int yy) ;
};

The Below declarations of two virtual functions are invalid. Even though these functions take identical arguments note that the return data types are different.

virtual int sum (int xx, int IT) ; //base class
virtual float sum (int xx, int IT) ; //derived class

Both the below functions can be written with int data types in the base class as well as in the derived class as:

virtual int sum (int xx, int yy) ; //base class
virtual int sum (int xx, int yy) ; //derived class

Only a member function of a class can be declared as virtual. A non member function (non method) of a class cannot be declared virtual.

virtual void display ( ) //error, nonmember function 
{
    Function body
}

Example:

#include<iostream.h>
class Base
{
	public:
		void display()
		{
			cout<<”Display Base”;
		}
		virtual void show()
		{
			cout<<”Show Base”;
		}
};
class Derived : public Base
{
	public:
		void display()
		{
			cout<<”Display Derived”;
		}
		void show()
		{
			cout<<”show derived”;
		}
};
void main()
{
		Base b;
		Derived d;
		Base *ptr;
			cout<<”ptr points to Base”;
		ptr=&b;
		ptr->display(); 	//calls Base
		ptr->show(); 		//calls Base
			cout<<”ptr points to derived”;
		ptr=&d;
		ptr->display(); 	//calls Base
		ptr->show(); 		//class Derived
}

Output:

ptr points to Base
Display Base
Show Base
ptr points to Derived
Display Base
Show Derived

When ptr is made to point to the object d, the statement ptr->display(); calls only the function associated with the Base class i.e.. Base::display().

where as the statement

ptr->show();

calls the derived version of show(). This is because the function display() has not been made virtual in the Base class.

Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function.

Related posts