Virtual Function Vs Pure Virtual Function In C++ - C++ Programming Tutorial
C++ Course / Polymorphism / Virtual Function Vs Pure Virtual Function In C++

Virtual Function Vs Pure Virtual Function In C++

BLUF: Mastering Virtual Function Vs Pure Virtual Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Virtual Function Vs Pure Virtual Function In C++

C++ is renowned for its efficiency. Learn how Virtual Function Vs Pure Virtual Function In C++ enables low-level control and high-performance computing in the tutorial below.

Before delving into the disparities between the virtual function and pure virtual function in C++, it is essential to grasp the concepts of virtual function and pure virtual function in the C++ programming language.

What is virtual function?

A virtual function is a method defined in the base class that can be overridden in a subclass.

Let's understand through an example.

Example

#include <iostream>

using namespace std;

class base

{

    public:

    void show()

    {

        std::cout << "Base class" << std::endl;

    }

};

class derived1 : public base

{

    public:

    void show()

    {

        std::cout << "Derived class 1" << std::endl;

    }

};

class derived2 : public base

{

    public:

    void show()

    {

        std::cout << "Derived class 2" << std::endl;

    }

};

int main()

{

    base *b;

    derived1 d1;

    derived2 d2;

    b=&d1;

    b->show();

    b=&d2;

    b->show();

    return 0;

}

In the provided code snippet, the virtual method has not been utilized. A foundational class has been established with the show function. Additionally, two classes named 'derived1' and 'derived2' have been implemented to inherit the characteristics of the base class. Both 'derived1' and 'derived2' have overridden the show function. Within the main function, a pointer variable 'b' of the base class is defined. Instances of derived1 and derived2 are represented by d1 and d2 respectively. Despite 'b' storing the references of d1 and d2, the show method consistently invokes the show function of the base class instead of accessing the functions of derived1 and derived2.

To resolve the previously mentioned issue, it is necessary to declare the method as virtual in the base class. In this context, virtual indicates that the method is present conceptually but not concretely. This adjustment can be achieved by introducing the keyword "virtual" before the function. In the earlier code snippet, it is essential to include the virtual keyword preceding the show function in the base class as illustrated below:

Example

virtual void show()

    {

        std::cout << "Base class" << std::endl;

    }

Once the adjustments mentioned are implemented, the result will be:

Importancpp tutorials:

  • It is a run-time polymorphism.
  • Both the base class and the derived class have the same function name, and the base class is assigned with an address of the derived class object then also pointer will execute the base class function.
  • If the function is made virtual, then the compiler will determine which function is to execute at the run time on the basis of the assigned address to the pointer of the base class.
  • What is pure virtual function?

A pure abstract function is a virtual function lacking an implementation within the class. Let's delve into the idea of pure virtual functions with an illustrative example.

In the visual illustration above, the foundation class is referred to as shape, whereas rectangle, square, and circle belong to the subclasses. Without specifying a definition for the virtual function, it will inherently become a pure virtual function.

Characteristics of a pure virtual function

  • A pure virtual function is a "do nothing" function. Here "do nothing" means that it just provides the template, and derived class implements the function.
  • It can be considered as an empty function means that the pure virtual function does not have any definition relative to the base class.
  • Programmers need to redefine the pure virtual function in the derived class as it has no definition in the base class.
  • A class having pure virtual function cannot be used to create direct objects of its own. It means that the class is containing any pure virtual function then we cannot create the object of that class. This type of class is known as an abstract class.
  • Syntax

There are two methods for defining a virtual function:

Example

virtual void display() = 0;
Example

virtual void display() {}

Let's understand through an example.

Example

#include <iostream>

using namespace std;

// Abstract class

class Shape

{

    public:

    virtual float calculateArea() = 0; // pure virtual function.

};

class Square : public Shape

{

    float a;

    public:

    Square(float l)

    {

        a = l;

    }

    float calculateArea()

    {

        return a*a;

    }

};

class Circle : public Shape

{

    float r;

    public:

    

    Circle(float x)

    {

        r = x;

    }

    float calculateArea()

    {

        return 3.14*r*r ;

    }

};

class Rectangle : public Shape

{

    float l;

    float b;

    public:

    Rectangle(float x, float y)

    {

       l=x;

       b=y;

    }

    float calculateArea()

    {

        return l*b;

    }

};

int main()

{

    

    Shape *shape;

    Square s(3.4);

    Rectangle r(5,6);

    Circle c(7.8);

    shape =&s;

    int a1 =shape->calculateArea();

    shape = &r;

    int a2 = shape->calculateArea();

    shape = &c;

    int a3 = shape->calculateArea();

    std::cout << "Area of the square is " <<a1<< std::endl;

    std::cout << "Area of the rectangle is " <<a2<< std::endl;

    std::cout << "Area of the circle is " <<a3<< std::endl;

    return 0;

}

Differences between the virtual function and pure virtual function

Virtual function Pure virtual function
A virtual function is a member function in a base class that can be redefined in a derived class. A pure virtual function is a member function in a base class whose declaration is provided in a base class and implemented in a derived class.
The classes which are containing virtual functions are not abstract classes. The classes which are containing pure virtual function are the abstract classes.
In case of a virtual function, definition of a function is provided in the base class. In case of a pure virtual function, definition of a function is not provided in the base class.
The base class that contains a virtual function can be instantiated. The base class that contains a pure virtual function becomes an abstract class, and that cannot be instantiated.
If the derived class will not redefine the virtual function of the base class, then there will be no effect on the compilation. If the derived class does not define the pure virtual function; it will not throw any error but the derived class becomes an abstract class.
All the derived classes may or may not redefine the virtual function. All the derived classes must define the pure virtual function.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience