Interfaces in C++ (Abstract Classes)

    In C++, we define interface as an un-committing implementation of a class, and to implement the interface, we use the abstract class. Do not confuse the abstract class with Data Abstraction, both are different concepts. In C++, when a class inherits the properties of another class, it is known as inheritance. Also, inheritance is one of the most important features of Object-Oriented Programming . But what if we want to create such a class that if some other classes inherit it, then they have to create a member function that we have not declared in our class. Well, for this, we will create an abstract class. The overall concept of the Abstract class is that sometimes the base class does not know the implementation and definition of some member functions and it wants the derived class to have their specific definition on those member functions. In this tutorial, we will discuss interfaces (abstract classes) in C++ and explain their use with examples.

    What is an Abstract Class in C++?

    Classes that have one or more Pure Virtual Functions are known as abstract classes, and they are also known as interfaces in C++. An abstract class is always used to serve as a Base class and we do not create its object. If we try to create an object of the abstract class, the compiler would through an error.

    Pure Virtual Functions

    Pure virtual functions are also known as abstract functions. A pure virtual function is a virtual function that has no implementation and definition. Syntax to write a Pure virtual Function:

     virtual return_data_type function_name() = 0;

    Example :

    virtual int set() =0;

    Here set is a pure virtual function because it has no definition and implementation. Example of abstract class:

    #include <iostream>
    using namespace std;
    
    class Shapes
    {
      protected:
         int height, length, width, radius;
      public:
    
        virtual int area()=0;  //every class who inherit shape need to define this function         
    
        void set_height(int h)
          {
               height =h;
          }
        void set_width(int w)
          { 
               width=w;
          }             
    
        void set_radius(int r)
          { 
               radius=r;
          }
    };
    
    class Circle:public Shapes
    {
      public:
         int area()  // defining area for Circle
           {
               int ar;
               ar= 3.14*radius*radius;
               cout<<"Area of Circle is: "<<ar<<endl;
           }
    };
    
    class Rectangle:public Shapes
    {
      public:
         int area()  // defining area for Rectangle
           {
              int ar;
              ar= height*width;
              cout<<"Area of Rectangle is: "<<ar<<endl;
           }
    };
    
    int main() 
    {
       Rectangle rec;
       rec.set_height(10);
       rec.set_width(20);
    
       Circle cir;
       cir.set_radius(40);
     
       rec.area();
       cir.area();
       return 0;
    }

    Output:

    Area of Rectangle is: 200
    Area of Circle is: 5024

    Behind the Code: In the above example, Shapes is an abstract class because it has a virtual function virtual int area()=0;. So when we inherit the Shapes class via Rectangle and Circle , we define the area() member function for each class, and that’s how we implement an abstract class.

    Takeaway

    Following are the key points to remember about the abstract class:

    • A class having one or more than one pure virtual function is known as an abstract class.
    • A pure virtual function is a virtual function with no definition.
    • An abstract class can be worked as a base class and we cannot create an object for it, and that's why an abstract class is also known as Interface.
    • The pure virtual function must have a definition in the derived class.

    People are also reading: