-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPEQ3.cpp
81 lines (68 loc) · 1.6 KB
/
PEQ3.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
#include <iostream>
#include <cmath>
using namespace std;
class Triangle
{
private:
float a, b, c, h;
public:
Triangle() {}
Triangle(float a, float b, float c)
{
this->a = a;
this->b = b;
this->c = c;
}
Triangle(float b, float h)
{
this->b = b;
this->h = h;
}
float area()
{
return area(this->a, this->b, this->c);
}
float area(float a, float b, float c)
{
float p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
// overloaded functions
float area(float b, float h)
{
return (b + h) / 2;
}
// Overload assignment operator
Triangle &operator=(const Triangle &triangle)
{
// do the copy
this->a = triangle.a;
this->b = triangle.b;
this->c = triangle.c;
return *this;
}
// equality operator.
friend bool operator==(const Triangle &t1, const Triangle &t2)
{
return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c);
}
};
int main()
{
Triangle t1(18, 30, 24);
cout << "Area of the tringle with sides : " << t1.area(18, 30, 24) << "\n";
Triangle t2;
cout << "Area of the tringle with base and height : " << t2.area(24, 18) << "\n";
;
Triangle tCopy = t1;
cout << "Area of the copy tringle " << tCopy.area() << "\n";
if (t1 == tCopy)
{
cout << "The triangles are equal.\n";
}
else
{
cout << "The triangles are not equal.\n";
}
return 0;
}