What is the difference between Abstract and the interface in the C ++ context?

advertisements

An interface in C++ is implemented as an abstract class i.e. pure virtual method. Similarly, an abstract class in C++ is also implemented the same way. Can I say that interface and abstract is same in C++, except the concept that interface may be a verb - the behaviour and the abstract class is a noun. Is my understanding right? I am asking this question because design patterns - talks about interface and abstract differently. Also, the reason is that JAVA has two different keywords - interface and abstract. We need to implment an interface in java. And we need to extend the abstract class. However, in CPP, we only inherit from the abstract class and then implement.


As you said, an interface in C++ is implemented using an abstract class with only pure virtual functions. A pure virtual function must be overridden by any concrete (i.e., non-abstract) derived class. By adding expression =0 to a virtual function, we indicate that virtual function is a pure virtual function. An abstract class is a class that contains at least one pure virtual function.

For example:

class AbstractBase {
    public:
      const char* Hello() { return "Hello World!"; } // a normal non-virtual function    

      virtual void Method_1() {} // a normal virtual function

      virtual int Method_2() = 0; // a pure virtual function makes AbstractBase class not instantiable
  };  

class InterfaceBase {
    public:
      const char* Hello() = 0; // a pure virtual function    

      virtual void Method_1() = 0; // a pure virtual function

      virtual int Method_2() = 0; // a pure virtual function
  };