CJCoding With Joseph
15free / daySign inCreate account

Printer Deletes Its Own Cartridge

Write TWO classes. Printer owns a Cartridge that 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
`

Printer private members: Cartridge* cartridge; and int 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 to pagesPrinted.

The driver reads startingInk 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
Topics:
Pointers
📥 Auto-Input:
10 3 4\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.