Thanks for the replies. This is an update. The assignment says I can use unique pointers or shared pointers. I am guessing that I should, since I have to make three different functions that removes the last point from the graph, add a point to the graph using a pointer, and removes all pointers in a field and clears the memory for each object removed.
I was just testing things out with the random number generator to see how I could get it to work.
1 2 3 4 5 6 7 8 9 10 11
|
const int SIZE = 11;
int *myPointer = new int[SIZE];
default_random_engine engine{ static_cast<unsigned int>(time(0)) };
uniform_int_distribution<int> randomInt{ -11, 11 };
for (size_t i = 0; i < SIZE; i++)
{
myPointer[i] = randomInt(engine);
cout << myPointer[i] << endl << endl;
}
delete[] myPointer;
return 0;
|
This is the rest of my program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
class Graph
{
private:
vector<const Point*> m_vecPoints;
public:
Graph()
{
}
void addPoint(const Point*)
{
}
void clearGraph()
{
}
void plot()
{
}
bool removePoint(const int x, const int y)
{
}
};
class Point
{
private:
int m_x;
int m_y;
public:
Point(int x, int y)
{
m_x = x;
m_y = y;
}
void setX(int x)
{
m_x = x;
}
void setY(int y)
{
m_y = y;
}
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
double distanceFromOrigin()
{
return sqrt(pow(m_x, 2) + pow(m_y, 2));
}
};
int main()
{
const int SIZE = 11;
int *myPointer = new int[SIZE];
default_random_engine engine{ static_cast<unsigned int>(time(0)) };
uniform_int_distribution<int> randomInt{ -11, 11 };
for (size_t i = 0; i < SIZE; i++)
{
myPointer[i] = randomInt(engine);
cout << myPointer[i] << endl << endl;
}
delete[] myPointer;
return 0;
}
|
addPoint adds a point to the Graph by adding the passed in pointer to a Point object to the m_vecPoints.
clearGraph removes all the pointers in the m_vecPoints and frees the memory for each object removed.
Graph is the default constructor.
plot should display each point on the graph in order based on the distance from the origin (closest to furthest).
removePoint should remove the point from the graph if it exists and returns true if the point was removed and false if the point wasn’t removed.
The driver code should:
1) Randomly generate 11 points (x and y values should be between -11 and +11) and each point to the Graph. Each point must be dynamically allocated.
2) Plot the Graph
3) Remove the last point added from the Graph.
4) Attempt to remove a point that doesn’t exist in the Graph, the point must be a valid point (x and y values between -11 and +11)
5) Clear the Graph
6) Repeat steps 1 and 2.
The thing that threw me off was the vector<const Point*> m_vecPoints in private, and using pointers. I was not sure if I should use a vector.