Pure virtual function in c++ with example program

vijaya shankar.k | 3574 Views | c++ | 08 Aug 2016

 

What is a virtual function in c++ :

  •  A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
  • A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.

Pure Virtual Functions in c++ :

  • Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with = 0. Here is the syntax for a pure virtual function,
virtual void f() = 0;

Pure Virtual Functions program in c++ :

class Base   //Abstract base class
 {
   public:
   virtual void show() = 0;    //Pure Virtual Function
 };
class Derived:public Base
{
  public:
  void show()
  { 
    cout << "wikitechy"; 
  }
};
int main()
 {
   Base obj;   //Compile Time Error
   Base *b;
   Derived d;
   b = &d;
   b->show();
 }

Output :

wikitechy

Pure Virtual definitions in c++ :

  • Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class.
  • The Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.
  • A virtual function is a member function that is declared within a base class and redefined by a derived class.

C++ Abstract class and Pure virtual Function :

  • In C++ programming, sometimes inheritance is used only for the better visualization of data and you do not need to create any object of base class.

  • For example: If you want to calculate area of different objects like: circle and square then, you can inherit these classes from a shape because it helps to visualize the problem but, you do not need to create any object of shape. In such case, you can declare shape as a abstract class. If you try to create object of a abstract class, compiler shows error.
     // An abstract class
class Test
{   
    // Data members of class
public:
    // Pure Virtual Function
    virtual void show() = 0;
   
   /* Other members */
};

Why can't we create Object of Abstract Class in c++ :

  • When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE(studied in last topic), but doesn't put any address in that slot. Hence the VTABLE will be incomplete. as the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an errror message whenever you try to do so.

Why would you want a pure virtual function to have an implementation :

  • It is actually pretty rare to see a pure virtual function with an implementation in real-world code, but having that implementation may be desirable when you think that classes which derive from the base class may need some sort of default behavior for the pure virtual function. So, for example, if we have a class that derives from our SomeClass class above, we can write some code like this – where the derived class actually makes a call to the pure virtual function implementation that is inherited:

Syntax :

    //this class derives from SomeClass
class DerivedClass: public SomeClass 
{
 virtual void pure_virtual() 
 {
 /*
 Makes a call to the pure virtual function 
 implementation that is inside the SomeClass
 class. This can happen because DerivedClass
 may not have anything appropriate to define
 for this function, so it just calls the SomeClass's
 implementation
 */
SomeClass::pure_virtual();  
}
};

When should pure virtual functions be used in C++ :

  • In C++, a regular, "non-pure" virtual function provides a definition, which means that the class in which that virtual function is defined does not need to be declared abstract. You would want to create a pure virtual function when it doesn’t make sense to provide a definition for a virtual function in the base class itself, within the context of inheritance.

Virtual functions program in c++ :

#include<iostream.h>
#include<conio.h>
class base  // Base Class
{
 public: void show()      //NON Virtual function
  {
    cout<<"pure virtual function\\n";
   }
virtual void  display()    //Virtual function
    {
       cout<<"wikitechy\\n";
    }
 };
class derived: public base     //Deriving new class from base class
  {
public:void show()
  {
     cout<<"derived - Show \\n";
   }
void display()
  {
     cout<<"derived - display \\n";
   }
};

int main()
{
     base *ptr;
     base b;
     derived d;
     cout<<"virtual function\\n";
     ptr=&b;
     ptr->show();
     ptr->display();
     cout<<"wikitechy\\n";
     ptr=&d;
     ptr->show();
     ptr->display();
     getch();
     return 0;
}

Output :

virtual function
pure virtual function
virtual function
wikitechy
pure virtual function
derived – display

What is a virtual Destructors :

  • Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destrucstion of the object when the program exits.

NOTE : Constructors are never Virtual, only Destructors can be Virtual.