using c++ Write a definition of a class named Point that can be used to store an
ID: 3697855 • Letter: U
Question
using c++
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<<" ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.