Question
class StudentArrayV4 { // version 4: dynamic array of object pointers public: StudentArrayV4( ); ~StudentArrayV4( ); void add( ); void write( ); private: Student** members
class StudentArrayV4 { // version 4: dynamic array of object pointers public: StudentArrayV4( ); ~StudentArrayV4( ); void add( ); void write( ); private: Student** members = nullptr; int number_of_students = 0; int physicalArraySize = 0; };
dynamic memory can be used to allocate Student objects as needed, why not use it to allocate the array of Student pointers to be of an appropriate size as well? This would make more efficient use of storage as 1000 may be excessively large; further, an unexpected class size of more than 1000 might be encountered. This can be accomplished with the following class definition.
A constructor for this class can allocate a small array for the Student pointers and save the number as physicalArraySize; the value for number_of_students is initially zero. Write this constructor now; let the initial physical array size be three.
Students may be dynamically allocated and added to the array until the physical size limit is reached. At that point, a new pointer array of larger size is allocated, the pointers from the existing array are copied into it, and the existing array is then removed from memory. The physical size limit is increased accordingly, allowing successive additions to be made without additional work until the new physical size is reached. Write method add so that new array allocations are larger by three each time.
The destructor for this class will have to carefully free the memory we allocated. Remember that each Student in the array has been dynamically-allocated, so they must be freed. Additionally, the array itself was dynamically-allocated, so it must be freed (but only after freeing all students that it points to).
Test this new class with an addition to function main analogous to that used for the third version of the class.
help me write the Cpp file
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started