C++ class with only pure virtual functions: what’s that called

cinterfacesprototyping

So i'm looking for some input/consensus on what terminology we should be using to describe something that looks like this:

class Printable
{
  public:
    virtual void printTo(Printer *) = 0;
    virtual double getWidthInPoints() = 0;
    virtual double getHeightInPoints() = 0;
};

So this is a class that only has pure virtual methods. Any class that inherits it needs to implement the whole thing, i.e. there is no partial implementation.

So what is this called?

I have mostly seen two different names:

1) Interface

Used in Windows COM programming (perhaps it came from the IDL), it's a keyword in both C# and Java and also a compiler directive/keyword in Objective-C (but isn't the same thing). I've heard engineers using the phrase 'interface' synonymously with 'API', but, at least in this scenario, it's a single thing whereas an API could comprises more than one.

2) Protocol

I've seen this used in various open-source libraries such as Bloomberg BSL and it's a compiler directive/keyword in Objective-C, but does not necessarily need to be in the inheritance hierarchy. In the broader computer science sense, protocol is a formal definition of information exchange between two parties, like TCP and HTTP, but this definition kind of holds for the above class too.

Is there any clear distinction between these in C++ or are they pretty much interchangeable?

Best Answer

The technical term for a class with a pure virtual function is abstract class, but that terms also applies if the class has additional non-pure virtual functions.

Often the term Interface is used in C++ to describe an abstract class like Printable, that describes a service that can be provided by a range of different, otherwise unrelated classes. This corresponds to the use of the interface keyword in C#/Java.

I have never encountered the term 'protocol' in connection with a C++ class