oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/cpp/inheritance.txt
1.6 KiB, 1 run
created by r1870400018:11946, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <vector> |
| 4 | #include <memory> |
| 5 | |
| 6 | class Shape { |
| 7 | public: |
| 8 | virtual ~Shape() = default; |
| 9 | virtual double area() const = 0; |
| 10 | virtual std::string name() const = 0; |
| 11 | |
| 12 | void describe() const { |
| 13 | std::cout << name() << ": area = " << area() << "\n"; |
| 14 | } |
| 15 | }; |
| 16 | |
| 17 | class Circle : public Shape { |
| 18 | public: |
| 19 | explicit Circle(double radius) : radius_(radius) {} |
| 20 | |
| 21 | double area() const override { |
| 22 | return 3.14159265 * radius_ * radius_; |
| 23 | } |
| 24 | |
| 25 | std::string name() const override { return "Circle"; } |
| 26 | |
| 27 | private: |
| 28 | double radius_; |
| 29 | }; |
| 30 | |
| 31 | class Rectangle : public Shape { |
| 32 | public: |
| 33 | Rectangle(double w, double h) : width_(w), height_(h) {} |
| 34 | |
| 35 | double area() const override { |
| 36 | return width_ * height_; |
| 37 | } |
| 38 | |
| 39 | std::string name() const override { return "Rectangle"; } |
| 40 | |
| 41 | private: |
| 42 | double width_; |
| 43 | double height_; |
| 44 | }; |
| 45 | |
| 46 | class Triangle : public Shape { |
| 47 | public: |
| 48 | Triangle(double base, double height) : base_(base), height_(height) {} |
| 49 | |
| 50 | double area() const override { |
| 51 | return 0.5 * base_ * height_; |
| 52 | } |
| 53 | |
| 54 | std::string name() const override { return "Triangle"; } |
| 55 | |
| 56 | private: |
| 57 | double base_; |
| 58 | double height_; |
| 59 | }; |
| 60 | |
| 61 | int main() { |
| 62 | std::vector<std::unique_ptr<Shape>> shapes; |
| 63 | shapes.push_back(std::make_unique<Circle>(5.0)); |
| 64 | shapes.push_back(std::make_unique<Rectangle>(4.0, 6.0)); |
| 65 | shapes.push_back(std::make_unique<Triangle>(3.0, 8.0)); |
| 66 | |
| 67 | double total = 0.0; |
| 68 | for (const auto& shape : shapes) { |
| 69 | shape->describe(); |
| 70 | total += shape->area(); |
| 71 | } |
| 72 | std::cout << "total area: " << total << "\n"; |
| 73 | |
| 74 | return 0; |
| 75 | } |