-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-Sub_distance-constructor.cpp
More file actions
63 lines (56 loc) · 1.13 KB
/
Add-Sub_distance-constructor.cpp
File metadata and controls
63 lines (56 loc) · 1.13 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
#include <iostream>
using namespace std;
class Distance
{
private:
int inch, feet;
public:
Distance(int a, int b)
{
inch = a;
feet = b;
}
Distance() {
inch = 0;
feet = 0;
}
void display()
{
cout << "Distance is: " << feet << " : " << inch << endl;
}
Distance add(Distance d)
{
Distance temp;
temp.inch = inch + d.inch;
temp.feet = feet + d.feet;
if (temp.inch >= 12)
{
temp.feet += temp.inch / 12;
temp.inch = temp.inch % 12;
}
return temp;
}
Distance sub(Distance d)
{
Distance temp;
temp.inch = inch - d.inch;
temp.feet = feet - d.feet;
if (temp.inch < 0)
{
temp.feet -= 1;
temp.inch += 12;
}
return temp;
}
};
int main()
{
Distance d1(8, 6), d2(3, 5), d3, d4;
d3 = d1.add(d2);
d4 = d1.sub(d2);
cout << "Addition of two distances: " << endl;
d3.display();
cout << "Subtraction of two distances: " << endl;
d4.display();
return 0;
}