From b878e5970f0095562617549ac9cd78d9f3a46478 Mon Sep 17 00:00:00 2001 From: Prashant-thakur77 Date: Thu, 27 Mar 2025 02:09:47 +0530 Subject: [PATCH 1/2] added triangle and allipse --- c++/one.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/c++/one.cpp b/c++/one.cpp index 67394da..b69f51b 100644 --- a/c++/one.cpp +++ b/c++/one.cpp @@ -71,22 +71,74 @@ class Rectangle : public Shape { } }; -// Main function demonstrating complex interactions +class Triangle : public Shape { +private: + double base; + double height; + +public: + Triangle(double b, double h) : base(b), height(h) { + if (b <= 0 || h <= 0) { + throw std::invalid_argument("Base and height must be positive"); + } + } + + double get_area() const override { + return 0.5 * base * height; + } +}; + +class Ellipse : public Shape { +private: + double major_axis; + double minor_axis; + +public: + Ellipse(double a, double b) : major_axis(a), minor_axis(b) { + if (a <= 0 || b <= 0) { + throw std::invalid_argument("Axes must be positive"); + } + } + + double get_area() const override { + return 3.14159 * major_axis * minor_axis; + } +}; + int main() { // Template container with type restrictions SmartContainer int_container; int_container.insert(10); int_container.insert(20); - + + // Print stored values + std::cout << "Stored numbers: "; + for (size_t i = 0; i < 2; ++i) { + auto value = int_container.get(i); + if (value) { + std::cout << *value << " "; + } + } + std::cout << std::endl; + // Polymorphic shape management std::vector> shapes; - + try { shapes.push_back(std::make_unique(5.0)); shapes.push_back(std::make_unique(4.0, 6.0)); + shapes.push_back(std::make_unique(3.0, 5.0)); + shapes.push_back(std::make_unique(4.0, 2.0)); + + // Print shape areas + std::cout << "Shape areas:\n"; + for (const auto& shape : shapes) { + std::cout << shape->get_area() << std::endl; + } } catch (const std::invalid_argument& e) { std::cerr << "Invalid shape: " << e.what() << std::endl; } - + return 0; -} \ No newline at end of file +} + From 0609dde61247a0b3a339baa1fbae530362339ace Mon Sep 17 00:00:00 2001 From: Prashant-thakur77 Date: Thu, 27 Mar 2025 02:38:36 +0530 Subject: [PATCH 2/2] added advance geometric calculation such as finding moment of inertia,centroid and perimeter --- c++/one.cpp | 155 ++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 79 deletions(-) diff --git a/c++/one.cpp b/c++/one.cpp index b69f51b..37cc687 100644 --- a/c++/one.cpp +++ b/c++/one.cpp @@ -2,143 +2,140 @@ #include #include #include -#include #include +#include -// Advanced template metaprogramming challenge -template -class SmartContainer { -private: - std::vector data; - -public: - // Intentionally complex insertion with type constraints - template >> - void insert(U value) { - data.push_back(static_cast(value)); - } - - // Constrained retrieval method - std::optional get(size_t index) const { - if (index < data.size()) { - return data[index]; - } - return std::nullopt; - } - - // Placeholder for future advanced operations - void advanced_transform(); -}; - -// Base class for a polymorphic type hierarchy +// Base class for shape calculations class Shape { public: virtual double get_area() const = 0; + virtual double get_perimeter() const = 0; + virtual std::pair get_centroid() const = 0; + virtual double get_moment_of_inertia() const = 0; virtual ~Shape() = default; }; class Circle : public Shape { private: double radius; - + public: explicit Circle(double r) : radius(r) { - if (r <= 0) { - throw std::invalid_argument("Radius must be positive"); - } + if (r <= 0) throw std::invalid_argument("Radius must be positive"); } - + double get_area() const override { - return 3.14159 * radius * radius; + return M_PI * radius * radius; + } + + double get_perimeter() const override { + return 2 * M_PI * radius; + } + + std::pair get_centroid() const override { + return {0.0, 0.0}; + } + + double get_moment_of_inertia() const override { + return (M_PI * radius * radius * radius * radius) / 4.0; } }; class Rectangle : public Shape { private: - double width; - double height; - + double width, height; + public: Rectangle(double w, double h) : width(w), height(h) { - if (w <= 0 || h <= 0) { - throw std::invalid_argument("Dimensions must be positive"); - } + if (w <= 0 || h <= 0) throw std::invalid_argument("Dimensions must be positive"); } - + double get_area() const override { return width * height; } + + double get_perimeter() const override { + return 2 * (width + height); + } + + std::pair get_centroid() const override { + return {width / 2.0, height / 2.0}; + } + + double get_moment_of_inertia() const override { + return (width * height * height * height) / 12.0; + } }; class Triangle : public Shape { private: - double base; - double height; - + double base, height; + public: Triangle(double b, double h) : base(b), height(h) { - if (b <= 0 || h <= 0) { - throw std::invalid_argument("Base and height must be positive"); - } + if (b <= 0 || h <= 0) throw std::invalid_argument("Base and height must be positive"); } - + double get_area() const override { return 0.5 * base * height; } + + double get_perimeter() const override { + return base + 2 * sqrt((height * height) + (base / 2) * (base / 2)); + } + + std::pair get_centroid() const override { + return {base / 3.0, height / 3.0}; + } + + double get_moment_of_inertia() const override { + return (base * height * height * height) / 36.0; + } }; class Ellipse : public Shape { private: - double major_axis; - double minor_axis; - + double major_axis, minor_axis; + public: Ellipse(double a, double b) : major_axis(a), minor_axis(b) { - if (a <= 0 || b <= 0) { - throw std::invalid_argument("Axes must be positive"); - } + if (a <= 0 || b <= 0) throw std::invalid_argument("Axes must be positive"); } - + double get_area() const override { - return 3.14159 * major_axis * minor_axis; + return M_PI * major_axis * minor_axis; } -}; -int main() { - // Template container with type restrictions - SmartContainer int_container; - int_container.insert(10); - int_container.insert(20); - - // Print stored values - std::cout << "Stored numbers: "; - for (size_t i = 0; i < 2; ++i) { - auto value = int_container.get(i); - if (value) { - std::cout << *value << " "; - } + double get_perimeter() const override { + return M_PI * (3 * (major_axis + minor_axis) - sqrt((3 * major_axis + minor_axis) * (major_axis + 3 * minor_axis))); } - std::cout << std::endl; - // Polymorphic shape management - std::vector> shapes; + std::pair get_centroid() const override { + return {0.0, 0.0}; + } + + double get_moment_of_inertia() const override { + return (M_PI * major_axis * minor_axis * minor_axis * minor_axis) / 4.0; + } +}; +int main() { + std::vector> shapes; try { shapes.push_back(std::make_unique(5.0)); shapes.push_back(std::make_unique(4.0, 6.0)); shapes.push_back(std::make_unique(3.0, 5.0)); shapes.push_back(std::make_unique(4.0, 2.0)); - // Print shape areas - std::cout << "Shape areas:\n"; for (const auto& shape : shapes) { - std::cout << shape->get_area() << std::endl; + std::cout << "Area: " << shape->get_area() << "\n"; + std::cout << "Perimeter: " << shape->get_perimeter() << "\n"; + auto [cx, cy] = shape->get_centroid(); + std::cout << "Centroid: (" << cx << ", " << cy << ")\n"; + std::cout << "Moment of Inertia: " << shape->get_moment_of_inertia() << "\n\n"; } } catch (const std::invalid_argument& e) { std::cerr << "Invalid shape: " << e.what() << std::endl; } - return 0; -} - +} \ No newline at end of file