Chapter Eleven: Abstract Classes or Interfaces
You may need to describe or use classes in your programs or code without committing or linking the class to a specific type of implementation. You can do this using an interface. You can implement interfaces in C++ using a concept termed as an abstract class. Do not confuse an abstract class with the concept of data abstraction. The latter is a concept used in object-oriented programming wherein the code's implementation details are kept away or apart from the interface and data used in the code.
You can make any class abstract by declaring a pure virtual function. We discussed this in an earlier chapter. a pure virtual function is one where the value of the function is equated to zero. For example:
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
};
Most programmers use an abstract class as a base class using which they create derived classes. An abstract class is one type of class using which you cannot create, declare, assign, or initialize an object. These classes only serve the purpose of an interface. If you try to create an object and instantiate it in an abstract class, it only leads to compilation errors. Therefore, if you want to instantiate a subclass or object in an abstract class, you need to implement numerous virtual functions. Virtual functions can support any interface created or declared within an abstract class. If you do not override a pure virtual function in any derived class and declare or instantiate an object in the class, it will lead to an error. These mistakes are very small but hard to detect in the code. You can also create a concrete class to instantiate and declare objects.
Example
The following is an example of how you can use abstract classes to implement functions. In the example below, we will look at how you can implement the function getArea().
#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);
// 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;
}
When you run the above code, you receive the following output:
Total Rectangle area: 35
Total Triangle area: 17
In the above example, we see how you can use an abstract class to define an interface using the function getArea(). We also saw how you could implement this function in two other classes in the code. Each of these classes, however, uses a different algorithm to calculate the area of the shape.
When you develop an application using object-orientation, you may need to use an abstract class to provide a standard and common interface. This interface will be appropriate for any external class, application, or function you may want to use. Through inheritance, the derived classes obtain the necessary methods and data from the abstract base class. The functions, classified as public in the abstract class, should be pure virtual functions. These pure functions can only be used in the derived classes, which correspond to the specific functions and methods used in the application.
This makes it easier for you, as a programmer, to add new objects and applications to the existing code even after you have defined the system.