← Back to Pointers
Question 735
Portfolio Rule of Three With Two Resources
Portfolio Rule of Three With Two Resources
Holdingis written for you. Write the classPortfolio, which owns TWO heap resources through two simple pointers:m_ownerpoints at a heapchararray holding the owner's name as a C-string, andm_holdingspoints at a heap array ofHoldingstructs. Each is one block, released with its owndelete[]. Private members:char* m_owner;,Holding* m_holdings;andsize_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 ownstrcpyand their own copying loop. The driver reads the owner,nandnlines ofticker shares, then one extraticker shares. It copy-constructs a second portfolio and adds the extra holding to the COPY only, prints both, assigns the original into a portfolio namedTempthat 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 — 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.