56 [[nodiscard]]
virtual double area()
const = 0;
68 [[nodiscard]]
virtual std::string
name()
const = 0;
75 return name() +
"(area=" + std::to_string(
area())
76 +
", perimeter=" + std::to_string(
perimeter()) +
")";
90 static inline int instance_count_ = 0;
112 [[nodiscard]]
double area()
const override {
113 return M_PI * radius_ * radius_;
117 return 2.0 * M_PI * radius_;
120 [[nodiscard]] std::string
name()
const override {
return "Circle"; }
123 [[nodiscard]]
double radius()
const {
return radius_; }
147 [[nodiscard]]
double area()
const override {
155 [[nodiscard]] std::string
name()
const override {
return "Rectangle"; }
185 [[nodiscard]] std::string
name()
const override {
return "Square"; }
211 Triangle(
double a,
double b,
double c) : a_(a), b_(b), c_(c) {}
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_));
222 [[nodiscard]] std::string
name()
const override {
return "Triangle"; }
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));
double area() const override
Compute the area of the shape.
std::string name() const override
Return the human-readable name of the shape.
Circle(double radius)
Construct a circle with the given radius.
double perimeter() const override
Compute the perimeter of the shape.
double radius() const
Get the radius.
Rectangle(double width, double height)
Construct a rectangle.
double perimeter() const override
Compute the perimeter of the shape.
double height() const
Get the height.
std::string name() const override
Return the human-readable name of the shape.
double width() const
Get the width.
double height_
The rectangle's height.
double area() const override
Compute the area of the shape.
double width_
The rectangle's width.
virtual std::string name() const =0
Return the human-readable name of the shape.
std::string describe() const
Pretty-print shape information.
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.
static int instance_count()
Get the total count of Shape instances created.
virtual double area() const =0
Compute the area of the shape.
std::string name() const override
Return the human-readable name of the shape.
double side() const
Get the side length.
Square(double side)
Construct a square with the given side length.
std::string name() const override
Return the human-readable name of the shape.
double perimeter() const override
Compute the perimeter of the shape.
Triangle(double a, double b, double c)
Construct a triangle from three side lengths.
double area() const override
Compute the area of the shape.
std::vector< std::unique_ptr< Shape > > make_demo_shapes()
Create a container of example shapes for demonstration.