-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop_lab0.5.cpp
144 lines (115 loc) · 2.2 KB
/
oop_lab0.5.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <cmath>
using namespace std;
struct Point {
double x;
double y;
void print() {
printInfo();
cout << "(" << x << ", " << y << ")";
}
bool equal(const Point& p) {
return x == p.x && y == p.y;
}
double dist(const Point& p) {
return sqrt( (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) );
}
// Use constructors instead
void set(double x_ = 0.0, double y_ = 0.0) {
x = x_;
y = y_;
}
private:
void printInfo() {
cout << "" << endl;
}
};
class Triangle {
Point a;
Point b;
Point c;
public:
void print() {
cout << "Triangle: ";
a.print();
cout << " , ";
b.print();
cout << " , ";
c.print();
cout << endl;
}
/*const*/Point/*&*/ get(int index) {
if (index == 0) return a;
else if (index == 1) return b;
else return c;
}
double area() const {
return 1; /* ... */
}
// Two triangles have the same area
bool equal(const Triangle& t) {
return area() == t.area();
}
// Two triangles are actually the same (same points)
bool isTheSame(const Triangle& t) {
// We have access to *everything* inside *any* Triangle,
// not just the object we called the method on
return ( a.equal(t.a) && b.equal(t.b) && c.equal(t.c) );
}
// Use constructors instead
void set(const Point& a_, const Point& b_, const Point& c_) {
a = a_;
b = b_;
c = c_;
}
};
class Rectangle {
Point a;
Point b;
public:
void print() {
cout << "Rectangle: ";
a.print();
cout << " , ";
b.print();
cout << endl;
}
/*const*/Point/*&*/ get(int index) {
if (index == 0) return a;
else return b;
}
double area() const {
return 1; /* ... */
}
bool equal(const Rectangle& r) {
return area() == r.area();
}
// Use constructors instead
void set(const Point& a_, const Point& b_) {
a = a_;
b = b_;
}
};
int main() {
Point p1, p2;
p1.set(1.2, 3.4);
p2.set();
p1.print();
cout << endl;
p2.print();
// p2.printInfo();
cout << endl;
cout << p1.dist(p2) << endl;
Point a, b, c, d, e, f;
a.set();
b.set(1, 2);
c.set(3, 4);
d.set();
e.set(5, 6);
f.set(7, 8);
Triangle t1, t2;
t1.set(a, b, c);
t2.set(d, e, f);
cout << (t1.equal(t2) ? "" : "NOT ") << "Equal" << endl;
cout << (t1.isTheSame(t2) ? "" : "NOT ") << "The same" << endl;
}