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