A housing society is represented by a 3D plane. We have a class house which
stores the location of a house of three persons using x, y and z coordinates of
plane. Another class market which stores the location of a main market. Each class
should initialize the data members using parameterized constructor. Keep the data
members of all the classes private. Also display the location of houses and market.
Calculate the distance of houses of all the persons from the market and display
which person’s house is far away from the market. Also display which person has
nearest distance from the market.
The class manager has the access to both class house and market so it swaps the
location of the market with the house of a person that has the largest distance
with the market. in C++
#include<iostream>
#include<math.h>
using namespace std;
class market;
class house{
private:
float x, y, z;
public:
house(int a, int b, int c){
x = a;
y = b;
z = c;
}
void display(){
cout<<"The person is located at point\n";
cout<<x<<" , "<<y<<" , "<<z<<endl;
}
float getX(){
return x;
}
float getY(){
return y;
}
float getZ(){
return z;
}
friend float cal_distance(house h, market m);
};
float distance(float x1, float y1, float z1, float x2, float y2, float z2)
{
float d = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0);
return d;
}
class market{
private:
float x, y, z;
public:
market(int a, int b, int c){
x = a;
y = b;
z = c;
}
void displayMarket(){
cout<<"The market is located at point\n";
cout<<x<<" , "<<y<<" , "<<z<<endl;
}
float getX(){
return x;
}
float getY(){
return y;
}
float getZ(){
return z;
}
friend float cal_distance(house h, market m);
};
float cal_distance(house h, market m){
float x1 = h.getX();
float y1 = h.getY();
float z1 = h.getY();
float x2 = m.getX();
float y2 = m.getY();
float z2 = m.getZ();
float dist = distance(x1, y1, z1,x2, y2, z2);
cout<<"The distance between the house and the market is: "<<dist<<endl;
return dist;
}
int main(){
house h1(2, -5, 7);
h1.display();
house h2(0, -4, 8);
h2.display();
house h3(1, -3, 7);
h3.display();
market m(3,4,5);
m.displayMarket();
float first = cal_distance(h1, m);
float second = cal_distance(h2, m);
float third = cal_distance(h3, m);
float arr[3] = {first, second, third};
float min = arr[0];
float max = arr[0];
int index = 0;
int k = 0;
for(int i=0; i<3; i++){
if(arr[i]>max){
max = arr[i];
index = i;
}
}
for(int i=0; i<3; i++){
if(arr[i]<min){
min = arr[i];
k = i;
}
}
cout<<"The person with nearest distance is person "<<1<<" with a distance of: "<<min<<endl;
cout<<"The person with farthest distance is person "<<3<<" with a distance of: "<<max<<endl;
}
Comments
Leave a comment