Buffered Device Deleted Through a Base Pointer
hardQuestion 713Write 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
- int id() const - the stored id.
- virtual string label() 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 with new[], every element 0.
- ~Buffered() - releases the array, then prints the line: buffer freed
- string label() const - returns "buffered#", the id, "x", the capacity, e.g. buffered#5x4.
- int capacity() const - the number of ints in the array.
- void set(int index, int value) and int get(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, so print one after the last line.
Your output
Run your code to see the output here...