Write a definition of a class named Point that can be used to store and manipula
ID: 3697835 • Letter: W
Question
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 sec that vets 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
Answer for Question:
1. Created class called Point with default and parametrized constructors.
2. Created set and distance and getX and getY methods
3. Created main method with two objects.
Code :
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
// Coordinates x and y
double x;
double y;
public:
// Default Constructor
Point()
{
x = 0.0;
y = 0.0;
}
// Parametrised Constructor
Point(double x1, double y1)
{
x = x1;
y = y1;
}
// Set method will set the x and y
void set(double x1, double y1)
{
x = x1;
y = y1;
}
// distance method will calculate the distance between origin and x,y
double distance()
{
double result = 0.0;
result = sqrt(x*x + y*y);
return result;
}
// Swap the x and y
void swap()
{
double temp;
temp = x;
x = y;
y = temp;
}
// Retrieve methods for x and y
double getX()
{
return x;
}
double getY()
{
return y;
}
// Print the x and y and distance
void print()
{
cout<<"X is :"<<x<<" Y is :"<<y<<" Distance is :"<<distance()<<endl;
}
};
int main()
{
Point p1(5.6, 6.7), p2;
p1.distance();
p1.print();
p2.set(7.8,9.45);
p2.distance();
p2.print();
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.