Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ For this activity we attempt to make our own container class (like vector) t

ID: 3856880 • Letter: C

Question

C++ For this activity we attempt to make our own container class (like vector) to hold instances of our "point" class. To do this you will need create 2 classes as described below: CMyPoint - This class will contain a point in an arbitrary 2 dimensional space. You will create instances of this class and store them in the container CMyPointArray. To achieve this you will need to implement the following: int _X - which is an integer that contains the location of the point in the horizontal direction int _Y - which is an integer that contains the location of the point in the vertical direction const int GetX() - which is the "getter function for the "x" coordinate. const int GetY() - which is the "getter function for the "y" coordinate. friend ostream& operator << (ostream& os, const CMyPoint& pt) - this function is an overload of the "<<" operator that will allow the class to print itself when used in an io stream (just like all of the other variables in this course). Please see this link for more information The constructor for this class should take "X" and "Y" parameters with defaults of zero. All variables are to be declared private and all functions declared public. CMyPointArray - This class will dynamically (e.g. new and delete) allocate and de-allocate memory so that it is capable of containing an arbitrary number of instances of the CMyPoint object. To achieve this you will need to implement the following: CMyPoint* pPoints - This variable is a pointer to the memory that contains the CMyPoint objects. CMyPoint Get(const int iIndex) - This is the "getter" function used to retrieve a particular point that is contained in an instance of the CMyPointArray class. The parameter const int iIndex is used to locate the particular point. void Set(const int iIndex, const CMyPoint pt) - This is the "setter" function used to store a particular point in an instance of the CMyPointArray class, at the location indicated by iIndex. The class constructor should take as a parameter the size of the container (e.g. how may CMyPoint objects it will hold) and allocate the appropriate space to hold theses objects. The class destructor should free any memory the CMyPointArray class owns. All variables are to be declared private and all functions declared public. Note: Do not use the STL vector object in any part of your program or you will receive 0 points. Instead go caveman style and use the C++ new and delete functions as described in this chapter.

#include <iostream>
using namespace std;

int main() {

/* Type your code here. */

return 0;
}

Explanation / Answer

#ifndef CMyPoint _H
#define CMyPoint _H
#include <iostream>

class CMyPoint {
private:
int x, y;

public:
explicit CMyPoint (int x = 0, int y = 0);
int getX() const;
int getY() const;
void setX(int x);
void setY(int y);
CMyPoint & operator++(); // ++prefix
const CMyPoint operator++(int dummy); // postfix++
const CMyPoint operator+(const CMyPoint & rhs) const; // CMyPoint + CMyPoint
const CMyPoint operator+(int value) const; // CMyPoint + int
CMyPoint & operator+=(int value); // CMyPoint += int
CMyPoint & operator+=(const CMyPoint & rhs); // CMyPoint += CMyPoint

friend std::ostream & operator<<(std::ostream & out, const CMyPoint & CMyPoint ); // out << CMyPoint
friend std::istream & operator>>(std::istream & in, CMyPoint & CMyPoint ); // in >> CMyPoint
friend const CMyPoint operator+(int value, const CMyPoint & rhs); // int + CMyPoint
};

#endif

======================================================================

#include "CMyPoint .h"
#include <iostream>
using namespace std;

// Constructor - The default values are specified in the declaration
CMyPoint ::CMyPoint (int x, int y) : x(x), y(y) { }

// Getters
int CMyPoint ::getX() const { return x; }
int CMyPoint ::getY() const { return y; }

// Setters
void CMyPoint ::setX(int x) { this->x = x; }
void CMyPoint ::setY(int y) { this->y = y; }

// Overload ++Prefix, increase x, y by 1
CMyPoint & CMyPoint ::operator++() {
++x;
++y;
return *this;
}

// Overload Postfix++, increase x, y by 1
const CMyPoint CMyPoint ::operator++(int dummy) {
CMyPoint old(*this);
++x;
++y;
return old;
}

// Overload CMyPoint + int. Return a new CMyPoint by value
const CMyPoint CMyPoint ::operator+(int value) const {
return CMyPoint (x + value, y + value);
}

// Overload CMyPoint + CMyPoint . Return a new CMyPoint by value
const CMyPoint CMyPoint ::operator+(const CMyPoint & rhs) const {
return CMyPoint (x + rhs.x, y + rhs.y);
}

// Overload CMyPoint += int. Increase x, y by value
CMyPoint & CMyPoint ::operator+=(int value) {
x += value;
y += value;
return *this;
}

// Overload CMyPoint += CMyPoint . Increase x, y by rhs
CMyPoint & CMyPoint ::operator+=(const CMyPoint & rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}

// Overload << stream insertion operator
ostream & operator<<(ostream & out, const CMyPoint & CMyPoint ) {
out << "(" << CMyPoint .x << "," << CMyPoint .y << ")";
return out;
}

// Overload >> stream extraction operator
istream & operator>>(istream & in, CMyPoint & CMyPoint ) {
cout << "Enter x and y coord: ";
in >> CMyPoint .x >> CMyPoint .y;
return in;
}

// Overload int + CMyPoint . Return a new CMyPoint
const CMyPoint operator+(int value, const CMyPoint & rhs) {
return rhs + value; // use member function defined above
}

===================================================

class CMyPointArray

{private:

int size;

CMyPoint* ptr;

public:

CMyPointArray( int arraySize;)

{

size = arraySize;

ptr = new CMyPoint[ size ];

}

~CMyPointArray()

{

delete [] ptr;

}

  

void Set(const int iIndex, const CMyPoint pt)

{

ptr[iIndex]=pt;

}

CMyPoint Get(const int iIndex)

{

return ptr[iIndex];

}

};