Review

We've almost finished building our own version of a concrete data type called string, which provides a means of storing and processing a group of characters in a more convenient form than a C string. The fact that string is a concrete data type means that a string that is defined as a local variable in a block should be created when the code in the block begins execution and automatically destroyed when the block ends. Another requirement for a concrete data type is to be able to copy one variable of that type to another variable of the same type and have the two copies behave like independent variables, not linked together in the manner of Siamese twins.

As we've previously noted, the creation of an object is performed by a special member function called a constructor. Any class can have several constructors, one for each possible way that a newly created object can be initialized. So far, we've examined the interface and implementation of the default constructor, which takes no arguments, and a constructor that takes a char* argument. The former is needed to create a string that doesn't have a specified initial value, while the latter allows us to create a string that has the same contents as a C string. The default constructor is one of the required member functions in a concrete data type.

We've also seen that in the case of our string constructors, we need to know the order in which the member initialization expressions are executed. Since this is dependent on the order of declaration of member variables, we have to make sure that those member variables are declared in the correct order for our member initialization expressions to work properly.

Continuing with the requirements for a concrete data type, we've implemented our own version of operator =, which can set one string to the same value as another string while leaving them independent.

We've also created one other required member function for a concrete data type, the destructor, which is used to clean up after a string when it expires. This function is called automatically for an auto variable at the end of the block where the variable is defined.

We're still short a copy constructor, which can create a string that has the same value as another pre-existing string. This may sound just like operator =, but it's not exactly the same. While operator = is used to set a string that already exists to the same value as another extant string, the copy constructor creates a brand-new string with the same value as one that already exists. We'll see how this works in the next chapter; in the meantime, let's take a look at some exercises intended to test your understanding of this material.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset