CJCoding With Joseph
15free / daySign inCreate account

Holding is written for you. Write the class Portfolio, which owns TWO heap resources through two simple pointers: m_owner points at a heap char array holding the owner's name as a C-string, and m_holdings points at a heap array of Holding structs. Each is one block, released with its own delete[].

Private members: char* m_owner;, Holding* m_holdings; and size_t m_count;

Public interface used by the hidden driver:

Portfolio(const char* owner);                  // copy owner into a new char array (strlen + 1), no holdings yet
Portfolio(const Portfolio& other);             // deep copy of BOTH resources
Portfolio& operator=(const Portfolio& other);  // guard self-assignment, release both old blocks, deep copy both
~Portfolio();                                  // release both
Portfolio& operator+=(const Holding& holding); // append a copy of holding (allocate one slot more, copy, release the old array)
void print() const;                            // "OWNER (N holdings)" then one line per holding: two spaces, the ticker, a space, x and the shares

Write every loop inside the special member that needs it; do not add private helper functions. Both the copy constructor and the assignment operator contain their own strcpy and their own copying loop.

The driver reads the owner, n and n lines of ticker shares, then one extra ticker shares. It copy-constructs a second portfolio and adds the extra holding to the COPY only, prints both, assigns the original into a portfolio named Temp that already holds the extra holding and prints it, then assigns the original to itself through a reference and prints it.

For the input:

Ana
2
AAPL 10
SHOP 3
MSFT 5

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

Ana (2 holdings)
  AAPL x10
  SHOP x3
Ana (3 holdings)
  AAPL x10
  SHOP x3
  MSFT x5
Ana (2 holdings)
  AAPL x10
  SHOP x3
Ana (2 holdings)
  AAPL x10
  SHOP x3

= newline. The output ends with a newline, so print one after the last line.

Hints4 available
Topics
Pointers
Auto-input
Ana\n2\nAAPL 10\nSHOP 3\nMSFT 5\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.