Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose we have a C++ class called student. There are many ways one can create a

ID: 3821736 • Letter: S

Question

Suppose we have a C++ class called student. There are many ways one can create a collection student objects. Among these: one can use a normal C++ array or an STL vector, and one could create a collection of objects, or of pointers to objects (presumably allocated on the heap.) a. Write a line of C++ code that declares an array a that can hold 10 objects of type Student b. Write a line of C++ code that declares an array b that can hold 10 pointers to objects of type student. c. Write a line of C++ code that declares an STL vector c that can hold 10 objects of type student d. Write a line of C++ code that declares an STL vector d that can hold 10 pointers to objects of type student

Explanation / Answer

As asked in the question I am giving line of the code to implement collection and some explanation below: -

a) Array of objects of type Student

Student a[10];

this statement have Student has its data type and created an array of 10 objects of Student type .Each element of this array can access properties and methods of the Class Student.

b) Student* b[10];

this statement creates an array b which have 10 elements, with each element having a pointer pointing to different Student object.

Difference between the above two implementations are that in first statement, we are saving the object itself as an element of array, and in second we are saving a reference or a pointer to object's location in the array.

c) vector<Student> c(10);

vector is a template class just like an array with some extra functions like taking care of allocated memory etc. Here we declare a vector containing 10 objects of Student class.

d) vector<Student>* d(10);

Like array of pointers, this vector d holds pointer to 10 elements in the array.