Garmaine Staff asked 2 years ago

Consider the following code

class Shape
{
protected:
    int length, height;
public:
    Shape();
    ~Shape();
};

class Square : Shape
{
private:
    double Area;

public:
    Square();
    ~Square();
};

class Circle : Shape
{
private:
    double Circumference;

public:
    Circle();
    ~Circle();
};
int main() 
{

    Shape *shape[5];
    int choice, i = 0;

    cout << "Which shape are you making?\n";
    cout << "1. Square\n";
    cout << "2. Circle\n";
    cin >> choice;

    if (choice == 1)
    {
        shape[i] = new Square();
        i++;
    }

    if (choice == 2)
    {
        shape[i] = new Circle();
        i++;
    }


}

How would I make an array of pointers that contain both Circle and Squares so I can easily access both later to do stuff with it? Currently, it is giving me an error in the shape[i] = new Square(); and shape[i] = new Circle(); in main() and I don't know how to create an array of pointers to inherited classes.