Abstract classes
An abstract class is a class you cannot instantiate because it has at least one pure virtual function. It exists to be a base: it names a contract, and derived classes provide the missing operations.
Detailed Explanation
Shape with virtual double area() const = 0; is abstract. The compiler will not let you write Shape s; or new Shape. It will let you write Shape&, Shape*, and unique_ptr<Shape> that point at a Circle. The abstract class is the type of the handle. The derived class is the type of the object.
Abstract classes sit on a spectrum.
At the interface end, every public function is pure virtual and there is a virtual destructor. No data members. Multiple such bases can be mixed in. This is the C++ "interface".
In the middle, the class has some implemented methods and some pure ones. Document might implement save() in terms of a pure serialise(Writer&). Shared code lives once. Descendants only fill the gap. This is the template-method pattern done with virtuals.
At the other end, a class becomes abstract only because one function is still pure. That is a way to stop people from constructing a type that is not finished.
You cannot have an abstract value you store in a vector<Shape>. The vector would need to create Shape elements. Store vector<unique_ptr<Shape>> or vector<reference_wrapper<Shape>>. That is not a limitation of abstract classes so much as a reminder that polymorphism in C++ is pointer/reference polymorphism.
Construction still runs. When you construct a Circle, Shape's constructor runs first. Shape can have a constructor that takes a colour and stores it in a member. Abstract does not mean "no constructor". It means "no complete object of this type". Do not call pure virtuals from that constructor.
A protected constructor on an abstract class documents "only descendants create me". Combined with a public virtual destructor, that is the usual interface package. If you never delete through the interface, you can give it a protected non-virtual destructor instead, which keeps the type abstract-or-not independent of a vtable if you had no other virtuals — but you already have pure virtuals, so you already have a vtable. Give interfaces a public virtual destructor.
Testing an abstract class means testing a concrete descendant, or a test double that implements the pure virtuals. You cannot new up the abstract type in a fixture.
std::is_abstract<T> reports the property. Use it in a static_assert if a factory is only supposed to return abstract handles, or if a template should refuse abstract T for a value parameter.
Code Examples
#include <iostream>
#include <memory>
#include <string>
class Document {
public:
explicit Document(std::string title) : title_(std::move(title)) {}
virtual ~Document() = default;
virtual void serialise(std::ostream& out) const = 0;
void save(std::ostream& out) const { // shared implementation
out << title_ << '\n';
serialise(out);
}
protected:
std::string title_;
};
class Note : public Document {
public:
Note(std::string title, std::string body)
: Document(std::move(title)), body_(std::move(body)) {}
void serialise(std::ostream& out) const override { out << body_ << '\n'; }
private:
std::string body_;
};
int main() {
std::unique_ptr<Document> doc =
std::make_unique<Note>("todo", "write tests");
doc->save(std::cout);
// prints:
// todo
// write tests
}Common Gotchas
`vector<Shape>` does not compile
`Shape` is abstract, and a vector must be able to construct elements. Store pointers.
Abstract is not "no state"
an abstract class can have members and constructors. Calling a pure virtual from that constructor is still undefined behaviour.
A class can become concrete in two steps
`Circle` implements `area` but leaves `draw` pure; only `FilledCircle` is constructible. Read the error: it lists the remaining pure virtuals.
Slicing still applies if you ever make the class concrete later
removing `= 0` and storing values will slice descendants. Keep polymorphic types behind pointers.