Classes and functions

Those are the instructions:
Write a class point that has two protected data members X, Y of type double. Include a constructor, a double function to find the distance between two points and a print function that prints the point X,Y and the distance between the origin (0,0) and that point. I'm facing a problem in writing the double function: if my class only contains 2 variables(the coordinates of the first point) how am i supposed to implement 2 other variables (the coordinates of the second point) in the double function??
Thank you in advance.
if there's anything else that's wrong let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  class point
{	public: point (double,double);
			point ();
			double findDistance (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(double X, double Y)
{
	cout<<"X= "<<X<<endl;
	cout<<"Y= "<<Y<<endl;
	cout<<"The distance between this point and the origin is"<<sqrt(X*X,Y*Y)<<endl;
}
double point::findDistance (double,double,double,double)

The distance could be a stand-alone function
double findDistance( const point & lhs, const point & rhs );
However, then the point needs more interface for outsider to (read) access the members.

Apparently you are expected to implement a member function with signature:
double point::findDistance( const point & rhs ) const;


The signature of your print should probably be:
void point::print() const;


Do not name the member function parameters the same as the member variables are. That creates confusion.
Last edited on
what do you mean by signature and rhs??
excuse me if my questions seem silly.
Signature, declaration, list of name and (return and parameter types) and qualifiers.

The "rhs" is just a name for a parameter. Right hand size.
Signature is what makes a function unique.
In C++, you can have two (or more) functions with the same name, but it is the different argument lists that makes the functions distinct. The signature is the combination of the function name and the unique argument list.

rhs is simply a variable name. We could have called the points a and b. In this case rhs was used to stand for Right Hand Side. More commonly used in comparisons where there are both a left hand side and right hand side. i.e. if (lhs < rhs)


code:
class point
{ public: point (double,double);
point ();
double findDistance (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;
}
double point::findDistance (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;
}




Is it correct??
Last edited on
I would like to use the findDistance like this:
1
2
3
4
point a( 2.4, 4.7 );
point b( -0.3, 3.1 );
double distance = a.findDistance( b );
std::cout << "The distance between the two points is " << distance << '\n';

Your version is used differently:
1
2
3
point a; // value is not important
double x, y, z, w; // values are not important
a.findDistance( x, y, z, w );


Which do you think to be closer to what your teacher expects?
Topic archived. No new replies allowed.