An abstract class would be used when some common implementation was required. An interface would be if you just want to specify a contract that parts of the program have to conform too. By implementing an interface you are guaranteeing that you will implement certain methods. By extending an abstract class you are inheriting some of it's implementation. Therefore an interface is just an abstract class with no methods implemented (all are pure virtual).

interface主要是通过 Java 流行起来的。
下面是 interface及其 C + + 等价物的性质:

  1. interface只能包含没有 body 的抽象方法; C + + 等价于纯 virtual方法,尽管它们可以/不能有 body
  2. interface只能包含 static final数据成员; C + + 等效的是 static const数据成员,它们是 编译时间常数
  3. 多个 interface可以通过 Javaclass进行 implemented,如下所示 因为 Javaclass只能继承1 class; C++ supports multiple inheritance straight away with help of virtual 关键字

因为第三点 interface的概念在 C + + 中从来没有正式介绍过。人们仍然可以灵活地做到这一点。

除此之外,你可以参考比亚内的 常见问题关于这个主题。

我假设在 接口中,你指的是一个只有 纯粹的虚拟方法的 C + + 类(即没有任何代码) ,而在 抽象类中,你指的是一个具有可以被重写的虚方法和一些代码的 C + + 类,但是 at least one pure virtual method使这个类不可实例化。 例如:

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, 接口 are fundamental in COM. In fact, a COM component exports only interfaces (i.e. pointers to 桌子, 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. 换句话说,对于接口,客户机代码和服务器代码之间具有很高的解耦性。

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

如果我想定义一组规则,使用这些规则可以对组件进行编程,而不需要指定具体的特定行为,那么我会使用 接口。实现此接口的类本身将提供一些具体的行为。

相反,当我想提供一些默认的 基础设施代码和行为时,我会使用一个 抽象类,并使客户端代码有可能从这个抽象类派生,用一些自定义代码覆盖纯虚方法,用自定义代码覆盖 complete这个行为。 以 OpenGL 应用程序的基础设施为例。 你可以定义一个抽象类来初始化 OpenGL,设置窗口环境等等,然后你可以从这个类派生出来并实现自定义代码,例如渲染过程和处理用户输入:

// 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
};

请不要将成员放入接口中,尽管措辞是正确的。请不要“删除”接口。

class IInterface()
{
Public:
Virtual ~IInterface(){};
…
}


Class ClassImpl : public IInterface
{
…
}


Int main()
{


IInterface* pInterface = new ClassImpl();
…
delete pInterface; // Wrong in OO Programming, correct in C++.
}

纯虚函数主要用于定义:

a) abstract classes

这些是基类,您必须从它们派生,然后实现纯虚函数。

B)界面

这些是“ em pty”类,其中所有函数都是纯虚函数,因此必须派生并实现所有函数。

Pure virtual functions are actually functions which have no implementation in base class and have to be implemented in derived class.