Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 99 additions & 50 deletions c++/one.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,140 @@
#include <vector>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <optional>
#include <cmath>

// Advanced template metaprogramming challenge
template <typename T, typename = void>
class SmartContainer {
private:
std::vector<T> data;

public:
// Intentionally complex insertion with type constraints
template <typename U = T,
typename = std::enable_if_t<std::is_arithmetic_v<U>>>
void insert(U value) {
data.push_back(static_cast<T>(value));
}

// Constrained retrieval method
std::optional<T> 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<double, double> 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<double, double> 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<double, double> 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, 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;
}

double get_perimeter() const override {
return base + 2 * sqrt((height * height) + (base / 2) * (base / 2));
}

std::pair<double, double> 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, 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 M_PI * major_axis * minor_axis;
}

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::pair<double, double> 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;
}
};

// Main function demonstrating complex interactions
int main() {
// Template container with type restrictions
SmartContainer<int> int_container;
int_container.insert(10);
int_container.insert(20);

// Polymorphic shape management
std::vector<std::unique_ptr<Shape>> shapes;

try {
shapes.push_back(std::make_unique<Circle>(5.0));
shapes.push_back(std::make_unique<Rectangle>(4.0, 6.0));
shapes.push_back(std::make_unique<Triangle>(3.0, 5.0));
shapes.push_back(std::make_unique<Ellipse>(4.0, 2.0));

for (const auto& shape : shapes) {
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;
}