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

Implement ostream operator << as a friend C++ /*The overloaded stream operator f

ID: 3594752 • Letter: I

Question

Implement ostream operator << as a friend C++

/*The overloaded stream operator for the DynamicArray class. If

an array object is printed that contains the elements a,c,b and m,

with element 'a' at index 0 and element 'm' at index 3 (first to last),

the output MUST be in the following format:

[a,c,b,m]

with no white space or new lines.

It is possible that some of the elements might be null. If this is the

case then the null elements should be indicated with asterisks.

If the array contains the elements a and m,

with element 'a' at index 0 and element 'm' at index 3 (first to last),

the output MUST be in the following format:

[a,*,*,m]

NOTE: if all elements are null, output asterisks. If the array is of size 4,

but containts no elements yet, the output MUST be in the following format:

[*,*,*,*]

*/

friend ostream& operator<< <T>(ostream&,DynamicArray<T>&);

Explanation / Answer


friend ostream& operator<< <T>(ostream& out,DynamicArray<T>& darr) {
int size = darr.getCapacity();
if (size == 0) out << "[]";
if (darr[0] == NULL)
out << "[*";
else
out << "[*" << darr[0];
for (int i = 1; i < size; i++) {
if (darr[i] == NULL) {
out << ",*;
}
else {
out << ","<< darr[i];
}
}
out << "]";
}

This assumes that there are two method already implemented getCapacity() to get capacity of the dynamic array and overloaded operator [] to get element at given index

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote