← Back to Pointers
Question 710
CharBuffer Rule of Three With a Self-Assignment Guard
CharBuffer Rule of Three With a Self-Assignment Guard
Write 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 newchar[], 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. - intlength()const - characters stored, not counting the terminating null. - voidsetAt(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 — 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.