Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class StudentArrayV3 { // version 3: automatic array of object pointers public: StudentArrayV3( ); ~StudentArrayV3( ); void add( ); void write( ); const static int

class StudentArrayV3 { // version 3: automatic array of object pointers public: StudentArrayV3( ); ~StudentArrayV3( ); void add( ); void write( ); const static int max_array_size = 1000; private: Student* members[max_array_size]; int number_of_students = 0; };

When memory is set aside for an object of this type, no constructor calls are made for Student objects as no such objects are part of the class definition; the private data array members does however contain pointers to such objects. Because of this, the size of the array can be made arbitrarily large (1000) and let the number of meaningful pointers in the object be maintained by the counter number_of_students.

Consider the definition for a constructor of the class. It is obligated to initialize the private data of a StudentArrayV3 object with meaningful data. Put the implementation for the constructor of the class in the file StudentArrayV3.cpp. Explain the values given to the private data (one integer and one thousand pointers) in comments in the code. Do all of the pointers in the members array need to be initialized to meet the obligation of a constructor?

In this version, we need to create an add method; this was not necessary in the previous version of the array class since constructors were called for each Student in the array as soon as memory was allocated for the array. In other words, all adding took place at the time of array construction. Arrange for add to put the address of a new Student in the next available spot in the members array; be sure to update the number of students now stored in the array. Put the add implementation in file StudentArrayV3.cpp.

A destructor is again necessary (as the logical and physical objects of the class are not the same). In this particular situation, logical minus physical may consist of up to one thousand Student objects (as pointed to by the members array pointers). How will the destructor know how many there are and where the pointers to them are stored? Put the implementation of the destructor for the class in the file StudentArrayV3.cpp.

There is a significant difference in the write method from those of the first two versions of the array. Methods StudentArrayV1::write and StudentArrayV2::write simply invoke Student::write for each element of the members array. In this version, the members array contains pointers to Student objects, not Student objects themselves. The first of these pointers is members[0]. The dereference operator * can be used to follow the arrow from this pointer to the object; *members[0] is then the first Student object. The selector operator . (the period character) is used to invoke the Student::write method for the object located by the pointer. Since the selector operator has precedence higher than that of the dereference operator, parentheses must be used; thus (*members[0]).write() will perform the desired task. This rather clumsy notation may alternatively be expressed with the -> operator as members[0]->write(); both mean exactly the same thing in C++. Put the write implementation in file StudentArrayV3.cpp.

Test this version of the array class with the following addition to function main.

need help with the cpp file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions