In: Computer Science
1. Create a class Point which has a template parameter of the type of internal data, T, and a template parameter for the dimension of the Point(2D, 3D etc.). Store a statically allocated, internal array of type T with dimension n. Ensure to include any constructer(s),destructors, getters or setters you might need.
2. Create a template function which computes the Euclidean distance between 2 points.
3. Instantiate two Point and compute their distance. Instantiate two Point and compute their distance.
Please have a .h and .cpp file and follow the instructions precisely thank you!
Edit: the value for n (size of internal array) should that be 2
#ifndef _EuclideanDistance_h
#define _EuclideanDistance_h
#include <iostream>
#include <cmath>
using namespace std;
// Defines a template class Point
template <class T>
class Point
{
// Data member to store x and y coordinate
T pointX;
T pointY;
public:
// Default constructor to assign default values to data
member
Point()
{
pointX = 0;
pointY = 0;
}// End of default constructor
// Parameterized constructor to assign parameter values to data
member
Point(T px, T py)
{
pointX = px;
pointY = py;
}// End of parameterized constructor
// Function to calculate Euclidean Distance of 2D
T EuclideanDistance2D(Point first, Point second)
{
// Calculates and returns the distance
return sqrt(pow(first.pointX - second.pointX, 2) +
pow(first.pointY - second.pointY, 2) * 1.0);
}// End of function
// Function to calculate Euclidean Distance of 3D
T EuclideanDistance3D(Point first, Point second, Point third)
{
// Calculates and returns the distance
return sqrt(pow(second.pointY - first.pointX, 2) +
pow(third.pointX - first.pointY, 2) +
pow(third.pointY - second.pointX, 2) * 1.0);
}// End of function
};// End of class Point
#endif
--------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include "EuclideanDistance.h" // Includes header file
using namespace std;
// main function definition
int main()
{
// Creates two objects of class Point of type float using
parameterized constructor for 2D
Point <float>point2DOne(5, 8);
Point <float>point2DTwo(2, 7);
// Calls the function to calculate the distance and displays the
return result
cout<<"\n Euclidean Distance 2D:
"<<point2DOne.EuclideanDistance2D(point2DOne,
point2DTwo);
// Creates two objects of class Point of type double using
parameterized constructor for 3D
Point <double>point3DOne(9, -7);
Point <double>point3DTwo(3, 9);
Point <double>point3DThree(2, 7);
// Calls the function to calculate the distance and displays the
return result
cout<<"\n Euclidean Distance 3D:
"<<point3DOne.EuclideanDistance3D(point3DOne, point3DTwo,
point3DThree);
return 0;
}// End of main function
Sample Output:
Euclidean Distance 2D: 3.16228
Euclidean Distance 3D: 9.84886