CharBuffer Rule of Three With a Self-Assignment Guard
hardQuestion 710Write a class CharBuffer that owns a C-style string on the heap. Do NOT write a main function; a hidden test provides one.
Required members:
- CharBuffer(const char* text) - measure text with strlen, allocate that many characters PLUS ONE for the terminating null with new char[], and copy the text in.
- CharBuffer(const CharBuffer& other) - a DEEP copy: its own fresh array holding the same characters.
- CharBuffer& operator=(const CharBuffer& other) - guard self-assignment first, release the old array, allocate a new one, copy the characters in, and return *this.
- ~CharBuffer() - releases the array.
- const char* c_str() const - the stored characters.
- int length() const - characters stored, not counting the terminating null.
- void setAt(int index, char ch) - overwrites one character.
Free the memory you allocate - this question checks for memory leaks.
The hidden test reads two words. It builds a CharBuffer from the first, copy-constructs a second from it and sets the copy's character 0 to 'Z', builds a third from the second word, assigns the first into the third and sets its character 1 to 'Q', and finally assigns the third to itself.
For the input: hello world
your class must produce exactly the output shown in Expected Output.
Expected output
hello Zello 5 5 hello hQllo hQllo 5
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...