Loading...
Searching...
No Matches
shapes.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
14#include <cmath>
15#include <string>
16#include <memory>
17#include <vector>
18
32
33namespace geometry {
34
47class Shape {
48public:
50 virtual ~Shape() = default;
51
56 [[nodiscard]] virtual double area() const = 0;
57
62 [[nodiscard]] virtual double perimeter() const = 0;
63
68 [[nodiscard]] virtual std::string name() const = 0;
69
74 [[nodiscard]] std::string describe() const {
75 return name() + "(area=" + std::to_string(area())
76 + ", perimeter=" + std::to_string(perimeter()) + ")";
77 }
78
83 static int instance_count() { return instance_count_; }
84
85protected:
87 Shape() { ++instance_count_; }
88
89private:
90 static inline int instance_count_ = 0;
91};
92
103class Circle : public Shape {
104public:
110 explicit Circle(double radius) : radius_(radius) {}
111
112 [[nodiscard]] double area() const override {
113 return M_PI * radius_ * radius_;
114 }
115
116 [[nodiscard]] double perimeter() const override {
117 return 2.0 * M_PI * radius_;
118 }
119
120 [[nodiscard]] std::string name() const override { return "Circle"; }
121
123 [[nodiscard]] double radius() const { return radius_; }
124
125private:
126 double radius_;
127};
128
138class Rectangle : public Shape {
139public:
146
147 [[nodiscard]] double area() const override {
148 return width_ * height_;
149 }
150
151 [[nodiscard]] double perimeter() const override {
152 return 2.0 * (width_ + height_);
153 }
154
155 [[nodiscard]] std::string name() const override { return "Rectangle"; }
156
158 [[nodiscard]] double width() const { return width_; }
160 [[nodiscard]] double height() const { return height_; }
161
162protected:
163 double width_;
164 double height_;
165};
166
177class Square : public Rectangle {
178public:
183 explicit Square(double side) : Rectangle(side, side) {}
184
185 [[nodiscard]] std::string name() const override { return "Square"; }
186
188 [[nodiscard]] double side() const { return width_; }
189};
190
202class Triangle : public Shape {
203public:
211 Triangle(double a, double b, double c) : a_(a), b_(b), c_(c) {}
212
213 [[nodiscard]] double area() const override {
214 double s = (a_ + b_ + c_) / 2.0;
215 return std::sqrt(s * (s - a_) * (s - b_) * (s - c_));
216 }
217
218 [[nodiscard]] double perimeter() const override {
219 return a_ + b_ + c_;
220 }
221
222 [[nodiscard]] std::string name() const override { return "Triangle"; }
223
224private:
225 double a_;
226 double b_;
227 double c_;
228};
229
243inline std::vector<std::unique_ptr<Shape>> make_demo_shapes() {
244 std::vector<std::unique_ptr<Shape>> shapes;
245 shapes.push_back(std::make_unique<Circle>(5.0));
246 shapes.push_back(std::make_unique<Rectangle>(4.0, 6.0));
247 shapes.push_back(std::make_unique<Square>(3.0));
248 shapes.push_back(std::make_unique<Triangle>(3.0, 4.0, 5.0));
249 return shapes;
250}
251
252} // namespace geometry
253 // end of shapes group
double area() const override
Compute the area of the shape.
Definition shapes.hpp:112
std::string name() const override
Return the human-readable name of the shape.
Definition shapes.hpp:120
Circle(double radius)
Construct a circle with the given radius.
Definition shapes.hpp:110
double perimeter() const override
Compute the perimeter of the shape.
Definition shapes.hpp:116
double radius() const
Get the radius.
Definition shapes.hpp:123
Rectangle(double width, double height)
Construct a rectangle.
Definition shapes.hpp:145
double perimeter() const override
Compute the perimeter of the shape.
Definition shapes.hpp:151
double height() const
Get the height.
Definition shapes.hpp:160
std::string name() const override
Return the human-readable name of the shape.
Definition shapes.hpp:155
double width() const
Get the width.
Definition shapes.hpp:158
double height_
The rectangle's height.
Definition shapes.hpp:164
double area() const override
Compute the area of the shape.
Definition shapes.hpp:147
double width_
The rectangle's width.
Definition shapes.hpp:163
virtual std::string name() const =0
Return the human-readable name of the shape.
std::string describe() const
Pretty-print shape information.
Definition shapes.hpp:74
virtual double perimeter() const =0
Compute the perimeter of the shape.
virtual ~Shape()=default
Virtual destructor for safe polymorphic deletion.
Shape()
Increment the instance counter.
Definition shapes.hpp:87
static int instance_count()
Get the total count of Shape instances created.
Definition shapes.hpp:83
virtual double area() const =0
Compute the area of the shape.
std::string name() const override
Return the human-readable name of the shape.
Definition shapes.hpp:185
double side() const
Get the side length.
Definition shapes.hpp:188
Square(double side)
Construct a square with the given side length.
Definition shapes.hpp:183
std::string name() const override
Return the human-readable name of the shape.
Definition shapes.hpp:222
double perimeter() const override
Compute the perimeter of the shape.
Definition shapes.hpp:218
Triangle(double a, double b, double c)
Construct a triangle from three side lengths.
Definition shapes.hpp:211
double area() const override
Compute the area of the shape.
Definition shapes.hpp:213
std::vector< std::unique_ptr< Shape > > make_demo_shapes()
Create a container of example shapes for demonstration.
Definition shapes.hpp:243