Oregami
Repositories/oxedyne/fe2o3

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
6class Shape {
7public:
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
17class Circle : public Shape {
18public:
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
27private:
28 double radius_;
29};
30
31class Rectangle : public Shape {
32public:
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
41private:
42 double width_;
43 double height_;
44};
45
46class Triangle : public Shape {
47public:
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
56private:
57 double base_;
58 double height_;
59};
60
61int 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}