-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes.h
More file actions
56 lines (40 loc) · 1.54 KB
/
shapes.h
File metadata and controls
56 lines (40 loc) · 1.54 KB
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
#pragma once
#include <numbers>
#include <variant>
template <typename Numeric> struct IShape {
virtual ~IShape() = default;
virtual auto Area() const -> Numeric = 0;
};
template <typename Numeric, typename Derived> struct AbstractShape : IShape<Numeric> {
[[nodiscard]] auto Area() const -> Numeric override { return impl()->compute_area(); }
private:
[[nodiscard]] auto impl() const -> const Derived * { return static_cast<const Derived *>(this); }
};
template <typename Numeric> class Rectangle : public AbstractShape<Numeric, Rectangle<Numeric>> {
public:
constexpr Rectangle(Numeric len, Numeric breth) : len_(len), breth_(breth) {}
friend struct AbstractShape<Numeric, Rectangle>;
private:
[[nodiscard]] constexpr auto compute_area() const -> Numeric { return len_ * breth_; }
Numeric len_;
Numeric breth_;
};
template <typename Numeric> class Circle : public AbstractShape<Numeric, Circle<Numeric>> {
public:
constexpr explicit Circle(Numeric radius) : radius_(radius) {}
friend struct AbstractShape<Numeric, Circle>;
private:
[[nodiscard]] constexpr auto compute_area() const -> Numeric {
return 2 * radius_ * std::numbers::pi;
}
Numeric radius_;
};
template <typename Numeric> class Triangle : public AbstractShape<Numeric, Triangle<Numeric>> {
public:
constexpr Triangle(Numeric base, Numeric heit) : base_(base), heit_(heit) {}
friend struct AbstractShape<Numeric, Triangle>;
private:
[[nodiscard]] constexpr auto compute_area() const -> Numeric { return (base_ * heit_) / 2; }
Numeric base_;
Numeric heit_;
};