Interfaces are a powerful feature in object-oriented programming that define a contract for classes to implement.
To run the program, compile it and then execute:
In C++, interfaces are typically implemented using abstract base classes with pure virtual functions. The Geometry class serves as our interface, defining the contract that all geometric shapes must fulfill.
The Rect and Circle classes inherit from Geometry and provide concrete implementations of the area() and perim() methods.
The measure function demonstrates polymorphism: it can work with any object that implements the Geometry interface, allowing for flexible and extensible code.
Note that C++ doesn’t have a built-in interface keyword like some other languages. Instead, we use abstract classes to achieve similar functionality. Also, C++ uses virtual for method overriding and requires override keyword for clarity in derived classes.
To learn more about C++ interfaces and abstract classes, check out this cppreference page.