-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_MemberFunctions.cpp
89 lines (73 loc) · 1.73 KB
/
11_MemberFunctions.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
/*Write a c++ program in to find distance between two points and take third point and find from where the distance from third point is closest*/
#include <iostream>
#include <cmath>
using namespace std;
class algebra
{
public:
// setters
void setX(int);
void setY(int);
void setData(int, int);
void setAlgebra(algebra);
// getters
int getX() const;
int getY() const;
void getData() const;
algebra getAlgebra() const;
// memberFunctions
float calculateDistance(algebra);
algebra closestPoint(algebra , algebra );
private:
int x;
int y;
};
// SETTERS_scope_resolutions
void algebra::setX(int x) { this->x = x; }
void algebra::setY(int y) { this->y = y; }
void algebra::setData(int x, int y)
{
this->x = x;
this->y = y;
}
void algebra::setAlgebra(algebra obj)
{
x = obj.x;
y = obj.y;
}
// GETTERS_scope_resolutions
int algebra::getX() const { return x; }
int algebra::getY() const { return y; }
void algebra::getData() const
{
cout << "The value of x = " << x << endl;
cout << "The value of y = " << y << endl;
}
algebra algebra::getAlgebra() const
{
return *this;
}
// Member_Functions
float algebra::calculateDistance(algebra point)
{
int d1 = point.x - x;
int d2 = point.y - y;
int temp = ((d1 * d1) + (d2 * d2));
return sqrt(temp);
}
algebra algebra ::closestPoint(algebra p1, algebra p2)
{
float d1 = calculateDistance(p1);
float d2 = calculateDistance(p2);
return d1 <= d2 ? p1 : p2;
}
int main()
{
algebra point1, point2, point3;
point1.setData(6, 9);
point2.setData(12, 3);
point3.setData(1, 15);
cout << point1.calculateDistance(point2)<<endl;
point1.closestPoint(point2,point3).getData();
return 0;
}