Virtual function vs Pure virtual function in C++

Hello, Coders! In this section, we will study the concept of the virtual function and pure virtual function and the difference between them in C++.

Virtual Function

  • A virtual function is a  function that is implemented in the base class. It can be overridden or redefined by a derived class. It is a concept of run-time polymorphism.

Syntax:

virtual<function_type><function_name>()
{
    // code body
}

Example:

#include <iostream>  
using namespace std;  
class Base  
{  
    public:  
    virtual void display()  
    //Defining the Display as a Virtual function
    {  
        std::cout << "You are in Base Class" << std::endl;  
    }  
};

class Derived_One : public Base  
{  
    public:  
    void display()  
    {  
        std::cout << "You are in Derived class 1" << std::endl;  
    }  
};  

class Derived_Two : public Base  
{  
    public:  
    void display()  
    {  
        std::cout << "You are in Derived class 2" << std::endl;  
    }  
};  

int main()  
{  
    Base *obj;  
    Derived_One obj1;  
    Derived_Two obj2; 
    
    obj=&obj1;  
    obj->display();  
    obj=&obj2;  
    obj->display();  
    
    return 0;  
}

Output:

You are in Derived class 1
You are in Derived class 2

Here, in this program, we make the display method of the base class virtual by simply adding the virtual keyword. Here virtual indicates the method exits in appearance only, not in reality.

Pure Virtual Function

  • A pure virtual method is also known as abstraction function is a member function of the base class which have not any implementation or definition within the class.

Syntax:

virtual<function_type><function_name>() = 0;

//OR

virtual<function_type><function_name>() {}

Example:

#include<iostream>
using namespace std;
class Base {
    
   public:
      virtual void display() = 0; // Pure Virtual Function
};

class Derived_One:public Base {
    
   public:
      void display() {
         cout << "You are in the Derived Class \n";
      }
};

int main() {
   Base *obj;
   Derived_One obj1;
   obj = &obj1;
   obj -> display();
   return 0;
}

Output:

You are in the Derived Class

 

Difference Between Virtual and Pure Virtual in C++

  • A virtual function is a  defined function in the base class in the case of a virtual function, while in the pure virtual function it is not declared within the class.
  • The virtual function has no concept of an abstract class, while if the class contains a minimum of one pure virtual function, it is defined as an abstract class.
  • The instantiation of the Base class is possible if it contains a virtual function, but in the case of a pure virtual function, the instantiation of the base class is not possible.

Hope this article has helped you understand the concept of the virtual function and pure virtual function and the common difference between them.

You can also read, Methods vs Functions in C++ with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *