C++ abstract classes and virtual dispatch
A pure virtual method defines a required operation and makes the base class abstract. A derived class supplies the implementation with override.
C++
#include <iostream>
class Shape {
public:
virtual int area() const = 0;
virtual ~Shape() = default;
};
class Rectangle : public Shape {
int width_, height_;
public:
Rectangle(int w, int h) : width_(w), height_(h) {}
int area() const override { return width_ * height_; }
};
int main() {
Rectangle rectangle(3, 4);
const Shape& shape = rectangle;
std::cout << shape.area() << '\n';
}