I have an assignment and I will list the exercises and then my solution for them as well as the problem that I am nicely asking you to help me understand and resolve it!
The Point class describes a point in a 2D / 3D space and contains
· x - integer attribute · y - integer attribute · z - integer attribute · Attributes are initialized by default · Class has no default constructor · The class has two constructors: one that requests X and Y and another that requests all values · The class has get and set methods for the three values
Each geometric figure is defined by a given number of corners (the lines that join them define the geometric figure)
The AbstractGeometricFigure class contains
· Figure type - string attribute · is2D - boolean attribute · number of corners - integer attribute · Corners array - dynamic vector of Point objects · area() - a pure virtual method that determines the area of the geometric figure · perimeter() - pure virtual method that determines the area of the geometric figure · getCorners() - method that displays the corners of the geometric figure using the template (X, Y) for 2D and (X, Y, Z) for 3D · addCorner () - method that adds a new corner
I stumble upon the addCorner() method because I need to create another array to which I will add the new corner. In order to create a dynamic array of type Point class, the compiler is telling me to create the default constructor for the class Point. As you can see above, I am not allowed to do it. How can I create another array that will copy the Corners array, and add at the end of it the new value, without implementing the default constructor of class Point? Even if I don't have to create another array, at the exam it might be possible to have such an exercise, and I want to know , what should I do in case I have a dynamic array of type class, and I am not allowed to implement the default constructor of the class.
Here is my code till this point:
#include<iostream>
#include<string>
using namespace std;
class MySpecialException:public exception
{
public:
MySpecialException(string msg) :exception(msg.data())
{
}
};
class Point
{
int x=0;
int y=0;
int z=0;
public:
Point(int X,int Y)
{
this->x = X;
this->y = Y;
}
Point(int X,int Y,int Z)
{
this->x = X;
this->y = Y;
this->z = Z;
}
int getX()
{
return this->x;
}
int getY()
{
return this->y;
}
int getZ()
{
return this->z;
}
void setX(int xcs)
{
this->x = xcs;
}
void setY(int igrec)
{
this->y = igrec;
}
void setZ(int zet)
{
this->z = zet;
}
};
class AbstractGeometricFigure
{
string Figuretype;
bool is2D;
int nbcorners;
Point* Corners;
public:
virtual int area() = 0;
virtual int perimeter() = 0;
void getCorners() {
for (int i = 0; i < nbcorners; ++i) {
cout << "(" << Corners[i].getX() << ", " << Corners[i].getY();
if (is2D) {
cout << ")\n";
}
else {
cout << ", " << Corners[i].getZ() << ")\n";
}
}
}
void addCorner(int newcorner)
{
if (Corners!=NULL)
{
Point* varfuri = new Point[nbcorners];
}
}
};
But if you have the intention to explain to me what happens when I have a dynamic array o type class, class Point,and in that class I must not implement the default constructor. What should I do when I have an exercise like this at the exam? Update: but I am still waiting for someone to explain to me the answer to my questions.
I was also thinking about implementing it this way:
void addCorner(Point newcorner)
{
if (Corners!=NULL)
{
Corners[nbcorners] = newcorner;
}
}