question archive Representing a point in the plane, a class named PlanePoint has Two data members i

Representing a point in the plane, a class named PlanePoint has Two data members i

Subject:Computer SciencePrice:4.89 Bought3

Representing a point in the plane, a class named PlanePoint has

  • Two data members i.e. and (correspond to x and y coordinates) of integer type,
  • Two accessor functions named as getX() and getY() to get values of data members and Y
  • A function named planeDistance() that calculates and returns the distance between two points in a plane

Derive a class named SpacePoint to imagine a point in a three-dimensional space and has following additional features:

  • A data member of type integer to represent the z-coordinate of a point in space
  • no-argument constructor that constructs a point with coordinates (0,0,0)
  • constructor with three arguments that constructs a point with three specified coordinate values
  • An accessor function, which returns the value of data field Z
  • A function named spaceDistance to return the distance between two points in the space

Implement both the classes and in the main() create two points with different dimensions and invoke appropriate function to display the distance between these two points.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

#include<bits/stdc++.h>
using namespace std;


class PlanePoint
{
public:
   int x;
   int y;


   int getX()
   {
       return x;
   }


   int getY()
   {
       return y;
   }


  double planeDistance(PlanePoint p1,PlanePoint p2)
  {
      //finding (x1-x2)^2 +  (y1-y2)^2
      double distance= (p1.getX()-p2.getX())*(p1.getX()-p2.getX()) + (p1.getY()-p2.getY())*(p1.getY()-p2.getY());

      //finding square root of (x1-x2)^2 +  (y1-y2)^2   and return distance
      distance=sqrt(distance);
      return distance;
  }
};


class SpacePoint:public PlanePoint
{
public:
    int z;
    SpacePoint()
    {
        x=0;
        y=0;
        z=0;
    }
    SpacePoint(int X,int Y,int Z)
    {
        x=X;
        y=Y;
        z=Z;
    }


    int getZ()
    {
        return z;
    }

     //finding (x1-x2)^2 +  (y1-y2)^2 + (z1-z2)^2
    double spaceDistance(SpacePoint p1,SpacePoint p2)
    {
        double distance= (p1.getX()-p2.getX())*(p1.getX()-p2.getX()) + (p1.getY()-p2.getY())*(p1.getY()-p2.getY()) + (p1.getZ()-p2.getZ())*(p1.getZ()-p2.getZ());

      //fincding square root of (x1-x2)^2 +  (y1-y2)^2 + (z1-z2)^2
      distance=sqrt(distance);
      return distance;
    }
};


int main()
{
   cout<<"---------------Plane Point-------------------"<<endl;
   //create object as point p1 and p2
   PlanePoint p1;
   PlanePoint p2;

   //initialize value of oint p1 and p2
   p1.x=5;
   p1.y=7;


   p2.x=10;
   p2.y=15;

   //print points
   cout<<"x1="<<p1.getX()<<endl;
   cout<<"y1="<<p1.getY()<<endl;


   cout<<"x2="<<p2.getX()<<endl;
   cout<<"y2="<<p2.getY()<<endl;
   //print distance by callinfg function
   cout<<"Distance between Point P1 and P2: "<<p1.planeDistance(p1,p2)<<endl;


   cout<<"\n---------------Space Point-------------------"<<endl;

   //creating object for space point s1 and s2
   SpacePoint s1;
   SpacePoint s2;

   //initialize point value by s1 and s2
   s1.x=4;
   s1.y=7;
   s1.z=10;


   s2.x=15;
   s2.y=20;
   s2.z=30;

   //print points 
   cout<<"x1="<<s1.getX()<<endl;
   cout<<"y1="<<s1.getY()<<endl;
   cout<<"z1="<<s1.getZ()<<endl;


   cout<<"x2="<<s2.getX()<<endl;
   cout<<"y2="<<s2.getY()<<endl;
   cout<<"z1="<<s2.getZ()<<endl;

   //print distance between space points s1 and s2
   cout<<"Distance between Space Point s1 and s2: "<<s1.spaceDistance(s1,s2)<<endl;


  return 0;
}

 

pleaes see the attached file for the complete solution.