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 67
|
#include <iostream>
#include <vector>
#include <memory>
#include <numeric>
#include <cmath>
//#include <string>
class Shape {
protected:
//std::string name;
int width {}, height {}, radius {};
public:
Shape(int w, int h) : width(w), height(h) {}
Shape(int r) : radius(r) {}
virtual ~Shape() {}
void set_data(int a, int b) {
width = a;
height = b;
}
virtual int getarea() const = 0;
};
class Rectangle : public Shape {
public:
Rectangle(int w, int h) : Shape(w, h) {}
int getarea() const override { return width * height; }
};
class Triangle : public Shape {
public:
Triangle(int w, int h) : Shape(w, h) {}
int getarea() const override { return (width * height) / 2; }
};
class Circle : public Shape {
public:
Circle(int r) : Shape(r) {}
int getarea() const override { return std::round(3.1415 * (radius * radius)); }
};
int main()
{
int rectHeight {}, rectWidth {};
int triaHeight {}, triaWidth {};
int circRadius {};
std::cout << "Enter rect height, rect width, tri height, tri width, circ rad: ";
std::cin >> rectHeight >> rectWidth >> triaHeight >> triaWidth >> circRadius;
std::vector<std::unique_ptr<Shape>> shapes;
shapes.emplace_back(std::make_unique<Rectangle>(rectHeight, rectWidth));
shapes.emplace_back(std::make_unique<Triangle>(triaHeight, triaWidth));
shapes.emplace_back(std::make_unique<Circle>(circRadius));
const auto totalArea {std::accumulate(shapes.begin(), shapes.end(), 0, [](auto total, const auto& shape)
{ return total + shape->getarea(); })};
std::cout << totalArea << '\n';
}
| |