← Back to Pointers
Question 733
Roster Deep Copy of an Array of Player Pointers
Roster Deep Copy of an Array of Player Pointers
Playerand most ofRosterare written for you.m_playersis a pointer to an ARRAY OF POINTERS: the array itself was made withnew Player*[n], and every element holds the address of a separatePlayermade withnew. A deep copy therefore needs a new array AND a newPlayerfor every slot, and a release needs onedeleteper slot and then a singledelete[]for the array (see the destructor). Write the copy constructor and the copy assignment operator: -Roster(const Roster& other): copy the team name and the count, allocate a new pointer array, and in a loop store a newPlayercopied from each of the other roster's players -Roster& operator=(const Roster& other): do nothing on self-assignment; otherwise delete every player this roster owns and its array, then do exactly what the copy constructor does, and return*thisWrite every loop inside the special member that needs it; do not add private helper functions. The driver reads a team name,nandnlines ofnumber name(the name may contain spaces), then one extranumber name. It copy-constructs a second roster, renumbers the copy's first player to 99 and adds the extra player to the copy, prints both; assigns the original into a roster namedReservesthat already holds the extra player, renumbers that one's first player to 7 and prints the original and it; then assigns the original to itself through a reference and prints it. For the input: ``Hawks 2 10 Ada Lovelace 23 Grace Hopper 50 Sub`` the exact output is what is shown in Expected Output. Free the memory you allocate — this question checks for memory leaks. Do NOT write a main function.
Expected Output:
Hawks (2 players) #10 Ada Lovelace #23 Grace Hopper Hawks (3 players) #99 Ada Lovelace #23 Grace Hopper #50 Sub Hawks (2 players) #10 Ada Lovelace #23 Grace Hopper Hawks (2 players) #7 Ada Lovelace #23 Grace Hopper Hawks (2 players) #10 Ada Lovelace #23 Grace Hopper
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.