Abstract Base Class (ABC)

Abstract classes don’t have any instances. They exist only to define common shapes of descendant classes.

  • their purpose is to provide a framework to organize and define subclasses

An abstract class in C++ is a class that contains at least one pure virtual function: virtual void nameOfFunction() = 0;

class AbstractBase {
public:
    virtual void pureVirtualFunction() = 0;  // Pure virtual function
    void nonVirtualFunction() {
        // Some implementation here
    }
};

Since the class cannot be instantiated directly because it has an undefined function. However, derived classes can inherit from the Abstract Class and provide concrete implementations for the pure virtual function:

class Derived : public AbstractBase {
public:
    void pureVirtualFunction() override {
        // Provide implementation for the pure virtual function
    }
};

Now, Derived provides an implementation for the pureVirtualFunction and can be instantiated.

Concrete classes, on the other hand, are designed to inherit the properties of the common ABCs, and extend them by adding new instance variables and methods peculiar to them.

Abstract classes serve as way to define an interface or a common set of functions that derived classes must implement while allowing a common base for polymorphism.