-2

What is the meaning of separating interface from implementation in C++? And also what is implied by interface and implementation?

1

It means that you should use objects through interface classes (pure virtual) instead of concrete classes. This will allow you to change the concrete class while keeping the rest of the code intact (useful for refactoring, testing, etc)

For example if you have an interface

class INotify
{
public:
  virtual void Notify() = 0;
};

Then when your notifier uses it:

class Notifier
{
  // ...
  void Subscribe(INotify* subscriber)
  {
    m_subscribers.push_back(subscriber);
  }

  void NotifyAll()
  {
    for(auto it = m_subscribers.begin(); it != m_subscribers.end(); ++it)
    {
      (*it)->Notify();
    }
  }
  // ...
}

Notice how the Notifier is not coupled to any specific class implementation? Any class that implements INotify will work. This will allow you to use more than one class as your subscribers, it will allow you to refactor your subscribers without having to touch the Notifier and it will allow you to test the Notifier by mocking subscribers.

And for your second question, the interface is the public contract of a class (the operations that you can do with it) and implementation is the actual realization of that contract (e.g. a subscriber class inherits INotify and implements the contract by providing definition for Notify method).

In the example above, I showed only the interface of INotify but I didn't show any classes that implement that interface (inherit INotify and define an implementation for the pure virtual methods). I did this on purpose as I wanted to stress how the implementation is not relevant for the notifier, but only the interface (contract) is.

  • 1
    Nice answer. However, I believe it is not complete. – Christophe May 27 '17 at 14:39
  • @Christophe I agree. But imo, it only has the relevant part :p ... I don't consider c++'s header/cpp split as interface/implementation as the header at a minimum contains much more than just the "interface" (e.g. private and protected members). Even in the quote from Stroustrup, it says "like an interface" – Rado May 27 '17 at 15:38
1

The term "interface" is ambiguous in the C++ language, in the sense that it is not formally defined.

General definition

An interface is the specification of how a function, a class or an object interacts with the rest of the code. This meaning is for example used in the C++ standard:

17.5.1.3/3: Interface convention requirements are stated as generally as possible. Instead of stating “class X has to define a member function operator++(),” the interface requires “for any object x of class X, ++x is defined.” That is, whether the operator is a member is unspecified.

It is also used consistently by Bjarne Stroustrup in The design and evolution of C++, Addison-Wessley, 1994, for example when he explains core principles for the language design decisions he made for classes:

(5) function definitions are typically specified elesewhere to make a class more like an interface specification than a lexical mechanism for organizing source code.

In other words, this allows you to have a class declared in a header (interface that can be used across all the compilation units), and the implementation of its members defined in a compilation unit, which inernl's don't have to be known by the external world to use the interface.

Specific programming technique

The term "interface" gained popularity with Java, where it is a language feature used to complement the single inheritance. In C++ you can do the same by using an inheritance from abstract so-called interface class, as suggested in the C++standard:

10.4/1: An abstract class can also be used to define an interface for which derived classes provide a variety of implementations.

For this just look at the very good example of Rado's answer

  • 1
    @Laiv yes. However, I used this formulation so to avoid reanimating the usually heated discussion about whether or not MI is a good thing (I love it but don't want to offend anybody) ;-) – Christophe May 27 '17 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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