00 //===================================================================
01 // Name        : visitors.h
02 // Author      : Simonas Saltenis
03 // Version     :
04 // Description : Different implementations of visitors for the shape hierarchy
05 //===================================================================
06 
07 #ifndef VISITORS_H_INCLUDED
08 #define VISITORS_H_INCLUDED
09 
10 #include <iostream>
11 #include <cmath>
12 #include "shapes.h"
13 
14 class AreaCalculator: public Visitor
15 {
16 public:
17     void visit (Circle& c) override    { std::cout << "Circle area: " << M_PI * c.r * c.r << '\n'; }
18     void visit (Rectangle& r) override { std::cout << "Rectangle area: " << r.l * r.w << '\n'; }
19 };
20 
21 class CircumferenceCalculator: public Visitor
22 {
23 public:
24     void visit (Circle& c) override    { std::cout << "Circle length: " << 2*M_PI * c.r << '\n'; }
25     void visit (Rectangle& r) override { std::cout << "Rectangle circumference: " << 2*(r.l + r.w) << '\n'; }
26 };
27 
28 #endif // VISITORS_H_INCLUDED
29