[cs13001] Lab 11

Mikhail Nesterenko mikhail at cs.kent.edu
Thu Apr 4 22:17:20 EDT 2013


> I was wondering what the void constructor is supposed to do in lab 11. What
> are we initializing in this? I know usually you set things to -1 but I
> wasn't sure what.

The void constructor initializes the object to the state of "no
elements".

Variable "size" should be set to zero. Now, dArray may be initialized
to NULL. However in this case, you will have to handle this special
case in member functions such as addNumber().

That is, you have to specifically code the case when the array is
empty (pointer points to NULL) and you are adding the first number to
it.

A simple, and more elegant solution, is to have this in the void
constructor:

 dArray=new int[0];

That is, you are requesting to allocate an array of zero elements. It
is guaranteed to work. Moreover, in all the other member functions,
such as addNumber(), there is no need for a special case of zero
elements i.e. the case of zero elements is processed the same way as as
any other number of elements.

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list