-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.cpp
101 lines (83 loc) · 2.02 KB
/
operators.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
/* Operator overloading
* Tanner Babcock
* tababcock@dmacc.edu
* October 23, 2021 */
#include <iostream>
#include <vector>
using namespace std;
class Shape {
private:
public:
virtual double area() {
return 0;
}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h): width(w), height(h) { }
double perimeter(void) {
return width + width + height + height;
}
double area(void) {
return width * height;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r): radius(r) { }
double area(void) {
return 3.14 * radius * radius;
}
};
/* addition */
double operator+(Shape &s1, Shape &s2) {
return s1.area() + s2.area();
}
double operator+(double area, Shape &s) {
return area + s.area();
}
/* subtraction */
double operator-(Shape &s1, Shape &s2) {
return s1.area() - s2.area();
}
double operator-(double area, Shape &s) {
return area - s.area();
}
/* multiplication */
double operator*(Shape &s1, Shape &s2) {
return s1.area() * s2.area();
}
double operator*(double area, Shape &s) {
return area * s.area();
}
int main(void) {
Rectangle shape1(10, 10);
Rectangle shape2(5, 10);
Circle shape3(5);
cout << "Shape1 area: " << shape1.area() << endl;
cout << "Shape2 area: " << shape2.area() << endl;
cout << "Shape3 area: " << shape3.area() << endl;
// adding shape objects together returns the sum of their areas
double total = shape1 + shape2 + shape3;
cout << "The total area is " << total << endl;
cout << "Shape1 minus Shape2: " << (shape1 - shape2) << endl;
cout << "Shape3 minus Shape2: " << (shape3 - shape2) << endl;
cout << "Shape1 times Shape2: " << (shape1 * shape2) << endl;
return 0;
}
/* A program like this could be used by landscaping or construction
* companies, to find out how much grass, or metal, or concrete
* they need, to cover multiple buildings */
/* Output:
Shape1 area: 100
Shape2 area: 50
Shape3 area: 78.5
The total area is 228.5
Shape1 minus Shape2: 50
Shape3 minus Shape2: 28.5
Shape1 times Shape2: 5000
*/