diff --git a/c++/one.cpp b/c++/one.cpp index 766e807..d3eaddd 100644 --- a/c++/one.cpp +++ b/c++/one.cpp @@ -7,45 +7,54 @@ #include #include +#define PI 3.14159 // Advanced template metaprogramming challenge template -class SmartContainer { +class SmartContainer +{ private: std::vector data; - + public: // Intentionally complex insertion with type constraints - template >> - void insert(U value) { + void insert(U value) + { data.push_back(static_cast(value)); } - + // Constrained retrieval method - std::optional get(size_t index) const { - if (index < data.size()) { + std::optional get(size_t index) const + { + if (index < data.size()) + { return data[index]; } return std::nullopt; } - + // Flexible transformation method template >> - void advanced_transform(Func func) { + void advanced_transform(Func func) + { std::transform(data.begin(), data.end(), data.begin(), func); } // Type-safe filtering method template >> - SmartContainer filter(Predicate pred) const { + SmartContainer filter(Predicate pred) const + { SmartContainer result; std::copy_if(data.begin(), data.end(), std::back_inserter(result.data), pred); return result; } // Print method for demonstration - void print() const { - for (const auto& item : data) { + void print() const + { + for (const auto &item : data) + { std::cout << item << " "; } std::cout << std::endl; @@ -53,70 +62,86 @@ class SmartContainer { }; // Base class for a polymorphic type hierarchy -class Shape { +class Shape +{ public: virtual double get_area() const = 0; virtual ~Shape() = default; }; -class Circle : public Shape { +class Circle : public Shape +{ private: double radius; - + public: - explicit Circle(double r) : radius(r) { - if (r <= 0) { + explicit Circle(double r) : radius(r) + { + if (r <= 0) + { throw std::invalid_argument("Radius must be positive"); } } - - double get_area() const override { - return 3.14159 * radius * radius; + + double get_area() const override + { + return PI * radius * radius; } }; -class Rectangle : public Shape { +// Testing commits +class Rectangle : public Shape +{ private: double width; double height; - + public: - Rectangle(double w, double h) : width(w), height(h) { - if (w <= 0 || h <= 0) { + Rectangle(double w, double h) : width(w), height(h) + { + if (w <= 0 || h <= 0) + { throw std::invalid_argument("Dimensions must be positive"); } } - - double get_area() const override { + + double get_area() const override + { return width * height; } }; // Main function demonstrating complex interactions -int main() { +int main() +{ // Template container with type restrictions SmartContainer int_container; int_container.insert(10); int_container.insert(20); int_container.insert(30); - + // Apply a transformation: multiply each element by 2 - int_container.advanced_transform([](int x) { return x * 2; }); + int_container.advanced_transform([](int x) + { return x * 2; }); int_container.print(); // Output: 20 40 60 - + // Filter elements greater than 25 - auto filtered_container = int_container.filter([](int x) { return x > 25; }); + auto filtered_container = int_container.filter([](int x) + { return x > 25; }); filtered_container.print(); // Output: 40 60 - + // Polymorphic shape management std::vector> shapes; - - try { + + try + { shapes.push_back(std::make_unique(5.0)); shapes.push_back(std::make_unique(4.0, 6.0)); - } catch (const std::invalid_argument& e) { + } + catch (const std::invalid_argument &e) + { std::cerr << "Invalid shape: " << e.what() << std::endl; } - + return 0; } \ No newline at end of file