Question

In: Computer Science

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, 2> and compute their distance.(4 points)

Solutions

Expert Solution

#include <iostream>
#include <cstdarg>
#include <cmath>

using namespace std;

template <class T, int n>
class Point
{
private:
T arr[n];

public:
// Constructor
Point(...)
{
//cout<<"\n Constructor called \n";
va_list ap;
va_start(ap, n);
for(int i=0; i<n; i++)
{
arr[i] =va_arg(ap, T);
}
va_end(ap);
}
// For debugging
void print()
{
cout<<"\n Point: values \n";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
}
// Destructor
~Point()
{
//cout<<"\n deleting \n";
delete[] arr;
//cout<<"\n deleted\n";
}
// Setter: changing whole value of a Point
void setPoint(...)
{
//cout<<"\n Set point called \n";
va_list ap;
va_start(ap, n);
for(int i=0; i<n; i++)
{
arr[i] =va_arg(ap, T);
}
va_end(ap);
}
//Getter: get whole Point , as a pointer to the internal "arr"
T* getPoint()
{
return arr;
}

//Setter: set a particular dimension value
void setDimensionValue(long unsigned int dim, T val )
{
if ( dim>n ) cout<<"\n The dimension does not exist for this point \n";
else arr[dim-1] = val;
}

//Getter: get a particular dimension value
T getDimensionValue(long unsigned int dim)
{
if (dim> n){
cout<<"\n The dimension does not exist for this point. \n ";
return -9999999999999999999999;
}
else return arr[dim-1];
}

};

// function for calculating Eucledian distance
template <class T, int n>
T distance(Point<T, n> p1, Point<T, n> p2)
{
T dist;
for(int i=1; i<=n; i++){
T tmp = p1.getDimensionValue(i) - p2.getDimensionValue(i);
dist += tmp*tmp;
}
return sqrt(dist);
}


int main()
{

Point<double, 3> p1(1.0 , 3.5, 4.5), p2(3.4, 5.6, 2.3);
cout<<"\n Distance between 3d points p1 and p2 is: "<<distance<double, 3>(p1, p2) <<"\n";

Point<double, 2> p3(1.2, 6.7), p4(2.4, 9.1);
cout<<"\n Distance between 2d points p3 and p4 is: "<<distance<double, 2>(p3, p4) <<"\n";


return 0;
}


Related Solutions

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....
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. Use the enum keyword or a C++ class to create a new type Boolean with...
1. Use the enum keyword or a C++ class to create a new type Boolean with the two values F and T defined. Use the C++ class/struct keyword and an array to create a list of pairs that cover all possible combinations of the 2 Boolean constants you defined. 2. Extend the same program and implement conjunction and disjunction functionality in a separate library (mylib.h/mylib.cpp). Use your implementation to print truth tables for both. 3. Further extend the same program...
1. Use the enum keyword or a C++ class to create a new type Boolean with...
1. Use the enum keyword or a C++ class to create a new type Boolean with the two values F and T defined. Use the C++ class/struct keyword and an array to create a list of pairs that cover all possible combinations of the 2 Boolean constants you defined. 2. Extend the same program and implement conjunction and disjunction functionality in a separate library (mylib.h/mylib.cpp). Use your implementation to print truth tables for both. 3. Further extend the same program...
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?
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Use the enum keyword or a C++ class to create a new type Boolean with the...
Use the enum keyword or a C++ class to create a new type Boolean with the two values F and T defined. Use the C++ class/struct keyword and an array to create a list of pairs that cover all possible combinations of the 2 Boolean constants you defined.
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor:...
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor: Point(double x, double y, double z) and, the following API: double distanceto(Point q) it returns the Euclidean distance between this and q. The Euclidean distance between (x1, y1, z1) and (x2, y2, z2) is defined as sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2). String toString() – it returns the string representation of the point. An example would be (2.3,4.5,3.0). Write a main method in the...
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and...
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and an integer (the second parameter). If the second parameter is nonnegative, the function returns the value of the first parameter raised to the power of the second parameter. Otherwise, if the second parameter is negative the function returns 0. For example powerTo(2,3)=8, and powerTo(2,-3)=0
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind about details but please include the basic functions that are necessary for a RedBlackTree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT