# Pure virtual function

Pure virtual functions are used to create abstract base classes, which cannot be instantiated and must be inherited from.

```cpp
#include <iostream>

// Abstract base class
class Shape {
public:
    // Pure virtual function
    virtual double area() const = 0;
    
    // Regular virtual function
    virtual void display() const {
        std::cout << "This is a shape." << std::endl;
    }
    
    virtual ~Shape() {
        std::cout << "Shape destructor called" << std::endl;
    }
};

class Circle : public Shape {
private:
    double radius;

public:
    Circle(double r) : radius(r) {}

    // Implementation of the pure virtual function
    double area() const override {
        return 3.14159 * radius * radius;
    }

    void display() const override {
        std::cout << "This is a circle with radius " << radius << std::endl;
    }

    ~Circle() override {
        std::cout << "Circle destructor called" << std::endl;
    }
};

class Rectangle : public Shape {
private:
    double width;
    double height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}

    // Implementation of the pure virtual function
    double area() const override {
        return width * height;
    }

    void display() const override {
        std::cout << "This is a rectangle with width " << width << " and height " << height << std::endl;
    }

    ~Rectangle() override {
        std::cout << "Rectangle destructor called" << std::endl;
    }
};

int main() {
    // Shape shape;  // This would cause a compile error

    Shape* circle = new Circle(5);
    Shape* rectangle = new Rectangle(4, 6);

    std::cout << "Circle area: " << circle->area() << std::endl;
    circle->display();

    std::cout << "Rectangle area: " << rectangle->area() << std::endl;
    rectangle->display();

    delete circle;
    delete rectangle;

    return 0;
}
```

> Run it [here](https://onecompiler.com/cpp/42kzzhdtg).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tejpratapsingh.com/_/cpp/code/classes/pure-virtual-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
