← Back to Pointers
Question 700
Printer Deletes Its Own Cartridge
Printer Deletes Its Own Cartridge
Write TWO classes.Printerowns aCartridgethat it creates on the heap in its constructor.Cartridge(write it first): ``Cartridge(int ink); // private member: int inkLevel int getInk() const; void use(int amount); // subtract amount from inkLevel`Printerprivate members:Cartridge* cartridge;andint pagesPrinted;. Public interface:`Printer(int startingInk); // cartridge = new Cartridge(startingInk); pagesPrinted = 0 ~Printer(); // the cartridge is owned here - release it void printPages(int pages); int inkLeft() const; // the ink still in the cartridge int pages() const; // pages actually printed, over all calls`printPages(int pages)prints up to that many pages at 2 ink each. If the cartridge has less than 2 ink left, printing STOPS and the remaining pages are skipped. Every page printed adds 1 topagesPrinted. The driver readsstartingInk firstRequest secondRequest. For the input:`10 3 4`` 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:
pages=5 ink=0
= 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.