When we aim to abstract a base class in a manner that prevents any public member function of the base class from being accessed by objects of its derived class, we use a virtual function. In a virtual function, an object of the base class is created, allowing polymorphic behavior. This means that the same code can work with objects of different derived classes through a common base class interface.
Consider a scenario, where we want to create a system to handle different shapes like circles and rectangles. Imagine we have a base class called Shape with a virtual function for calculating the area. Derived classes like Circle and Rectangle provide their own implementations for calculating the area based on their specific characteristics. The key here is that we can use a pointer or reference to the base class to interact with objects of different shapes in a uniform way.
A pure virtual function solidifies the concept of an abstract class, as it prohibits an abstract class from creating an object.
Extend the scenario where we want to enforce that every shape must have a method to display information. We can create a pure virtual function called display in the Shape class. This makes any class derived from Shape an abstract class, meaning we cannot create an object of the base class itself; it must be instantiated through its derived classes.
Conclusion: A class is considered an abstract class if it contains a Pure Virtual function. Abstract classes act as templates for creating specialized classes and cannot be used to directly create objects.