Write a template class that represents a point. The data members should consist
of an x coordinate and a y coordinate. The coordinates should be of type int
or float depending on how the class is instantiated. The class should
contain the following methods:
- a default constructor that sets both coordinates to 0
- a constructor that accepts the initial values of the coordinates as parameters
- get and set methods for both x and y
- a print method that displays the coordinate pair in parentheses
- a distance method that accepts another point as a parameter and returns the
distance between the two points as a float
You can read about the distance formula at
http://www.purplemath.com/modules/distform.htm
You may be interested in using the sqrt function that computes the
square root of it argument. Here is code to test it also:
#include <iostream>
#include "point.h"
using namespace std;
int main(){
//test point template with int
point<int> a(1,1);
cout << "a is a ";
a.print();
cout << endl;
point<int> b;
cout << "b is a ";
b.print();
cout << endl;
cout << "The distance between a and b is ";
cout << a.distance(b) << endl << endl;
//test point template with float
point<float> c(6.5,4.25);
cout << "c is a ";
c.print();
cout << endl;
point<float> d(0.5,-3.75);
cout << "d is a ";
d.print();
cout << endl;
cout << "The distance between c and d is ";
cout << c.distance(d) << endl << endl;
}
Sample Output:
a is a point(1, 1)
b is a point(0, 0)
The distance between a and b is 1.41421
c is a point(6.5, 4.25)
d is a point(0.5, -3.75)
The distance between c and d is 10