7. Ownership and Lifetime Of Dynamic Variables
Activity 7: Storing the Words of a Book Using Dynamic Variables
Solution:
- Start with the skeleton
main()
program. It might look like this:#include <iostream> #include <memory> using namespace std; int main() { return 0; }
- Define the
word
class:class word { friend class line; unique_ptr<char[]> ptr_; int letters_; int spaces_; word* next_; public: word(char const* srcp, int l, int spaces); void to_string(char* dstp); int size(); };// end word
There is a
unique_ptr<>
tochar
array that holds the letters of the word, and a count of letters and spaces. Finally, since the words in a line are going to be a linked list, there is a next pointer.The constructor copies in the word string and the...