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
|
#ifndef SHAPE_HPP
#define SHAPE_HPP
#include <cmath>
#include <stdexcept>
#include <cstdio>
#include <sstream>
#include <string>
// abstract class shape
class Shape {
public:
virtual double get_perimeter()=0;
};
class Point {
public:
Point(double x_in, double y_in) : x(x_in), y(y_in) {}
friend std::ostream& operator<<(std::ostream& out, const Point& p) {
std::stringstream ss;
ss << "(" << p.x << ", " << p.y << ")";
out << ss.str();
return out;
}
double distance(Point p);
private:
double x, y;
};
class Triangle : public Shape {
public:
Triangle(Point p1_in, Point p2_in, Point p3_in);
friend std::ostream& operator<<(std::ostream& out, const Triangle& t) {
std::stringstream ss;
ss << t.p1 << " " << t.p2 << " " << t.p3;
out << ss.str();
return out;
}
double get_perimeter();
private:
Point p1, p2, p3;
bool check_valid();
};
class Circle : public Shape {
public:
Circle(Point centre_in, double radius_in);
friend std::ostream& operator<<(std::ostream& out, const Circle& c) {
std::stringstream ss;
ss << c.centre << ", " << c.radius;
out << ss.str();
return out;
}
double get_perimeter();
private:
const double PI = 3.14;
Point centre;
double radius;
};
#endif
|