Question:
Create an array of at least four pointers to Reader objects. Use the New operator to create at least four pointers to derived class objects and assign them to the array.
I'm not sure If I did it right or not.
Reader is the base class. John, David, Daniel, Mark are the derived class
int main(void)
{
Reader *obj[4];
obj[0] = new John();
obj[1] = new David();
obj[3] = new Daniel();
o开发者_开发问答bj[2] = new Mark();
}
Would this be right???
Your code is correct.
And as @sharptooth suggested, make a practice of delete
on the allocated obj[]
s. In C++ new
allocates memory and delete
deallocates.
精彩评论