Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Class vs Interface in C++ [duplicate]

Possible Duplicate:
How do you declare an interface in C++?

This is a general question about C++. As you know, there is no clear distinction between interface and abstract class in C++ unlike Java and C#. When would it be more preferrable to use an interface instead of an abstract class in C++? Could you give some examples?

like image 851
fatma.ekici Avatar asked Oct 12 '12 08:10

fatma.ekici


People also ask

Which is faster abstract or interface?

The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class. It is used to implement the core identity of class.

Is abstract class better than interface?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Is abstract class and interface are same?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.

Why abstract class is faster than interface?

4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java.


2 Answers

I assume that with interface you mean a C++ class with only pure virtual methods (i.e. without any code), instead with abstract class you mean a C++ class with virtual methods that can be overridden, and some code, but at least one pure virtual method that makes the class not instantiable. e.g.:

class MyInterface { public:   // Empty virtual destructor for proper cleanup   virtual ~MyInterface() {}    virtual void Method1() = 0;   virtual void Method2() = 0; };   class MyAbstractClass { public:   virtual ~MyAbstractClass();    virtual void Method1();   virtual void Method2();   void Method3();    virtual void Method4() = 0; // make MyAbstractClass not instantiable }; 

In Windows programming, interfaces are fundamental in COM. In fact, a COM component exports only interfaces (i.e. pointers to v-tables, i.e. pointers to set of function pointers). This helps defining an ABI (Application Binary Interface) that makes it possible to e.g. build a COM component in C++ and use it in Visual Basic, or build a COM component in C and use it in C++, or build a COM component with Visual C++ version X and use it with Visual C++ version Y. In other words, with interfaces you have high decoupling between client code and server code.

Moreover, when you want to build DLL's with a C++ object-oriented interface (instead of pure C DLL's), as described in this article, it's better to export interfaces (the "mature approach") instead of C++ classes (this is basically what COM does, but without the burden of COM infrastructure).

I'd use an interface if I want to define a set of rules using which a component can be programmed, without specifying a concrete particular behavior. Classes that implement this interface will provide some concrete behavior themselves.

Instead, I'd use an abstract class when I want to provide some default infrastructure code and behavior, and make it possible to client code to derive from this abstract class, overriding the pure virtual methods with some custom code, and complete this behavior with custom code. Think for example of an infrastructure for an OpenGL application. You can define an abstract class that initializes OpenGL, sets up the window environment, etc. and then you can derive from this class and implement custom code for e.g. the rendering process and handling user input:

// Abstract class for an OpenGL app. // Creates rendering window, initializes OpenGL;  // client code must derive from it  // and implement rendering and user input. class OpenGLApp { public:   OpenGLApp();   virtual ~OpenGLApp();   ...    // Run the app       void Run();     // <---- This behavior must be implemented by the client ---->    // Rendering   virtual void Render() = 0;    // Handle user input   // (returns false to quit, true to continue looping)   virtual bool HandleInput() = 0;    // <--------------------------------------------------------->   private:   //   // Some infrastructure code   //   ...    void CreateRenderingWindow();   void CreateOpenGLContext();   void SwapBuffers(); };   class MyOpenGLDemo : public OpenGLApp { public:   MyOpenGLDemo();   virtual ~MyOpenGLDemo();    // Rendering   virtual void Render();  // implements rendering code    // Handle user input   virtual bool HandleInput(); // implements user input handling     //  ... some other stuff }; 
like image 113
Mr.C64 Avatar answered Oct 25 '22 21:10

Mr.C64


interface were primarily made popular by Java.
Below are the nature of interface and its C++ equivalents:

  1. interface can contain only body-less abstract methods; C++ equivalent is pure virtual methods, though they can/cannot have body
  2. interface can contain only static final data members; C++ equivalent is static const data members which are compile time constants
  3. Multiple interface can be implemented by a Java class, this facility is needed because a Java class can inherit only 1 class; C++ supports multiple inheritance straight away with help of virtual keyword when needed

Because of point 3 interface concept was never formally introduced in C++. Still one can have a flexibility to do that.

Besides this you can refer Bjarne's FAQ on this topic.

like image 39
iammilind Avatar answered Oct 25 '22 22:10

iammilind