-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc4.cpp
More file actions
81 lines (67 loc) · 1.13 KB
/
pc4.cpp
File metadata and controls
81 lines (67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//ft n inches 3 constructors default copy parameterized fn addn (2 ob in 1 out) and disp fn
#include<iostream>
#include<stdlib.h>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
friend Distance addition(Distance d1,Distance d2);
Distance()
{
}
Distance(int ft, int in)
{
feet = ft;
inches = in;
}
Distance(Distance& d)
{
feet = d.feet;
inches = d.inches;
}
void display();
};
//class functions
void Distance::display()
{
cout<<"The distance is:"<<feet<<"ft and "<<inches<<"in\n";
}
//friend function
Distance addition(Distance d1,Distance d2)
{
Distance d;
d.feet = d1.feet + d2.feet;
d.inches = d1.inches + d2.inches;
return d;
}
void check(int a, int b)
{
try
{
if(a < 0 || b < 0)
throw 1;
}
catch(int)
{
cout<<"\nNO NEGATIVES!\n";
exit(0);
}
}
//Main
int main()
{ int a,b;
cout<<"\nEnter the first distance in > ft in :";
cin>>a>>b;
check(a,b);
Distance d1(a,b);
//cout<<"\nEnter the second distance in > ft in :";
// cin>>a>>b;
//check(a,b);
Distance d2(d1);
Distance d3;
d3 = addition(d1,d2);
d3.display();
}