IN C++11.Create a class BagOfPoints that represents a collection of points in tw
ID: 3873507 • Letter: I
Question
IN C++11.Create a class BagOfPoints that represents a collection of points in two-dimensional space. Its private attributes are numPoints, which represents the total number of points, and two sequences sequenceX and sequenceY that are of type double*. The declaration of the class should be
class BagOfPoints{
private:
double *sequenceX;
double *sequenceY;
int numPoints;
public:
BagOfPoints(const int & =0);
BagOfPoints(const BagOfPoints &);
BagOfPoints(BagOfPoints&&);
void operator=(const BagOfPoints &);
void operator=(BagOfPoints&&);
~BagOfPoints();
double getX(const int &) const;
double getY(const int &) const;
int getLength() const;
void setTerm(const int &, const double &, const double &);
void setLength(const int &);
};
(a) The methods getX(const int& k) and getY(const int& k) should return the x
and y coordinates of the k-th point.
(b) The method getLength() should return the attribute numPoints.
(c) The method setTerm(const int & i, const double &x, const double &y) should set the coordinates of the i-th point to x and y.
(d) The method setLength(const int &n) should change the length of the sequences to n
. Keep in mind that this means that the sequences have to be re-allocated. For example if the old length was 7 and the new is 107, you may have only allocated the blocks of length 7 for the pointers sequenceX and sequenceY. You need to create new memory locations, copy the appropriate values of sequences sequenceX and sequenceY, re-assign the pointers sequenceX and sequenceY to point to the new locations, and delete the old memory to prevent the memory leak.
(e) The method bagOfPoints(const int& k); should be the default constructor that sets the number of points to k and allocates the memory for the sequences sequnceX and sequenceY. The methods BagOfPoints(const BagOfPoints& copyFrom); and void operator=(const BagOfPoints& copyFrom); are copy constructor and copy assignment operator. They should allocate the memory for sequenceX and sequenceY to be the exact size as the corresponding sequences in copyFrom. However, they should be at new locations. The values copyFrom.sequenceX[i] should be copied to sequenceX[i], and analogous should hold for copyFrom.sequenceY[i].
(f) The methods BagOfPoints(BagOfPoints&& moveFrom); and void operator=(BagOfPoints&& moveFrom); should be the move constructor and move assignment operator. Instead of allocating new memory for the sequences sequenceX and sequenceY, the move constructor and move assignment operator should instead just position the pointers sequenceX and sequenceY to the addresses of the corresponding pointers of the object moveFrom. However, the object moveFrom should then have its pointers set to nullptr so there is no error when destructor tries to free the memory.
(g) The method ~BagOfPoints() should be the destructor and should free the memory occupied by sequences sequenceX and sequenceY
In addition to the class and methods described above, create a function BagOfPoints reflectPoints(const BagOfPoints& bag); that multiplies with 1
every x-coordinate and every y-coordinate of every point from the bag. Create a function int main() in which the user first enters the number of points he/she wishes to have in the bag, and then the user inserts the coordinates of these points. Based on the user input create an object bP that corresponds to the bag of points that user has entered. Create an object bPReflected that corresponds to the reflected bag of points obtained from the function reflectPoints. Discuss which constructors and/or assignment operators were called during the execution of your program.