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.
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.
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)
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?