C++

Abstract Classes C++

“A class in C++ Programming language is considered abstract if it contains at least a single virtual method. The virtual method can be defined by the classes that inherit the abstract class; otherwise, the subclass would have become an abstract class. Abstract classes have been used to construct the C++ APIs; however, data abstraction, which will be the idea of maintaining configuration settings apart from associated data, may not be mixed up with abstract methods. An abstract class operates as a base class from which certain classes may derive. Let’s see the different examples of an abstract class.”

Example no 1

The abstract class “S” and its members are constructed in the following example:

#include <iostream>
using namespace std;
class S {
   public:
      virtual int Area() = 0;
      void setWidth(int a) {
         wid = a;
      }
      void setHeight(int b) {
         hght = b;
      }  
   protected:
      int wid;
      int hght;
};
class Rectangle: public S {
   public:
      int Area() {
         return (wid * hght);
      }
};
class Triangle: public S {
   public:
      int Area() {
         return (wid * hght)/2;
      }
};
int main() {
  Rectangle Rec;
  Triangle Tri;
  Rec.setWidth(15);
  Rec.setHeight(50);
  Tri.setWidth(70);
  Tri.setHeight(88);
  cout << "The calculated area of the required rectangle: " << Rec.Area() << endl;
  cout << "The calculated area of the required triangle: " << Tri.Area() << endl;
}

At the start of the program, we will include the required library <iostream>. Then we have been using the standard namespace as std. Now we will set up the class named “S”. We publicly declare a virtual method. The virtual function we used here is Area(). Then we will employ the function setWidth(). The width of the shape can be specified using this function. The value of the width is stored in a variable “a”.

Now we will specify the value of the height of the shape, so we have been using the setHeight() function. We initialize a variable “b” to store the height of the shape. We set the values of width and height as protected. The data type of both width and height will be an integer. In the next line, we will inherit a class Rectangle shape from the class “S”. Here, we find the area of the rectangle, so the Area() method will be applied. It returns the area of a rectangle by multiplying the value of width by the value of the rectangle.

After all this, we will inherit another shape, “triangle”, from the class “S”. Now, we apply the formulae for obtaining the area of the triangle. The Area() function is being called. This returns the resultant value. We get the area of a triangle by multiplying the width by the height and then dividing the value by 2. Further, we will call the main() function. We define the objects of both classes, Rectangles and triangles, respectively.

Now we have to set the values of the width and height of the rectangle and triangle by using the setWidth() and setRectangle() methods correspondingly. In the end, we have employed the “cout” statement to display the calculated area of the rectangle and triangle.

Example no 2

Classes that inherit from an Abstract class provide some virtual methods. We will execute the instance in which we inherit the classes from the Abstract class:

#include<iostream>
using namespace std;

class Base
{
int n;
public:
    virtual void f() = 0;
    int getA() { return n; }
};
class Derived: public Base
{
    int m;
public:
    void f() { cout << "f() is being called"; }
};

int main(void)
{
    Derived x;
    x.f();
    return 0;
}

First, we have been integrating the header file <iostream>. Along with this, we will use standard namespace as std. Then we define the class termed “Base”. Within this class, we initialize a variable “n,” and the data type of this variable will be set as an integer. Here we call the function of this class and set these functions as public. We have been calling the void f() method of the class “Base”. Then we also utilize the get() function.

Further, we will inherit the derived class from the parent class. As a result, we derive the base class from it. Within the Derived class, we will declare a variable “m”. Now we publically call the function void f(). Within this function, the “cout” statement will have been used to show the line on the screen. Let’s begin the code in the main() function’s body. To do this, we have to first call the main() function. We call the function f() associated with the derived class. We have been applying the “return 0” command at the end of the code.

Example no 3

Constructors may be present in an abstract class. We will create the constructor in the succeeding illustration:

#include<iostream>
using namespace std;

class B
{
protected:
int u;
public:
virtual void f() = 0;
Base(int m) {
            u = m;
            cout<<"Calling the base constructor\n";
            }
};

class Derived: public B
{
    int v;
public:
    Derived(int m, int n):B(m) { v = n; }
    void f() { cout << "u = " << u << ", v = " << v<f();
    return 0;
}

Here we have been including the module <iostream>. Then we include the standard namespace as std. We will construct the Base class with the constructor. Here the Base class acts as an abstract class. We will initialize the variable “u” and set it to be protected. First, we have been calling the void f() function. Further, we will call the Base() function. This function contains a variable as its argument. We have used the “cout” statement inside this function. We will inherit the derived class from the “Base” class in the following step. Within the Derived class, we will initialize a variable “v”.

Now we publically call the function Derived(). We pass two variables as its parameters. We call the B() function. Then the void f() is called. Within this function, the “cout” statement will have been applied to show the values. We have to invoke the main() function. Here, we construct the Derived class object. This function contains two values as its attribute. We call the function f() associated with the derived class. We will enter the “return 0” command to terminate the program.

Conclusion

We have gone through the abstract class in C++ in this tutorial. Abstract class items are difficult to create. In C++, an abstract method is a virtual method that could have a definition. However, the derived class would override the abstract method to prevent the derived class from becoming an abstract class as well. The allocation of 0 during declaration creates a virtual method.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.