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?
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.
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.
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.
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.
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 };
interface
were primarily made popular by Java.
Below are the nature of interface
and its C++ equivalents:
interface
can contain only body-less abstract methods; C++ equivalent is pure virtual
methods, though they can/cannot have bodyinterface
can contain only static final
data members; C++ equivalent is static const
data members which are compile time constantsinterface
can be implement
ed 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 neededBecause 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With