-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program50.cpp
45 lines (39 loc) · 854 Bytes
/
Program50.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
/*PROGRAM USING OVERLOADING A BINARY OPERATOR WITH FRIEND FUNCTION NOT MEMBER FUNCTION*/
#include<iostream>
using namespace std;
class Distance {
private:
int feet, inches;
public:
Distance() {
feet = 0;
inches = 0;
}
Distance(int f) {
feet = f;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
void display() {
cout << feet << "'" << inches << "\"" << endl;
}
friend Distance operator+(const Distance&, const Distance&);
};
Distance operator+(const Distance& d1, const Distance& d2) {
int f = d1.feet + d2.feet;
int i = d1.inches + d2.inches;
if (i >= 12) {
i -= 12;
f++;
}
return Distance(f, i);
}
int main() {
Distance d1(2, 5), d2;
d2 = Distance(10) + d1;
d2.display();
return 0;
}