Interfaces in C++

Interfaces in C++

In the C++ programming language, class interfaces are a design pattern for user-defined classes that define a set of pure virtual functions without providing any implementation in a base class. A custom interface defined in a class enforces a specific behavior contract, requiring existing classes to implement certain custom class function methods while hiding implementation details. User-defined interfaces enforce class consistency and enhance the reuse of existing program source code and the C++ concept of polymorphism.

Interfaces in C++

In traditional C++ programming, there is no dedicated class interface keyword defined in C++, unlike some other popular programming languages, such as Java or C#. As such, C++ users can implement class interfaces by implementing abstract classes that define only pure virtual functions. These act as an abstract class contract or interface to the existing class, which must be implemented by the derived class or subclass.

Important features of the interface concept in C++ programming.

  • Pure virtual functions – A user-defined class has only one pure virtual function declared in an interface. For example, functions declared in the existing class but not defined in the class interface can be implemented by the derived class or subclass instead of the base class.
  • No data members – Typically, interfaces defined in C++ programs do not contain class data members. Technically, they can have non-static class data members defined, but this is avoided in typical class interface design.
  • No constructor or destructor – Because an interface defined in a class is not instantiated, it normally does not create a constructor. (Except for the virtual destructor method required for the C++ polymorphism concept.)
  • Multiple inheritance – As C++ programming supports the multiple inheritance concept, a class can implement multiple interfaces by inheriting from multiple abstract classes.

Creating an Interface in C++ Programming.

In a user-defined C++ program, an interface is typically an abstract class method with a pure virtual function.

So, let’s look at a detailed example of defining and implementing an interface in a class in C++ programming.

Detailed example of interface implementation in C++.

#include <iostream>

using namespace std;

// here we define a class Interface with Diagram

class Diagram {

public:

virtual void draw() const = 0;

virtual double area() const = 0;

virtual ~Diagram() {}

};

// here we create a class Interface with Previewable

class Previewable {

Public:

virtual void preview() const = 0;

virtual ~Previewable() {}

};

// here we create Concrete class: Triangle implementing Diagram and Previewable

class Triangle : public Diagram, public Previewable {

private:

double base, height;

Public:

Triangle(double b, double h) : base(b), height(h) {}

// here we are implementing Diagram class interface

void draw() const override {

cout << “Now we Draw a Triangle with base – ” << base

<< ” and its height is – ” << height << endl;

}

double area() const override {

return 0.10 * base * height;

}

//here we are Implementing the Previewable interface

void preview() const override {

cout << “The Triangle’s area is – ” << area() << endl;

}

};

// here we create a Concrete class: Rectangle implementing Diagram and Previewable method

class Rectangle : public Diagram, public Previewable {

private:

double length, width;

Public:

Rectangle(double l, double w) : length(l), width(w) {}

void draw() const override {

cout << “Now we Drawing a Rectangle with length – ” << length

<< ” and its width is – ” << width << endl;

}

double area() const override {

return length * width;

}

void preview() const override {

cout << “The Rectangle’s area – ” << area() << endl;

}

};

int main() {

Diagram* diagramfirst = new Triangle(12.0, 7.0);

Diagram* diagramsecond = new Rectangle(7.0, 5.0);

// here we define Polymorphic class behaviour

diagramfirst->draw();

diagramsecond->draw();

Previewable * Previewable1 = dynamic_cast<Previewable*>(diagramfirst);

Previewable * Previewable2 = dynamic_cast<Previewable*>(diagramsecond);

if(Previewable1)

Previewable1->preview();

if(Previewable2)

Previewable2->preview();

delete diagramfirst;

delete diagramsecond;

return 0;

}

Explanation of interface implementation in C++.

Interface Class Method.

  • In this example, the Diagram class is a user-defined abstract base class method. It declares two pure virtual functions (draw() and area()) with a value of 0. This creates an interface for the class.
  • It defines a class named Previewable and a class interface, which declares a pure virtual function named preview().

Concrete Class Method.

  • In this example, Triangle and Rectangle are concrete class methods that implement the Diagram and Previewable class interfaces.
  • Where both classes provide concrete method implementations for the draw(), area(), and preview() class functions.

Polymorphism Class Method.

  • C++ uses polymorphism through base class pointers to reference objects of user-derived classes. In which the draw() function is called polymorphically.
  • C++ users use the dynamic_cast method in the current expression to check whether the base class pointer can be safely cast to a Printable pointer, so that we can call the preview() function when needed.

Multiple Class Inheritance Concept.

  • In this, the Triangle and Rectangle classes inherit from both the Diagram and Previewable class interfaces. This is an example of the multiple class inheritance concept in a C++ program, which allows a user-defined class to implement multiple class interfaces.

Leave a Reply