You will find that you often want to allocate memory for class data members dynamically. You
can use the operator new in a constructor to allocate memory for an object member. In such a case, you must assume responsibility for releasing the memory when the object is no longer required by providing a suitable destructor. Let’s first define a simple class where we can do this. Suppose you want to define a class where each object is a message of some description — for example, a text string. The class should be as memory-efficient as possible, so, rather than defining a data member as a char array big enough to hold the maximum length string that you might require, you’ll allocate memory in the free store for the message when an object is created. Here’s the class definition: