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...



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:

" role="presentation">


double *sequenceX;

" role="presentation"> double *sequenceY;

" role="presentation"> int numPoints;

public:

" role="presentation"> BagOfPoints(const int & =0);

" role="presentation"> BagOfPoints(const BagOfPoints &);

" role="presentation"> BagOfPoints(BagOfPoints&&);

" role="presentation"> void operator=(const BagOfPoints &);

" role="presentation"> void operator=(BagOfPoints&&);

" role="presentation"> ~BagOfPoints();

" role="presentation"> double getX(const int &) const;

" role="presentation"> double getY(const int &) const;

" role="presentation"> int getLength() const;

" role="presentation"> void setTerm(const int &, const double &, const double &);

" role="presentation">


void setLength(const int &);

};




  • (a)
    The methods
    getX(const int& k)
    and
    getY(const int& k)
    should return the x" role="presentation">x


and y" role="presentation">y coordinates of the k" role="presentation">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" role="presentation">i

  • -th point to
    x
    and
    y.


  • (d)
    The method
    setLength(const int &n)
    should change the length of the sequences to n" role="presentation">n
  • . Keep in mind that this means that the sequences have to be re-allocated. For example if the old length was 7" role="presentation">7 and the new is 107" role="presentation">107, you may have only allocated the blocks of length 7" role="presentation">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" role="presentation">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" role="presentation">−1


    every x" role="presentation">x-coordinate and every y" role="presentation">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.



    problem 2:



    What is the output of the following code? Provide a detailed explanation for your answer.



    // move_assignment.cpp

    // compile with

    // c++ -o ma move_assignment.cpp -std=c++11 -fno-elide-constructors

    // execute with

    // ./ma

    #include

    class MyClass{

    public:

    " role="presentation">


    MyClass();

    " role="presentation"> MyClass(const MyClass &);

    " role="presentation"> MyClass(MyClass &&);

    " role="presentation"> void operator=(const MyClass &);

    " role="presentation"> void operator=(MyClass &&);

    " role="presentation"> ~MyClass();

    };

    MyClass::MyClass(){

    " role="presentation"> std::cout
    }

    MyClass::MyClass(const MyClass & copyFrom){

    " role="presentation"> std::cout
    }

    MyClass::MyClass(MyClass && moveFrom){

    " role="presentation"> std::cout
    }

    void MyClass::operator=(const MyClass & copyFrom){

    " role="presentation"> std::cout
    }

    void MyClass::operator=(MyClass&& moveFrom){

    " role="presentation"> std::cout
    }

    MyClass::~MyClass(){

    " role="presentation"> std::cout
    }

    MyClass myFunction(){

    " role="presentation"> MyClass temp;

    " role="presentation"> return temp;

    }

    void heavyPrinting(){

    " role="presentation"> MyClass object1;

    " role="presentation"> MyClass object2;

    " role="presentation"> object1=object2;

    " role="presentation"> MyClass object3;

    " role="presentation"> object3=myFunction();

    " role="presentation"> std::cout
    }

    int main(){

    " role="presentation"> heavyPrinting();

    " role="presentation"> std::cout" role="presentation"> return 0;

    }
    Mar 31, 2021
    SOLUTION.PDF

    Get Answer To This Question

    Related Questions & Answers

    More Questions »

    Submit New Assignment

    Copy and Paste Your Assignment Here