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

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. 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.
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.
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 c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
In C++ Create a class that simulates a school calendar for a course and has a...
In C++ Create a class that simulates a school calendar for a course and has a warner that provides the school administration with the option of warning students when the last day is that a student is permitted to drop the course. (Allow administrators to warn or not, as they wish. Do not make this a required function.) You will assume in this calendar that there are 12 months in a year, 4 weeks in a month, and 7 days...
(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...
Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT