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

This is a C++ program using Zybooks ( which means any warning will count as an e

ID: 3857176 • Letter: T

Question

This is a C++ program using Zybooks ( which means any warning will count as an error and terminate the program) Please help! Thank you

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.

LAB

#include <iostream>
using namespace std;

int main() {

/* Type your code here. */

return 0;
}

Explanation / Answer

# include<iostream>

# include<conio.h>

# include<stdio.h>

using namespace std;

class CMyPoint

{

private:

int int_X;

int int_Y;

public:

CMyPoint(int int_X_, int int_Y_)// Constructor

{

int_X = int_X_;

int_Y = int_Y_;

}

const int GetX()//Get x

{

return int_X;

}

const int GetY()// Get y

{

return int_Y;

}

friend ostream &operator<<( ostream &output, // Overload Operator

const CMyPoint* point )

{

output << "X : " << point->int_X << " Y : " << point->int_Y;

return output;

}

};

class CMyPointArray

{

private:

CMyPoint **pPoints;// Pointer to Array of objects

public:

CMyPointArray(int num)

{

pPoints = new CMyPoint*[num];// Allocate memory

for (int i = 0 ;i < num; i++)

{

*(pPoints+i) = new CMyPoint(0, 0);

}

}

~CMyPointArray()// Deconstructor

{

delete [] pPoints;

}

public:

CMyPoint* Get(const int iIndex)// Retrieve CMyPoint at iIndex

{

return *(pPoints+iIndex);

}

void Set(const int iIndex, CMyPoint* pt)// Set pt at index iIndex

{

// cout << pPoints.length << " ";

pPoints[iIndex] = pt;

}

};

int main()

{

CMyPoint *point1 = new CMyPoint(1, 2);// Make an instance of CMyPoint

cout << point1 << endl; // X : 1 Y : 2

CMyPoint *point2 = new CMyPoint(2, 3);

cout << point2 << endl;// X : 2 Y : 3

CMyPointArray pointArray(2);

pointArray.Set(0, point1);// Add to CMyPointArray

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote