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 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <cmath>
#include <memory>
#include <string>
#include <vector>
using namespace std::literals;
const double Pi = atan2(0, -1);
class Shape
{
public:
virtual double area() = 0;
virtual std::string name() = 0;
virtual ~Shape() = default;
};
class Rectangle: public Shape
{
public:
Rectangle(double x_, double y_) : x(x_), y(y_) {}
virtual double area() {return x*y;};
bool is_square() {return x == y;}
virtual std::string name() {return is_square() ? "Square"s : "Rectangle"s;}
private:
double x;
double y;
};
class Ellipse: public Shape
{
public:
Ellipse(double x_, double y_) : r1(x_), r2(y_) {}
virtual double area() {return r1*r2*Pi;}
bool is_circle() {return r1 == r2;}
virtual std::string name() {return is_circle() ? "Circle"s : "Ellipse"s;};
private:
double r1;
double r2;
};
std::unique_ptr<Shape> create_rectangle(double x, double y)
{ return std::make_unique<Rectangle>(x, y); }
std::unique_ptr<Shape> create_square(double side)
{ return std::make_unique<Rectangle>(side, side); }
std::unique_ptr<Shape> create_ellipse(double x, double y)
{ return std::make_unique<Ellipse>(x, y); }
std::unique_ptr<Shape> create_circle(double r)
{ return std::make_unique<Ellipse>(r, r); }
int main()
{
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(create_rectangle(20, 10));
shapes.push_back(create_square(15));
shapes.push_back(create_ellipse(12, 14));
shapes.push_back(create_circle(10));
auto end = shapes.size();
for(decltype(end) i = 0; i < end; ++i)
std::cout << "Shape " << i << " is a " << shapes[i]->name() <<
" and its area is " << shapes[i]->area() << '\n';
}
|
Shape 0 is a Rectangle and its area is 200
Shape 1 is a Square and its area is 225
Shape 2 is a Ellipse and its area is 527.788
Shape 3 is a Circle and its area is 314.159 |