CJCoding With Joseph
15free / daySign inCreate account

Roster Deep Copy of an Array of Player Pointers

Player and most of Roster are written for you. m_players is a pointer to an ARRAY OF POINTERS: the array itself was made with new Player*[n], and every element holds the address of a separate Player made with new. A deep copy therefore needs a new array AND a new Player for every slot, and a release needs one delete per slot and then a single delete[] 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 new Player copied 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 *this

Write every loop inside the special member that needs it; do not add private helper functions.

The driver reads a team name, n and n lines of number name (the name may contain spaces), then one extra number 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 named Reserves that 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
Topics:
Pointers
📥 Auto-Input:
Hawks\n2\n10 Ada Lovelace\n23 Grace Hopper\n50 Sub\n
This input is automatically fed to your program's stdin
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.