abstract class

< cpp‎ | language
Revision as of 06:30, 24 September 2013 by Cubbi (Talk | contribs)

C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
decltype specifier (C++11)
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
alternative representations
typedef declaration
type alias declaration (C++11)
attributes (C++11)
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Class-specific function properties
Special member functions
class template
function template
template specialization
parameter packs (C++11)
Inline assembly

Defines an abstract type which cannot be instantiated, but can be used as a base class.


pure virtual function is a virtual function declared using the following syntax:

virtual function_declaration = 0 ;

An abstract class is a class that either defines or inherits at least one function for which the final overrider is pure virtual.


Abstract classes are used to represent general concepts (for example, Shape), which can be used as base classes for concrete classes (for example, Circle).

No objects of an abstract class can be created. Abstract types cannot be used as parameter types, as function return types, or as the type of an explicit conversion. Pointers and references to an abstract class can be declared.

struct Abstract {
    virtual void f() = 0; // pure virtual
}; // "Abstract" is abstract
struct Concrete : Abstract {
    void f() override {}; // non-pure virtual
    virtual void g();     // non-pure virtual
}; // "Concrete" is non-abstract
struct Abstract2 : Concrete {
    void g() override = 0; // pure virtual overrider
}; // "Abstract2" is abstract
int main()
    // Abstract a; // Error: abstract class
    Concrete b; // OK
    Abstract& a = b; // OK to reference abstract base
    a.f(); // virtual dispatch to Concrete::f()
    // Abstract2 a2; // Error: abstract class (final overrider of g() is pure)

The definition of a pure virtual function may be provided (and must be provided if the pure virtual is the destructor): the member functions of the derived class are free to call the abstract base's pure virtual function using qualified function id. This definition must be provided outside of the class body (the syntax of a function declaration doesn't allow both the pure specifier = 0 and a function body)

Making a virtual call to a pure virtual function from a constructor or the destructor of the abstract class is undefined behavior (regardless of whether it has a definition or not)

struct Abstract {
    virtual void f() = 0; // pure virtual
    virtual void g() {}; // non-pure virtual
    ~Abstract() {
        g(); // okay, calls Abstract::g()
        // f(); // undefined behavior!
        Abstract::f(); // okay, non-virtual call
//definition of the pure virtual function
void Abstract::f() { std::cout << "A::f()\n"; }
struct Concrete : Abstract {
    void f() override {
        Abstract::f(); // OK: calls pure virtual function
    void g() override {}
    ~Concrete() {
        g(); // okay, calls Concrete::g()
        f(); // okay, calls Concrete::f()

See also