以下のプログラムに関して2つの質問があります。1。プログラムは要素(長方形と六角形のタイプ)のみを動的として作成しますか、それともそれらへのポインターも動的ですか?
2.プログラムの最後に削除がない理由。たとえば、次のようなものです:(要素のみが動的であると正しく仮定した場合..)
for(i=0;i<3;i++)
delete shapeArray[i];
どうもありがとうございました、このウェブサイトは私の先生が助けることができないことで私を大いに助けてくれます!シラン
プログラムは次のとおりです。
int main()
{
// Create array of pointers to Shapes of various types.
const int NUM_SHAPES = 3;
Shape * shapeArray[] = { new Hexagon(),
new Rectangle(),
new Hexagon()
};
// Set positions of all the shapes.
int posX = 5, posY = 15;
for (int k = 0; k < NUM_SHAPES; k++)
{
shapeArray[k]->setPosition(posX, posY);
posX += 10;
posY += 10;
};
// Draw all the shapes at their positions.
for (int j = 0; j < NUM_SHAPES; j++)
{
shapeArray[j]->draw();
}
return 0;
}