C++ “Interfaces”

Bringing It All Together

  • C++ has a toolset for

    • Inheritance

    • Dynamic dispatch

  • Does not make any policies

  • Java does

  • Programmer’s choice how to use tools

  • ⟶ very easily incomprehensible code is created

  • ⟶ best practices!

Interfaces

Bringing it all together: an interface is defined like so …

  • Virtual destructor, with an empty implementation (remember, destructors cannot be abstract/pure virtual)

  • Only abstract/pure virtual methods

class Interface
{
public:
    virtual ~Interface() {}
    virtual void method() const = 0;
};
  • Ideally nothing else

  • Programmers are invited to break the rules!

  • ⟶ Hybrid forms, e.g. providing little helper methods in the interface’s protected interface

  • ⟶ Personal taste, combined with respect for colleagues

Note

C++11 brings features for derived classes: