C++ Interface (Abstract Class)
An interface describes the behavior and functionality of a class without specifying the particular implementation of the class.
C++ interfaces are implemented using abstract classes, which are distinct from data abstraction, a concept that separates implementation details from associated data.
A class is considered abstract if it has at least one function declared as a pure virtual function. Pure virtual functions are specified by using "= 0" in their declaration, as shown below:
class Box
{
public:
// Pure virtual function
virtual double getVolume() = 0;
private:
double length; // Length
double breadth; // Breadth
double height; // Height
};
The purpose of designing an abstract class (commonly referred to as ABC) is to provide other classes with an appropriate base class that can be inherited. Abstract classes cannot be used to instantiate objects; they can only be used as interfaces. Attempting to instantiate an object of an abstract class will result in a compilation error.
Therefore, if a subclass of an ABC needs to be instantiated, it must implement each virtual function, which means C++ supports the use of ABCs to declare interfaces. Failing to override the pure virtual function in the derived class and attempting to instantiate an object of that class will result in a compilation error.
Classes that can be used to instantiate objects are called concrete classes.
Example of an Abstract Class
Consider the following example where the base class Shape provides an interface getArea(), which is implemented differently in the derived classes Rectangle and Triangle:
#include <iostream>
using namespace std;
// Base class
class Shape
{
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;
};
// Derived classes
class Rectangle: public Shape
{
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);
// Output the area of the object
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Output the area of the object
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Total Rectangle area: 35
Total Triangle area: 17
From the above example, we can see how an abstract class defines an interface getArea(), and how two derived classes implement this same function with different algorithms for calculating the area.
Design Strategy
Object-oriented systems may use an abstract base class to provide a common, standardized interface for all external applications. Then, derived classes inherit all similar operations from the abstract base class.
The functionalities provided by external applications (i.e., public functions) exist in the abstract base class as pure virtual functions, which are implemented in the corresponding derived classes.
This architecture also allows for new applications to be easily added to the system, even after the system has been defined.