C++ Interfaces

C++ Interfaces are also a part of object oriented programming OOP. Interface represents the behavior of a class. That means interface can show the list of functions that a class performs. Abstract classes help us achieve the functionality of interfaces. Abstract classes are not related to Data Abstraction. Abstract classes help achieve C++ interfaces while abstraction is the feature that hides implementation details of data and show only needed information. Abstraction can be achieved through:

  • Interface
  • Abstract Classes

Abstract Class

An abstract class is a class that contains at least one pure virtual function. Abstract class is the main class that can have no objects. If programmer tries to instantiate an object of abstract class, then in this case error arises in compilation. Abstract class acts as base class and is only used to derive other classes from. The derived classes can have their own separate definition of pure virtual functions. An abstract class is defined and declared in the same way an ordinary class is.

Pure Virtual Function

Pure virtual function contains virtual keyword in its declaration. The pure virtual function declaration also includes “=0” specification. The syntax for declaring a pure virtual function is as follows:

Here virtual is the keyword, return_type is the return type of function. The func_name is the name of function. For example, the function below is a pure virtual function.

In the above example, virFunc() is a pure virtual function. The pure virtual function can also be an abstract function as it has no body.

For example, the code below shows how to declare a pure virtual function in an abstract class and how to define it in different ways in derived classes.

#include <iostream>
using namespace std;
class Object {
   public:
      virtual int findArea() = 0;
      void setLen(int l) {
         length = l;
      }
      void setBred(int b) {
         breadth = b;
      }
   protected:
      int length;
      int breadth;
};
class RectangleObj: public Object {
   public:
      int findArea() { 
         return (length * breadth); 
      }
};
class TriangleObj: public Object {
   public:
      int findArea() { 
         return (length * breadth)/2; 
      }
};
 int main(void) {
   RectangleObj R1;
   TriangleObj  T1;
   R1.setLen(6);
   R1.setBred(9);
   cout << "Area of Rectangular object is: " << R1.findArea() << endl;
   T1.setLen(3);
   T1.setBred(8);
   cout << "Area of Triangular object is: " << T1.findArea() << endl; 
   return 0;
}

In the above code, there is an Abstract class Object. It has two derived classes RectangleObj and TriangleObj. findArea() is a pure virtual function. For the class RectangleObj it has different implementation and for the object of TriangleObj, it has different implementation of finding area. The objects of derived classes (T1 and R1) access the pure virtual function. The output of the above code after compilation is:

However C++ programming language allows abstract class to have pointer that points to the instance of derived class. For example the following syntax is allowed:

Object *obj = new RectangleObj();
obj->setLen(6);
obj->setBred(9);
obj->findArea();

In the above code, the pointer *obj of abstract class Object points to the instance of derived class RectangleObj. The Arrow operator (->) is used to access the methods of derived class. Abstract classes can also have constructors.

Abstract classes declare the C++ interface. The other name for abstract class is ABC. An abstract class or ABC can have more than one pure virtual functions. Each pure virtual function must be defined in derived class. Defining pure virtual functions in derived class means that the derived class is using the interface of ABC. If pure virtual function’s definition is not present in any of the derived classes then in this case compiler gives an error.

Concrete Class

The classes that can instantiate objects are concrete classes. For example all the derived classes in the above example are concrete classes.

Advantages of C++ Interface

Abstract classes provide interfaces that can have more than one external applications. C++ Interfaces give programmers a flexibility to use the same function in different ways, without having to declare it with different name in each derived class. It also helps us maintain more than one version of function’s definition.