My task specifically states that I have to create a random array of Squares and Triangles, that are inherited from an abstract class Figure, and then I have to print out their square area. Coming from C#, I thought I'd be off with an array of objects, but they do not exist in C++. I'm not allowed to use anything like vectors. Making a dynamic array of Figure doesn't work because apparently it never works with abstract classes. What should I do? Please, keep it simplified if possible.
Here's my current code. Very basic, but it's here just to show what I'm trying to do.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Figure
{
public:
virtual double square() = 0;
};
class Square : public Figure
{
public:
double side;
double square()
{
return side * side;
}
};
class Triangle : public Figure
{
public:
double height;
double side;
double square()
{
return 0.5 * side * height;
}
};
void main()
{
int size = 20;
Figure *dyn_arr = new Figure[size]; // this doesn't work
//Also I have to fill it somehow too...
for (int i = 0; i < size; i++) cout << Figure.square(); //this doesn't work either
}
Firstly
main
must returnint
. Then you need to create an array of pointers which will show to abstract classFigure
.Figure **dyn_arr = new Figure*[size];
Then if you want to add a new object of the derived class you are adding simply like this.
dyn_arr[0] = new Triangle();
--> this will create new object which will return the address of that object, and your array is actually array of pointers.Finnaly if you want to call the function
square
from any class you can do that like this.dyn_arr[0]->square();
p.s. If you don't have at least a little experience with pointers this can be confusing.
You can create a list of generated functions to select from, to create dynamically allocated objects. Using a rng you can then randomly select from the list of generated functions to call, in order to fill out an array of
unique_ptr<Figure>
s.Example using
std::deque
instead ofstd::vector
:It doesn't work because we cannot create an array of
Figure
s because of the pure virtual function. We wouldn't want to create aFigure
by itself in any case. We only want to create either aSquare
or aTriangle
and their sizes may differ. So there is no way to allocate the memory when the array is created.You could create an array of pointers (size is known at compile time) and then iterate over that:
Please keep in mind that arrays and pointers used this way is prone to errors. Far better to use
std::make_unique
andstd::vector
.Here is one way to randomly create the objects: