fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4.  
  5. /** Abstract interface class.
  6.  *
  7.  * This class defines the interface that must be implemented by concrete classes.
  8.  * It contains pure virtual functions that provide the contract for derived classes.
  9.  * The interface is designed to be used polymorphically through pointers or references.
  10.  */
  11. class IShape {
  12. public:
  13. /** Virtual destructor.
  14.   *
  15.   * Ensures proper cleanup when deleting through base class pointer.
  16.   */
  17. virtual ~IShape() = default;
  18.  
  19. /** Calculate area of the shape.
  20.   *
  21.   * Pure virtual function that must be implemented by derived classes.
  22.   * @return The area of the shape as a double.
  23.   */
  24. virtual double area() const = 0;
  25.  
  26. /** Calculate perimeter of the shape.
  27.   *
  28.   * Pure virtual function that must be implemented by derived classes.
  29.   * @return The perimeter of the shape as a double.
  30.   */
  31. virtual double perimeter() const = 0;
  32.  
  33. /** Get shape name.
  34.   *
  35.   * Pure virtual function for getting the name of the shape.
  36.   * @return The name of the shape as a C-style string.
  37.   */
  38. virtual const char * name() const = 0;
  39.  
  40. /** Draw the shape.
  41.   *
  42.   * Virtual function with default implementation that can be overridden.
  43.   * Provides a basic drawing mechanism that derived classes can customize.
  44.   */
  45. virtual void draw() const {
  46. std::cout << "Drawing a " << name() << std::endl;
  47. }
  48. };
  49.  
  50. /** Concrete implementation of circle shape.
  51.  *
  52.  * This class implements the IShape interface and provides specific
  53.  * implementations for circle geometry calculations.
  54.  */
  55. class Circle : public IShape {
  56. private:
  57. double radius; /*!< Radius of the circle */
  58.  
  59. public:
  60. /** Circle constructor.
  61.   *
  62.   * @param r The radius of the circle.
  63.   */
  64. explicit Circle(double r) : radius(r) {}
  65.  
  66. /** Calculate circle area.
  67.   *
  68.   * @return The area of the circle (πr²).
  69.   */
  70. double area() const override {
  71. return 3.14159 * radius * radius;
  72. }
  73.  
  74. /** Calculate circle perimeter.
  75.   *
  76.   * @return The perimeter of the circle (2πr).
  77.   */
  78. double perimeter() const override {
  79. return 2 * 3.14159 * radius;
  80. }
  81.  
  82. /** Get shape name.
  83.   *
  84.   * @return "Circle" as the shape name.
  85.   */
  86. const char * name() const override {
  87. return "Circle";
  88. }
  89.  
  90. /** Draw the circle.
  91.   *
  92.   * Custom implementation of draw for circles.
  93.   */
  94. void draw() const override {
  95. std::cout << "Drawing a circle with radius " << radius << std::endl;
  96. }
  97. };
  98.  
  99. /** Concrete implementation of rectangle shape.
  100.  *
  101.  * This class implements the IShape interface and provides specific
  102.  * implementations for rectangle geometry calculations.
  103.  */
  104. class Rectangle : public IShape {
  105. private:
  106. double width; /*!< Width of the rectangle */
  107. double height; /*!< Height of the rectangle */
  108.  
  109. public:
  110. /** Rectangle constructor.
  111.   *
  112.   * @param w The width of the rectangle.
  113.   * @param h The height of the rectangle.
  114.   */
  115. Rectangle(double w, double h) : width(w), height(h) {}
  116.  
  117. /** Calculate rectangle area.
  118.   *
  119.   * @return The area of the rectangle (width × height).
  120.   */
  121. double area() const override {
  122. return width * height;
  123. }
  124.  
  125. /** Calculate rectangle perimeter.
  126.   *
  127.   * @return The perimeter of the rectangle (2 × (width + height)).
  128.   */
  129. double perimeter() const override {
  130. return 2 * (width + height);
  131. }
  132.  
  133. /** Get shape name.
  134.   *
  135.   * @return "Rectangle" as the shape name.
  136.   */
  137. const char * name() const override {
  138. return "Rectangle";
  139. }
  140.  
  141. /** Draw the rectangle.
  142.   *
  143.   * Custom implementation of draw for rectangles.
  144.   */
  145. void draw() const override {
  146. std::cout << "Drawing a rectangle " << width << "x" << height << std::endl;
  147. }
  148. };
  149.  
  150. /** Shape processor class.
  151.  *
  152.  * This class demonstrates using the interface polymorphically.
  153.  * It can work with any shape that implements the IShape interface.
  154.  */
  155. class ShapeProcessor {
  156. public:
  157. /** Process a shape.
  158.   *
  159.   * This function takes a shape through the interface and performs
  160.   * operations on it polymorphically.
  161.   *
  162.   * @param shape Pointer to the shape to process.
  163.   */
  164. void processShape(const IShape * shape) {
  165. if (shape) {
  166. std::cout << "Processing " << shape->name() << ":" << std::endl;
  167. std::cout << " Area: " << shape->area() << std::endl;
  168. std::cout << " Perimeter: " << shape->perimeter() << std::endl;
  169. shape->draw();
  170. std::cout << std::endl;
  171. }
  172. }
  173.  
  174. /** Calculate total area of multiple shapes.
  175.   *
  176.   * Demonstrates using the interface with a collection of shapes.
  177.   *
  178.   * @param shapes Vector of shape pointers.
  179.   * @return Total area of all shapes.
  180.   */
  181. double totalArea(const std::vector<const IShape *> & shapes) const {
  182. double total = 0.0;
  183. for (const auto * shape : shapes) {
  184. if (shape) {
  185. total += shape->area();
  186. }
  187. }
  188. return total;
  189. }
  190. };
  191.  
  192. /** Demonstrates shared virtual functions between interface and objects.
  193.  *
  194.  * This function shows how the IShape interface defines virtual functions
  195.  * that are shared (implemented) by concrete classes like Circle and Rectangle.
  196.  * The interface provides the contract, while implementations provide the behavior.
  197.  */
  198. void demonstrateSharedVirtualFunctions() {
  199. std::cout << "=== Shared Virtual Functions Demonstration ===" << std::endl;
  200.  
  201. // Create concrete objects
  202. Circle circle(5.0);
  203. Rectangle rectangle(4.0, 6.0);
  204.  
  205. // Create processor
  206. ShapeProcessor processor;
  207.  
  208. // Process shapes polymorphically through interface
  209. std::cout << "Processing individual shapes:" << std::endl;
  210. processor.processShape(&circle);
  211. processor.processShape(&rectangle);
  212.  
  213. // Demonstrate collection processing
  214. std::vector<const IShape *> shapes = {&circle, &rectangle};
  215. double totalArea = processor.totalArea(shapes);
  216. std::cout << "Total area of all shapes: " << totalArea << std::endl;
  217.  
  218. std::cout << "\n=== Key Points ===" << std::endl;
  219. std::cout << "- IShape interface defines pure virtual functions (contract)" << std::endl;
  220. std::cout << "- Circle and Rectangle implement the interface (shared behavior)" << std::endl;
  221. std::cout << "- ShapeProcessor works with any IShape polymorphically" << std::endl;
  222. std::cout << "- Virtual functions enable runtime polymorphism" << std::endl;
  223. std::cout << "- Interface provides abstraction, implementations provide details" << std::endl;
  224. }
  225.  
  226. int main() {
  227. demonstrateSharedVirtualFunctions();
  228. return 0;
  229. }
  230.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
=== Shared Virtual Functions Demonstration ===
Processing individual shapes:
Processing Circle:
  Area: 78.5397
  Perimeter: 31.4159
Drawing a circle with radius 5

Processing Rectangle:
  Area: 24
  Perimeter: 20
Drawing a rectangle 4x6

Total area of all shapes: 102.54

=== Key Points ===
- IShape interface defines pure virtual functions (contract)
- Circle and Rectangle implement the interface (shared behavior)
- ShapeProcessor works with any IShape polymorphically
- Virtual functions enable runtime polymorphism
- Interface provides abstraction, implementations provide details