Implementar interfaces usando clases abstractas en C++
 
En Java, las herencias múltiples se logran usando interface. interface es como una plantilla porque contiene el diseño necesario pero es abstracto o no tiene cuerpo de método.
Cuando se trata de C++, no hay necesidad de interfaces porque C++ admite herencias múltiples, a diferencia de Java. En C++, la funcionalidad de las interfaces se puede lograr usando una clase abstracta.
Concepto de Clase Abstracta en C++
Una clase abstracta es una clase con al menos una función virtual pura. Solo es posible declarar una función virtual pura, no puede haber definición para ella, y se declara asignando 0 en la declaración.
Las clases abstractas son muy útiles para hacer un código reutilizable y extensible. Esto se debe a que es posible crear muchas clases derivadas que heredan de la clase principal pero con sus definiciones únicas.
Además, las clases abstractas no pueden ser instanciadas.
Ejemplo:
class A {
 public:
  virtual void b() = 0;  // function "b" in class "A" is an example of a pure
                         // virtual function
};
Durante la declaración de la función, tratar de hacer que la función sea puramente virtual mientras se define no funcionaría.
Implementar una función virtual pura en un programa usando C++
Considere un ejemplo con shape como clase principal. Cuando se trata de formas, hay muchos tipos diferentes de ellas, y cuando necesitamos calcular propiedades, por ejemplo, el área, la forma en que lo calculamos difiere de una forma a otra.
Un rectangle y un triangle como clases derivadas que heredan de la clase padre de shape. Luego, proporcionaremos la función virtual pura con la definición necesaria para cada forma (rectangle y triangle) en su propia clase derivada.
Código fuente:
#include <iostream>
using namespace std;
class Shape {
 protected:
  int length;
  int height;
 public:
  virtual int Area() = 0;
  void getLength() { cin >> length; }
  void getHeight() { cin >> height; }
};
class Rectangle : public Shape {
 public:
  int Area() { return (length * height); }
};
class Triangle : public Shape {
 public:
  int Area() { return (length * height) / 2; }
};
int main() {
  Rectangle rectangle;
  Triangle triangle;
  cout << "Enter the length of the rectangle: ";
  rectangle.getLength();
  cout << "Enter the height of the rectangle: ";
  rectangle.getHeight();
  cout << "Area of the rectangle: " << rectangle.Area() << endl;
  cout << "Enter the length of the triangle: ";
  triangle.getLength();
  cout << "Enter the height of the triangle: ";
  triangle.getHeight();
  cout << "Area of the triangle: " << triangle.Area() << endl;
  return 0;
}
Producción :
Enter the length of the rectangle: 2
Enter the height of the rectangle: 4
Area of the rectangle: 8
Enter the length of the triangle: 4
Enter the height of the triangle: 6
Area of the triangle: 12
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.
