Repository navigation
Expand file tree
/
Copy pathabstract_classes.cpp
More file actions
107 lines (90 loc) · 3.56 KB
/
Copy pathabstract_classes.cpp
File metadata and controls
107 lines (90 loc) · 3.56 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Abstraction: expose WHAT an object can do and hide HOW it does it.
// An abstract class has at least one pure virtual function (= 0). It cannot be
// instantiated; every concrete subclass must implement those functions.
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
constexpr double PI = 3.14159265358979323846;
// An interface: nothing but pure virtual functions
class Printable {
public:
virtual ~Printable() = default;
virtual void print() const = 0;
};
// An abstract base class: defines the contract every shape must follow
class Shape : public Printable {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual std::string name() const = 0;
// A concrete function written only in terms of the abstract ones
void print() const override {
std::cout << " " << std::left << std::setw(10) << name() << std::right << std::fixed << std::setprecision(2)
<< "area " << std::setw(8) << area() << " perimeter " << std::setw(7) << perimeter() << '\n';
}
};
class Circle : public Shape {
double r;
public:
explicit Circle(double radius) : r(radius) {}
double area() const override { return PI * r * r; }
double perimeter() const override { return 2 * PI * r; }
std::string name() const override { return "Circle"; }
};
class Rectangle : public Shape {
protected:
double w, h;
public:
Rectangle(double width, double height) : w(width), h(height) {}
double area() const override { return w * h; }
double perimeter() const override { return 2 * (w + h); }
std::string name() const override { return "Rectangle"; }
};
class Square : public Rectangle {
public:
explicit Square(double side) : Rectangle(side, side) {}
std::string name() const override { return "Square"; } // reuses area() and perimeter()
};
class Triangle : public Shape {
double a, b, c;
public:
Triangle(double a, double b, double c) : a(a), b(b), c(c) {
if (a + b <= c || a + c <= b || b + c <= a) throw std::invalid_argument("sides cannot form a triangle");
}
double area() const override { // Heron's formula
double s = perimeter() / 2;
return std::sqrt(s * (s - a) * (s - b) * (s - c));
}
double perimeter() const override { return a + b + c; }
std::string name() const override { return "Triangle"; }
};
int main() {
// Shape s; // compile error: cannot create an object of an abstract class
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Circle>(3));
shapes.push_back(std::make_unique<Rectangle>(4, 6));
shapes.push_back(std::make_unique<Square>(5));
shapes.push_back(std::make_unique<Triangle>(3, 4, 5));
std::cout << "All shapes, used only through the abstract Shape interface:\n";
for (const auto& s : shapes) s->print();
std::sort(shapes.begin(), shapes.end(),
[](const std::unique_ptr<Shape>& x, const std::unique_ptr<Shape>& y) { return x->area() < y->area(); });
std::cout << "\nSorted by area:\n";
for (const auto& s : shapes) s->print();
double total = 0;
for (const auto& s : shapes) total += s->area();
std::cout << "\nTotal area: " << total << '\n';
std::cout << "Largest shape: " << shapes.back()->name() << '\n';
std::cout << "\nThe constructor rejects impossible shapes:\n";
try {
Triangle bad(1, 2, 10);
} catch (const std::invalid_argument& e) {
std::cout << " Error: " << e.what() << '\n';
}
return 0;
}