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
}
std::vectoris what one would use and one would fill it with smart pointers in most cases, not raw pointers.