Tutorial Study Image

Interfaces in C++(Abstract Classes)


June 16, 2023, Learn eTutorial
424

We will discuss C++ interfaces in this section. The interface is defined as It explains a class's behavior without revealing the implementation (code) of that class.

For example, at an ATM machine, you will be offered a list of options provided by that bank, such as withdrawal and deposit, but not really the implementation of backend code for such services.

  • Sometimes it is sufficient to simply give the user with a range of services in the form of interface options rather than revealing all the implementation specifics (details) of your project.
  • When necessary, this concealing of implementation details is referred to as an interface.

In C++, abstraction is accomplished using abstract classes. In C++, abstraction is the technique of hiding internal details and simply displaying functionality. There are two approaches in order to achieve abstraction:

  • Abstract class
  • Interface

Both abstract classes and interfaces are capable of having the requisite abstract methods.

Abstract class in C++

In C++, a class is abstracted by declaring at least one of its functions to be a >strong>pure virtual function. "= 0" is used to indicate a pure virtual function in a declaration. The classes from which it is derived must offer its implementation. Let us look at an abstract class in C++ with one abstract method draw (). The derived classes Circle and Rectangle offer their implementation. Both classes use various implementations.

Example program for abstract class


#include <iostream>  
using namespace std;  
 class Shape    
{    
    public:   
    virtual void draw()=0;    
};    
 class Rectangle : Shape    
{    
    public:  
     void draw()    
    {    
        cout <<"drawing the rectangle..." <<endl;    
    }    
};    
class Circle : Shape    
{    
    public:  
     void draw()    
    {    
        cout <<"drawing the  circle..." <<endl;    
    }    
};    
int main( ) {  
    Rectangle rec;  
    Circle cir;  
    rec.draw();    
    cir.draw();   
   return 0;  
}  

 

Output:


drawing the rectangle...
drawing the  circle...

Designing Strategy

An abstract base class can be used by an object-oriented system to offer a uniform and common interface that is suitable for all external applications. Then, by inheriting from that abstract base class, derived classes that operate similarly are generated.

The abstract base class offers the capabilities (i.e., the public functions) provided by the external applications as pure virtual functions. The derived classes that are appropriate for the various types of applications offer the implementations of these pure virtual functions.

What do you mean by interfaces in C++?

A C++ class's behavior or capabilities are described through an interface without making any assumptions about the class's implementation.

It is important to distinguish between data abstraction, which is the idea of keeping implementation details apart from connected data, and abstract classes, which are used to implement C++ interfaces.

A class becomes abstract when at least one of its functions is declared as a pure virtual function. Adding "= 0" to a function's declaration designates it as a pure virtual function.


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

Providing a suitable base class from which other classes can inherit is the function of an abstract class, (often known as an ABC). Abstract classes are only used as interfaces and cannot be utilized to create objects. A compilation error occurs when an abstract class object is attempted to be instantiated.

As a result, in order to support the interface stated by the ABC, a subclass of an ABC must implement each of the virtual functions before it can be instantiated. A compilation error occurs when a pure virtual function in a derived class is not overridden before attempting to instantiate objects of that class.

Concrete classes are those that can be used to create objects.

An abstract class is one that has at least one of its functions declared as a pure virtual function, and the pure virtual function must be declared with public access. Abstract classes are used to implement syntax interfaces.

Syntax


class class_name 
{
  public:
  // pure virtual function
  virtual return-type func_name() = 0;
};
 

When defining a pure virtual function, use (=0) in the declaration.

Take a look at the example below, where the parent class gives the child class an interface to implement the getArea() declaration function.

Example of a C++ program showing interfaces


#include <iostream>
 
using namespace std;
 
// Base class
class Shape {
   public:
      // pure virtual function just for 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 << "The Total Rectangle area: " << Rect.getArea() << endl;

   Tri.setWidth(5);
   Tri.setHeight(7);
   
   // Print the area of the object.
   cout << "The Total Triangle area: " << Tri.getArea() << endl; 

   return 0;
}


Output:


The Total Rectangle area: 35
The Total Triangle area: 17

You can see how two other classes implemented the same function but used various algorithms to calculate the area particular to the shape, while an abstract class created an interface in terms of getArea().

Remember this about the abstract classes:

If you attempt to construct an object from an abstract class, a compile-time error will occur since you are not permitted to use it and it can only be used as an interface.

The only class that is permitted to generate and use objects is the child class that is implementing the parent abstract class, and that class must define all of the parent class's pure virtual functions.

Why interfaces?

An abstract base class in the OOP design system will offer a single generic interface that is suitable and usable for all external applications of the same type.

Even after a software system has been developed, interface mechanisms make it possible for applications to quickly add new software updates and make other modifications to the existing system.