Advanced Inheritance 539
16
18: String operator+(const String&);
19: void operator+=(const String&);
20: String & operator= (const String &);
21:
22: // General accessors
23: int GetLen()const { return itsLen; }
24: const char * GetString() const { return itsString; }
25: // static int ConstructorCount;
26:
27: private:
28: String (int); // private constructor
29: char * itsString;
30: unsigned short itsLen;
31:
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\n”;
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\n”;
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\n”;
66: // ConstructorCount++;
67: }
68:
LISTING16.1 continued