Containment and memory management in classes

I have an assigment I am supposed to do and I am completely lost my teacher went very fast through this section and I am still very confused. What I would like is not for someone to write the code for me but explain what I should do and possibly explain what containment and memory management in classes is exactly.

class Line
{
private:
char* letters;
public:
Line();
char* getLine()
char getCharAt(int index);
void setLine(char *line);
};
class Page
{
private :
Line* lines;
public:
Page();
Line getLineAt(int index);
void setLineAt(int index,Line line);
};
class Book
{
private:
Page* pages;
public:
Book();
Page getPageAt(int index);
void setPageAt(int index, Page p);
};

int main()
{
//Add all code here

The questions are stated below I am confused as to how they differ
maybe show some code and an explanation as to why, anything will help

1. If the max number of characters per line is 255, write the default Constructor for the Line class. Write the Copy Constructor for the Line Class. Write the Destructor for the Line class. Write the statement in main (where Add all code here is found) that will create a Line object name “line”. Write the statement that will initialize line to “The brown fox ran thru the hollow log”.

2. If the max number of lines per page is 64, write the default Constructor for the Page class. Write the Copy Constructor for the Page Class. Write the Destructor for the Page Class. Write the statement in main (beneath your declaration/initialization for Line) that will create a Page object named “page”. Write the statement that will initialize all 64 lines of page to the same line created in question 2.


3. If the max number of pages per book is 1000, write the default Constructor for the Book class. Write the Copy Constructor for the Book Class. Write the Destructor for the Book Class. Write the statement in main (beneath your declaration/initialization for Page) that will create a Book object named “book”. Write the statement that will initialize the 1st 200 pages of book to the same page created in 3.

4. Write the statement(s) beneath 4 that will display the 17th letter of the 49th line on the 173rd page of book.

5. Write the statement(s) that will set the 18th line of the 73rd page of book to “Hello world!”;

6. Write the statement that will display the 39th line of the 123rd page of book.


For 1.

Default constructor may look like this:

Note that you must prepend each functions with Line:: if you want to define them outside the class

1
2
3
Line::Line() : letters{new char[255]} // Allocating the max number
{
}


The copy constructor:

1
2
3
4
Line::Line(const Line &l) : Line() // The default constructor is called in order to create the buffer
{
  strcpy(letters, l.letters); // Now the content of the provided Line is copied
}


The destructor:

1
2
3
4
Line::~Line()
{
  delete[] letters
}


The definitions for the other classes are more or less the same.
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
Last edited on
Topic archived. No new replies allowed.