Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
In my team, we always call it interface class. –  John Zhang Jul 31 '13 at 14:35
    
Worthy of note: The term "interface" is the third letter in API. Its use in that context is a sort of abbreviation of the term. –  greyfade Aug 22 '13 at 8:41
    
It's not uncommon to see static const int Foo = 42; or enum colors {Red, Green, Blue}; in such classes, too. –  MSalters Aug 22 '13 at 12:00
1  
Hopefully your "interface" includes a virtual destructor as well ;) –  ssgriffonuser Aug 22 '13 at 18:11
add comment

2 Answers

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

share|improve this answer
    
Look where the 'Interfaces (Object Oriented Programming)' Wikipedia article redirects: en.wikipedia.org/wiki/… –  JBRWilkinson Jul 31 '13 at 17:04
    
@JBRWilkinson I would consider this an unfortunate and misleading redirect in the context of C++. The references mentions Objective-C. –  Captain Giraffe Jul 31 '13 at 17:21
3  
@JBRWilkinson: I see where it redirects and it makes it clear that 'protocol' and 'interface' are synonyms in this context. That does not change the fact that 'interface' is the more common term among the C++ programmers that I know. –  Bart van Ingen Schenau Jul 31 '13 at 17:22
    
Using the term Interface for C++ has become quite common. However, the term Interface is borrowed from Java. Java standardized the concept of a pure virtual abstract class with the use of Interface. –  Bill Door Aug 1 '13 at 15:04
    
Historically, the term in C++ has always been "abstract base class," and a lot of documentation and books use the abbreviation ABC quite liberally. The use of the term "interface" is relatively new in the C++ community, and I, too, have never heard the term "protocol" in a C++ context. –  greyfade Aug 22 '13 at 8:39
add comment
  • You can call C++ class with only pure virtual functions as interface or protocol.

  • Interface and protocol both are the same concept.

  • If you were a java/C# programmer,you call the class with only purt virtual functions as an interface. If you were a Objective-C programmer,you call the class with only purt virtual functions as a protocol!

share|improve this answer
    
A "protocol" is the "sequence of actions that have to be taken from cooperating interfaces to perform certain tasks". It cannot be a sinonimous of "interface", since it is defined around interfaces. A protocol describes how interface have to be used. an interface describes what an object exposes. –  Emilio Garavaglia Aug 22 '13 at 12:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.