简体   繁体   中英

How to create an array of pointers to object of a inherit class

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.

You need to specify what kind of inheritance you want; More specifically, you need to tell the compiler that it is a public inheritance, meaning, that your entire code-base can know that Circle and Square are Shape .

Just change the declaration of the classes into:

class Circle: public Shape

class Square: public Shape

Also, consider having Shape as a true virtual interface, meaning - it should have nothing other than the public API that each shape should have, for example:

class IShape
{
public:
    virtual double get_area() const = 0;
    virtual double get_perimeter() const = 0;
    // and so on...

    virtual ~IShape() = 0;

protected:
    virtual double calculate_something() const = 0;
    // also protected can be here
};

Also, notice that using raw arrays, and especially raw arrays of raw pointers, such as:

Shape *shape[5];

is a good way to cause memory leak in your program; You should use both std::array and std::unique_ptr<IShape> in this context, meaning, your main should look like:

    std::array<std::unique_ptr<IShape>, 5> shape;
    int choice = 1;
    int i = 0;

    if (choice == 1)
    {
        shape[i] = std::unique_ptr<IShape>(new Square());
        i++;
    }

    if (choice == 2)
    {
        shape[i] = std::unique_ptr<IShape>(new Circle());
        i++;
    }

It is because you are specifying the inheritance without telling whether it is public, private or protected. By default c++ will consider it private so you are not allowed to access a private class. change the inheritance from private to public and you are good to go. something like this

class Square :public Shape
{
private:
    double Area;

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

class Circle :public Shape
{
private:
    double Circumference;

public:
    Circle();
    ~Circle();
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM