29: String (int); // private constructor
30: char * itsString;
31: unsigned short itsLen;
32: };
33:
34: // default constructor creates string of 0 bytes
35: String::String()
36: {
37: itsString = new char[1];
38: itsString[0] = ‘\0’;
39: itsLen=0;
40: // cout << “\tDefault string constructor” << endl;
41: // ConstructorCount++;
42: }
43:
44: // private (helper) constructor, used only by
45: // class methods for creating a new string of
46: // required size. Null filled.
47: String::String(int len)
48: {
49: itsString = new char[len+1];
50: for (int i = 0; i<=len; i++)
51: itsString[i] = ‘\0’;
52: itsLen=len;
53: // cout << “\tString(int) constructor” << endl;
54: // ConstructorCount++;
55: }
56:
57: // Converts a character array to a String
58: String::String(const char * const cString)
59: {
60: itsLen = strlen(cString);
61: itsString = new char[itsLen+1];
62: for (int i = 0; i<itsLen; i++)
63: itsString[i] = cString[i];
64: itsString[itsLen]=’\0’;
65: // cout << “\tString(char*) constructor” << endl;
66: // ConstructorCount++;
67: }
68:
69: // copy constructor
70: String::String (const String & rhs)
71: {
72: itsLen=rhs.GetLen();
73: itsString = new char[itsLen+1];
74: for (int i = 0; i<itsLen;i++)
75: itsString[i] = rhs[i];
76: itsString[itsLen] = ‘\0’;
77: // cout << “\tString(String&) constructor” << endl;
78: // ConstructorCount++;
582 Day 16
LISTING16.8 continued