如何在 C++ 中声明接口?
接口描述了 C++ 类行为或功能,而无需承诺特定类的特定实现。
C++ 接口是使用抽象类实现的,这些抽象类不应与数据抽象混淆,数据抽象是将实现细节与相关数据分离的概念。
通过将类中的至少一个函数声明为纯虚函数,可以使类成为抽象类。纯虚函数通过在其声明中放置“= 0”来指定,如下所示:
class Box {
public:
// pure virtual function
virtual double getVolume() = 0;
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};抽象类(通常称为 ABC)的目的是提供一个合适的基类,其他类可以从中继承。抽象类不能用于实例化对象,仅作为接口使用。尝试实例化抽象类的对象会导致编译错误。
因此,如果 ABC 的子类需要被实例化,它必须实现每个虚函数,这意味着它支持由 ABC 声明的接口。如果派生类中没有覆盖纯虚函数,然后尝试实例化该类的对象,则会发生编译错误。
可以用来实例化对象的类称为具体类。
考虑以下示例,其中父类提供了一个接口,供基类实现名为getArea()的函数:
示例代码
#include <iostream>
using namespace std;
class Shape { // Base class
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape { // Derived classes
public:
int getArea() {
return (width * height);
}
};
class Triangle: public Shape {
public:
int getArea() {
return (width * height)/2;
}
};
int main(void) {
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}输出
Total Rectangle area: 35 Total Triangle area: 17
您可以看到抽象类如何根据 getArea() 定义接口,以及其他两个类如何实现相同的函数,但使用不同的算法来计算特定形状的面积。
设计策略
面向对象系统可以使用抽象基类来提供适用于所有外部应用程序的通用且标准化的接口。然后,通过从该抽象基类继承,形成操作方式类似的派生类。
外部应用程序提供的功能(即公共函数)在抽象基类中作为纯虚函数提供。这些纯虚函数的实现是在对应于应用程序特定类型的派生类中提供的。
这种架构还允许轻松地将新应用程序添加到系统中,即使在系统已定义之后也是如此。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP