Question

In: Computer Science

1. Create a class Point which has a template parameter of the type of internal data,...

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

Solutions

Expert Solution

#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


Related Solutions

In C++ 1.Create a class Point which has a template parameter of the type of internal...
In C++ 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. (10 points) 2.Create a template function which computes the Euclidean distance between 2 points. (6 points) 3.Instantiate two Point<double, 3> and compute their distance. Instantiate two Point<int,...
1. What does it mean to pass a parameter when the formal parameter is some type of class?
JAVA QuestionsAnswer both questions explaining what each is and using an example to back up your explanation.1. What does it mean to pass a parameter when the formal parameter is some type of class?2. What does it mean to pass a parameter when formal parameter is some type of interface?
Define a class template called genericStack for storing any data type in a static stack. Your...
Define a class template called genericStack for storing any data type in a static stack. Your class template must include a constructor, a destructor, push, pop, peek, isFull, and isEmpty functions (no display function is required). Write a simple main function in your program that demonstrates the class template with a stack of strings.
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template DArray (if you have not done so) and this attached Iterator class. Make sure to document and test your code thoroughly. import java.util.Arrays; public class DArray { private final double GROW_FACTOR = 0.5;// array size growing rate //attributes private int size; private int buffer[]; //the actual array //constructors public DArray() { this.size=10; } public DArray(int size) throws Exception { if(size < 0) { throw...
C++ Create an ArrayBag template class from scratch. This will require you to create two files:...
C++ Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation. The ArrayBag class must contain the following protected members: static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or...
1.) A class template can be derived from a non-template class True or False 2.) Inaccessible...
1.) A class template can be derived from a non-template class True or False 2.) Inaccessible pointer is a potential problem on simple linked list True or False 3.) Array based lists are faster in term of acing data True or False 4.) Simple linked lists use less space than double linked lists True or False 5.) For large lists "array based lists" are more efficient for insertion and deleting operational True or False 6.) We can remove data only...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
(In Matlab) Create a base class named Point that has the properties for x and y...
(In Matlab) Create a base class named Point that has the properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class, the x and y properties represent the center coordinates of a circle. The methods of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points. The derived class should have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT