Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract class in C++ is a class that cannot be instantiated directly and serves exclusively as a base class for other classes. A class automatically becomes abstract when it declares at least one pure virtual function, or when it inherits a pure virtual function and fails to provide a final overrider for it. A pure virtual function is a virtual function declared with the = 0 syntax (a pure specifier) at the end of its declaration. This instructs the compiler that the function requires an implementation in a derived class for that derived class to become a concrete (instantiable) type.

Syntax

class AbstractBase {
public:
    // Pure virtual function declaration
    virtual void doWork() = 0; 

    // Abstract classes can still contain concrete members and methods
    int dataMember;
    void concreteMethod() {
        // Implementation
    }

    // A virtual destructor is required for safe polymorphic deletion
    virtual ~AbstractBase() = default; 
};

class ConcreteDerived : public AbstractBase {
public:
    // Overriding the pure virtual function makes this class concrete
    void doWork() override {
        // Implementation
    }
};

Technical Rules and Constraints

1. Instantiation Restrictions The compiler strictly forbids the creation of objects of an abstract class type. Attempting to allocate an abstract class on the stack or the heap results in a compilation error. Furthermore, C++ strictly forbids using an abstract class as a pass-by-value parameter type or a return type in function declarations.
AbstractBase obj;                       // Error: Cannot declare variable of abstract type
AbstractBase* ptr = new AbstractBase(); // Error: Cannot allocate object of abstract type
void process(AbstractBase obj);         // Error: Cannot pass abstract class by value
AbstractBase create();                  // Error: Cannot return abstract class by value
2. Pointers and References While you cannot instantiate an abstract class, you can declare pointers and references to it. This is the foundational mechanism for runtime polymorphism in C++, allowing a base pointer to interface with derived concrete objects via the virtual table (vtable).
ConcreteDerived derivedObj;
AbstractBase& ref = derivedObj;               // Valid
AbstractBase* ptr = new ConcreteDerived();    // Valid
3. Abstract Class Propagation If a derived class inherits from an abstract base class and fails to override every pure virtual function, the derived class inherits the pure virtual status. Consequently, the derived class also becomes an abstract class and cannot be instantiated. 4. Constructors, Destructors, and Undefined Behavior Abstract classes can possess constructors. Though the class itself cannot be instantiated, its constructor is invoked during the initialization phase of a derived class object. However, making a virtual call to a pure virtual function (directly or indirectly) from within an abstract class’s constructor or destructor results in undefined behavior (typically manifesting as a “pure virtual method called” runtime crash). During the execution of a base class constructor or destructor, the object’s dynamic type is considered to be the base class, meaning derived class overrides are not yet initialized or have already been destroyed. Conversely, a statically bound (qualified) call to a pure virtual function (e.g., AbstractBase::process()) from a constructor or destructor is perfectly valid and well-defined, provided the pure virtual function has an out-of-line definition. 5. Pure Virtual Destructors A class can be made abstract even if it lacks other logical pure virtual methods by declaring a pure virtual destructor (virtual ~Base() = 0;). Crucially, a pure virtual destructor must be given an out-of-line definition. Because derived class destructors will implicitly call the base class destructor during the destruction sequence, failing to define the pure virtual destructor will result in a linker error.
class AbstractWithPureDtor {
public:
    virtual ~AbstractWithPureDtor() = 0; // Makes the class abstract
};

// Mandatory out-of-line definition to prevent linker errors
inline AbstractWithPureDtor::~AbstractWithPureDtor() {}
6. Pure Virtual Function Definitions C++ permits a standard pure virtual function to have a defined body, but it must be defined outside the class declaration. The class remains abstract, and derived classes are still forced to override the function. However, the derived class can explicitly invoke the base class’s pure virtual implementation.
class AbstractBase {
public:
    virtual void process() = 0; // Pure specifier
};

// Implementation must be out-of-line
inline void AbstractBase::process() {
    // Base implementation logic
}

class Derived : public AbstractBase {
public:
    void process() override {
        AbstractBase::process(); // Explicitly calling the base pure virtual function
    }
};
Master C++ with Deep Grasping Methodology!Learn More