0

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.

user3629119
  • 151
  • 1
  • 9
  • possible duplicate of [How do you declare an interface in C++?](http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c) – edmz Nov 03 '14 at 13:10

6 Answers6

5

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 
  };
DML
  • 494
  • 5
  • 4
0

No, abstract classes can provide default behavior for methods; interfaces are pure virtual and cannot. This is fundamentally different.

the behaviour and the abstract class is a noun

This makes no sense. Behavior is always the verb.

The point of abstract classes and interfaces is polymorphism: different behaviors depending on runtime type.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

If all it's methods are abstract, and there are no data members, a C++ class is indeed and interface. However, a class is already abstract if it has at least one abstract method. There can still be definitions for other virtual functions, non-virtual functions, and data members. None of those belongs into an interface.

Raoul Steffen
  • 527
  • 4
  • 10
0

The reason (or a reason) why Java has a specific syntax for interface classes is because Java doesn't support multiple inheritance (of non-interface classes).

C++ on the other hand does support multiple inheritance and therefore does not need the definition of interface class on the language level.

In c++ class is abstract if it has at least one pure virtual member function. In OO design classes with no method implementations are called interfaces. Therefore considering that point of view, while there is no special syntax for it in the language, an interface in c++ is simply an abstract class that has no member functions that are not pure virtual, i.e. all the member function are pure virtual. So, interface classes are a subset of abstract classes, rather than the same thing as you propose.

Of course, the word interface has more meanings than just the one in the context of interface classes. For example, you would typically consider the interface of a library being everything defined in the header file(s) of that library. This includes all class definitions, functions, globals and their documentation.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

The problem you are facing is mostly due to the fact that "classical OOP terminology" had been developed independently of the C++ language, that -in turn- is not a OOP pure language.

Despite this fact, there is an historical tendency, in many programming courses, to teach C++ through OOP, thus forcing OOP paradigm into C++ even where the C++ paradigm are wider.

In pure OOP terminology "interface" means "describing an external behavior" (in contrast with "implementation") , while "abstract" means "describing something at an higher level" (in contrast with "concrete")

Il language like Java, where OOP substitution is implemented with inheritance that has been designed for exactly that purose, an "interface" is "abstract" and a class is "More and more concrete" as going down the object hierarchy.

C++ inheritance is different: it has -in its very basics- nothing to do with OOP substitution and java inheritance, does not require methods to be virtual, and can be multiple. In a generic paradigm sense, it's just an aggregation mechanism the language made available to put things together.

The set of problems you can describe with it is wider then the ones OOP itself addresses, and in the same time does not cover all the Java ones. But you can still fit them if you force yourself into certain discipline.

Hence:

  • A class with all pure virtual methods (virtual method(params)=0) and a virtual dcestructor, viewed from an OOP perspective plays like an interface
  • A class with all the virtual inherited methods implemented it's like an implementation.

But C++ also admit all the intermediate cases (where only some methods are implemented) and admits the origin of the implementation to come not only by a same path of the inheritance ancestor trees.

In fact C++ admits a full "multiple inheritance scheme", while java admits a "partial multiple inheritance", with only one class and many interfaces (that's like to say all implemented methods must come from a same side of a tree)

You can force C++ to obey the same discipline as java by admitting (artificially, at this point) that only an inherited base class can provide methods, while all other can only declare them, and call "class" the first and "interface" the others.

But by a language stand point, there's no conceptual distinction between them.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
0

Consider it this way : There are two types of functions

  1. Concrete - One that has some code and actually performs a task.
  2. Virtual -One that does not have any code and is overridden by some other concrete class according to the local need of that class. A virtual function is specified by placing "= 0" in its declaration.

Now, an interface is an empty shell in which all the functions must be virtual functions.It is just a pattern.It is intended to be completely modified by the programmer by overriding its virtual functions.

But, an abstract class is a class that is only partially implemented by the programmer.That is, it must have at least one virtual function, but not necessarily all.

coder_r
  • 90
  • 2
  • 11