1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <iostream> #include <memory> #include <new> #include <vector> #include <cmath>
class Vector2D { public: double x, y;
Vector2D(double x = 0.0, double y = 0.0) : x(x), y(y) {} };
class Wall { protected: Vector2D start, end; int elevation, height; public: friend class WallFactory; Wall(Vector2D start, Vector2D end, int elevation, int height) : start{start}, end{end}, elevation{elevation}, height {height} { } friend std::ostream& operator<<(std::ostream& os, const Wall& obj) { return os << "start: " << "(" << obj.start.x << ")" << " " << "(" << obj.start.y << ")" << std::endl << "end: " << "(" << obj.end.x << ")" << " " << "(" << obj.end.y << ")" << std::endl << "elevation: " << obj.elevation << std::endl << "height: " << obj.height << std::endl; } virtual ~Wall() = default; };
enum class Material { brick, aerated_concrete, drywall };
std::string Material_backpack[] = {"brick", "aerated_concrete", "drywall"};
class SolidWall : public Wall { int width; Material material; protected: SolidWall(Vector2D start, Vector2D end, int elevation, int height, int width, Material material) : Wall{start, end, elevation, height}, width{width}, material{material} {} public: friend class WallFactory;
friend std::ostream& operator<<(std::ostream& os, const SolidWall& obj) { return os << "start: " << "(" << obj.start.x << ")" << " " << "(" << obj.start.y << ")" << std::endl << "end: " << "(" << obj.end.x << ")" << " " << "(" << obj.end.y << ")" << std::endl << "elevation: " << obj.elevation << std::endl << "height: " << obj.height << std::endl << "width: " << obj.width << std::endl << "material:" << Material_backpack[static_cast<int>(obj.material)] << std::endl; } };
enum class WallType { basic, main, partition };
class WallFactory {
public: static std::shared_ptr<SolidWall> create_main(Vector2D start, Vector2D end, int elevation, int height) { if (elevation < 0) return {}; return std::make_shared<SolidWall>(SolidWall(start, end, elevation, height, 375, Material::aerated_concrete)); }
static std::shared_ptr<Wall> create_wall(WallType type, Vector2D start, Vector2D end, int elevation, int height) { switch(type) { case WallType::main: return std::make_shared<SolidWall>(SolidWall(start, end, elevation, height, 375, Material::aerated_concrete)); case WallType::partition: return std::make_shared<SolidWall>(SolidWall(start, end, elevation, height, 120, Material::brick)); case WallType::basic: return std::make_shared<Wall>(Wall(start, end, elevation, height)); } } };
int main(void) {
const auto also_partition = WallFactory::create_wall(WallType::partition, {0, 0}, {5000, 0}, 0, 4200); if (also_partition) std::cout << *std::dynamic_pointer_cast<SolidWall>(also_partition) << std::endl; return 0; }
|