explain what containment and memory management in classes is exactly |
from Chapter 14, C++ Primer Plus (5th ed) by Stephen Prata -
... class members that are themselves objects of another class. This is referred to as containment or composition or layering. Another option is to use private or protected inheritance. Containment, private inheritance, and protected inheritance are typically used to implement has-a relationships—that is, relationships for which the new class has an object of another class. |
So, in your example a Book has Pages which has Lines ...
I'd recommend strongly that you take a look at this book/chapter for a comprehensive treatment of this topic with illustrative examples.
Re memory management, a crude definition might be whenever a class acquires resources through it's ctor (viz by use of the new operator && C-style raw pointers) it has the responsibility to release these resources through it's destructor and so needs to implement the dtor as well. But this is not sufficient, since objects may be copied or assigned, it implies that the copy ctor and copy assignment operators need to be implemented as well, unless they are deleted. also, there might be additional requirements to implement the move ctor and move assignment operator since C++11. i can't think of a single, stand-alone reference for this topic but some of the google search terms might be:
"using new in constructor C++", "rule of three (or five) C++" etc
edit: also, to mention, use of some of the standard library containers like std::vector instead of c-style arrays, std::string instead of c-style strings and (judicious) use of smart pointers instead of c-style raw pointers might make the program much easier to handle vis-a-vis memory management, algorithms, iterator etc