Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using c++ Write a definition of a class named Point that can be used to store an

ID: 3697855 • Letter: U

Question


using c++

Write a definition of a class named Point that can be used to store and manipulate the location of a point in the 2D plane. Provide a constructor to initialize the private data with default values. You will need to declare and implement the following member functions: A member function set that sets the private data after an object of this class is created. A member function distance to find the distance of the point from the origin using the distance formula d = squareroot x^2 + y^2. A member function to swap the X and Y values. Two functions to retrieve the current coordinates of the point (one for X and Y). Write a test program that requests data for several points from the user, creates the points, then calls the member functions.

Explanation / Answer

#include <iostream>

using namespace std;

class Point //creates a class Point

{

         int getX(int); //this is an accessor function to get X values

         int getY(int); //this is an accessor function to get Y values

         int xOne;

         int yOne;

         int xTwo;

         int yTwo;

private:

         int x; //holds data pertaining to X1

         int y; //holds data pertaining to Y1

};

int Point::getX()

{

         return x;

}

int Point::getY()

{

         return y;

}

int main ()

{

         int n; //number of points

         cout << "Enter the number (between 1 and 10) of points you wish to input: "; //prompts user to input the number of points they wish to enter

         cin >> n; //reads in the number from the user

         for (int i = 0; i < n; i++) //this loop should allow you to enter n points

                 {

                          cout << " Enter a point's X coordinate. ";

                          cin >> Point::x1; //reads data into an array of X values

                          cout << " Enter a point's Y coordinate. "; //prompts user to input the y coordinate of a point

                          cin >> ; //reads data into an array of Y values

                 }

        

}

double Distance (double,double,double,double);
void print(double,double);

protected: double X;
double Y;
};
void point::point (double x, double y)
{
X=x;
Y=y;
}
void point::point()
{
X=0;
Y=0;
}
void point::print()
{
cout<<"X= "<<X<<endl;
cout<<"Y= "<<Y<<endl;
cout<<"The distance between this point and the origin is"<<sqrt(X*X+Y*Y)<<endl;
}
Distance (double c,double d,double e,double f)
{
cout<<"enter the coordinates of the 2 points"<<endl;
cin>>c>>d>>e>>f;
cout<<"The distance between thos two points is"<<sqrt((c-e)*(c-e)+(d-f)(d-f))<<endl;
}

// function to retrieve current coordinate value is below.

// swap function is below

#include <iostream>

// function prototype

void swap(int x, int y);

int main(void)

{

int x = 5, y = 10;

std::cout<<"In main. Before swap, x: "<<x<<" y: "<<y<<" ";

std::cout<<" Then calling function swap(x, y)... ";

swap(x, y);

std::cout<<" ...back to main. After swap, x: "<<x<<" y: "<<y<<" ";

return 0;

}

void swap(int x, int y)

{

int temp;

std::cout<<" In swap function, confirm before swapping, x: "<<x<<" y: "<< y << " ";

temp = x;

x = y;

y = temp;

std::cout<<"In swap function. After swapping, x: "<<x<<" y: "<<y<<" ";

}