← Back to Pointers
Question 713
Buffered Device Deleted Through a Base Pointer
Buffered Device Deleted Through a Base Pointer
Write two classes. Do NOT write a main function; a hidden test provides one. class Resource (the base): - Resource(int id) - stores the id. - virtual ~Resource() - prints the line: resource closed - intid()const - the stored id. - virtual stringlabel()const - returns "resource#" followed by the id, e.g. resource#3. class Buffered : public Resource: - Buffered(int id, int capacity) - passes the id up to Resource and allocates capacity ints withnew[], every element 0. - ~Buffered() - releases the array, then prints the line: buffer freed - stringlabel()const - returns "buffered#", the id, "x", the capacity, e.g. buffered#5x4. - intcapacity()const - the number of ints in the array. - voidset(int index, int value)and intget(int index)const. The test deletes a Buffered through a Resource* pointer, so ~Resource MUST be virtual. Free the memory you allocate - this question checks for memory leaks. The hidden test reads an id for a plain Resource, an id for a Buffered, and the Buffered's capacity. It creates and deletes a plain Resource through a Resource*, then creates a Buffered through a Resource* and prints its label, writes 42 at index 0 and 7 at the last index and prints those two values plus the capacity, prints the id through the base pointer, deletes through the base pointer, and prints done. For the input: 3 5 4 your class must produce exactly the output shown in Expected Output.
Expected Output:
resource#3 resource closed buffered#5x4 42 7 4 5 buffer freed resource closed done
= 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.