> ## 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.

# C++ Abstract Class

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

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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).

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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
    }
};
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
