【发布时间】:2020-04-10 16:31:59
【问题描述】:
考虑下面的代码
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++;
}
}
如何创建一个包含 Circle 和 Squares 的指针数组,以便我以后可以轻松访问这两个指针来处理它?目前,它在 main() 中的 shape[i] = new Square(); 和 shape[i] = new Circle(); 中给我一个错误,我不知道如何创建一个指向继承类的指针数组。
【问题讨论】: