Let’s look at the string constructors. After all, one of the most important things to know about a class is what your options are when creating objects of that class. Listing 16.1 uses seven of the string constructors (labeled ctor, the traditional C++ abbreviation for constructor). Table 16.1 briefly describes the constructors. The table begins with the seven constructors used in Listing 16.1, in that order. It also lists a couple of C++11 additions. The constructor representations are simplified in that they conceal the fact that string really is a typedef for a template specialization basic_string and that they omit an optional argument relating to memory management. (This aspect is discussed later this chapter and in Appendix F, “The string Template Class.”) The type size_type is an implementation-dependent integral type defined in the string header file. The class defines string::npos as the maximum possible length of the string. Typically, this would equal the maximum value of an unsigned int. Also the table uses the common abbreviation NBTS for null-byte-terminated string—that is, the traditional C string, which is terminated with a null character.

Table 16.1. string Class Constructors

Listing 16.1. str1.cpp

// str1.cpp -- introducing the string class

#include

#include

// using string constructors

int main()

{

    using namespace std;

    string one("Lottery Winner!");     // ctor #1

    cout << one << endl;               // overloaded <<

    string two(20, '$');               // ctor #2

    cout << two << endl;

    string three(one);                 // ctor #3

    cout << three << endl;

    one += " Oops!";                   // overloaded +=

    cout << one << endl;

    two = "Sorry! That was ";

    three[0] = 'P';

    string four;                       // ctor #4

    four = two + three;                // overloaded +, =

    cout << four << endl;

    char alls[] = "All's well that ends well";

    string five(alls,20);              // ctor #5

    cout << five << "!\n";

    string six(alls+6, alls + 10);     // ctor #6

    cout << six  << ", ";

    string seven(&five[6], &five[10]); // ctor #6 again

    cout << seven << "...\n";

    string eight(four, 7, 16);         // ctor #7

    cout << eight << " in motion!" << endl;

    return 0;

}

The program in Listing 16.1 also uses the overloaded += operator, which appends one string to another, the overloaded = operator for assigning one string to another, the overloaded << operator for displaying a string object, and the overloaded [] operator for accessing an individual character in a string.

Here is the output of the program in Listing 16.1:

Lottery Winner!

$$$$$$$$$$$$$$$$$$$$

Lottery Winner!

Lottery Winner! Oops!

Sorry! That was Pottery Winner!

All's well that ends!

well, well...

That was Pottery in motion!

Program Notes

The start of the program in Listing 16.1 illustrates that you can initialize a string object to a regular C-style string and display it by using the overloaded << operator:

string one("Lottery Winner!");    // ctor #1

cout << one << endl;              // overloaded <<

The next constructor initializes the string object two to a string consisting of 20 $ characters:

string two(20, '$');              // ctor #2

The copy constructor initializes the string object three to the string object one:

string three(one);                // ctor #3

The overloaded += operator appends the string " Oops!" to the string one:

one += " Oops!";                  // overloaded +=

This particular example appends a C-style string to a string object. However, the += operator is multiply overloaded so that you can also append string objects and single characters:

one += two;   // append a string object (not in program)

one += '!';   // append a type char value (not in program)

Similarly, the = operator is overloaded so that you can assign a string object to a string object, a C-style string to a string object, or a simple char value to a string object:

two = "Sorry! That was "; // assign a C-style string

two = one;                // assign a string object (not in program)

two = '?';                // assign a char value (not in program)

Overloading the [] operator, as the String example in Chapter 12 does, permits access to individual characters in a string object by using array notation:

three[0] = 'P';

A default constructor creates an empty string that can later be given a value:

string four;                      // ctor #4

four = two + three;               // overloaded +, =

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги