string.h
1 // Listing 16.1 The String Class
2 #include <iostream>
3 #include <string.h>
4 using namespace std;
5 class String
6 {
7 public:
8 // constructors
9 String();
10 String(const char *const);
11 String(const String &);
12 ~String();
13
14 // overloaded operators
15 char & operator[](int offset);
16 char operator[](int offset) const;
17 String operator+(const String&);
18 void operator+=(const String&);
19 String & operator= (const String &);
20
21 // General accessors
22 int GetLen()const { return itsLen; }
23 const char * GetString() const { return itsString; }
24 static int ConstructorCount;
25
26 private:
27 String (int); // private constructor
28 char * itsString;
29 unsigned short itsLen;
30
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 cout << "\tDefault string constructor\n";
40 ConstructorCount++;
41 }
42
43 // private (helper) constructor, used only by
44 // class methods for creating a new string of
45 // required size. Null filled.
46 String::String(int len)
47 {
48 itsString = new char[len+1];
49 for (int i = 0; i<=len; i++)
50 itsString[i] = '\0';
51 itsLen=len;
52 cout << "\tString(int) constructor\n";
53 ConstructorCount++;
54 }
55
56 // Converts a character array to a String
57 String::String(const char * const cString)
58 {
59 itsLen = strlen(cString);
60 itsString = new char[itsLen+1];
61 for (int i = 0; i<itsLen; i++)
62 itsString[i] = cString[i];
63 itsString[itsLen]='\0';
64 cout << "\tString(char*) constructor\n";
65 ConstructorCount++;
66 }
67
68 // copy constructor
69 String::String (const String & rhs)
70 {
71 itsLen=rhs.GetLen();
72 itsString = new char[itsLen+1];
73 for (int i = 0; i<itsLen;i++)
74 itsString[i] = rhs[i];
75 itsString[itsLen] = '\0';
76 cout << "\tString(String&) constructor\n";
77 ConstructorCount++;
78 }
79
80 // destructor, frees allocated memory
81 String::~String ()
82 {
83 delete [] itsString;
84 itsLen = 0;
85 cout << "\tString destructor\n";
86 }
87
88 // operator equals, frees existing memory
89 // then copies string and size
90 String& String::operator=(const String & rhs)
91 {
92 if (this == &rhs)
93 return *this;
94 delete [] itsString;
95 itsLen=rhs.GetLen();
96 itsString = new char[itsLen+1];
97 for (int i = 0; i<itsLen;i++)
98 itsString[i] = rhs[i];
99 itsString[itsLen] = '\0';
100 return *this;
101 // cout << "\tString operator=\n";
102 }
103
104 //non constant offset operator, returns
105 // reference to character so it can be
106 // changed!
107 char & String::operator[](int offset)
108 {
109 if (offset > itsLen)
110 return itsString[itsLen-1];
111 else
112 return itsString[offset];
113 }
114
115 // constant offset operator for use
116 // on const objects (see copy constructor!)
117 char String::operator[](int offset) const
118 {
119 if (offset > itsLen)
120 return itsString[itsLen-1];
121 else
122 return itsString[offset];
123 }
124
125 // creates a new string by adding current
126 // string to rhs
127 String String::operator+(const String& rhs)
128 {
129 int totalLen = itsLen + rhs.GetLen();
130 String temp(totalLen);
131 int i, j;
132 for (i = 0; i<itsLen; i++)
133 temp[i] = itsString[i];
134 for (j = 0; j<rhs.GetLen(); j++, i++)
135 temp[i] = rhs[j];
136 temp[totalLen]='\0';
137 return temp;
138 }
139
140 // changes current string, returns nothing
141 void String::operator+=(const String& rhs)
142 {
143 unsigned short rhsLen = rhs.GetLen();
144 unsigned short totalLen = itsLen + rhsLen;
145 String temp(totalLen);
146 int i, j;
147 for (i = 0; i<itsLen; i++)
148 temp[i] = itsString[i];
149 for (j = 0; j<rhs.GetLen(); j++, i++)
150 temp[i] = rhs[i-itsLen];
151 temp[totalLen]='\0';
152 *this = temp;
153 }
154
155 int String::ConstructorCount = 0;
2 #include <iostream>
3 #include <string.h>
4 using namespace std;
5 class String
6 {
7 public:
8 // constructors
9 String();
10 String(const char *const);
11 String(const String &);
12 ~String();
13
14 // overloaded operators
15 char & operator[](int offset);
16 char operator[](int offset) const;
17 String operator+(const String&);
18 void operator+=(const String&);
19 String & operator= (const String &);
20
21 // General accessors
22 int GetLen()const { return itsLen; }
23 const char * GetString() const { return itsString; }
24 static int ConstructorCount;
25
26 private:
27 String (int); // private constructor
28 char * itsString;
29 unsigned short itsLen;
30
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 cout << "\tDefault string constructor\n";
40 ConstructorCount++;
41 }
42
43 // private (helper) constructor, used only by
44 // class methods for creating a new string of
45 // required size. Null filled.
46 String::String(int len)
47 {
48 itsString = new char[len+1];
49 for (int i = 0; i<=len; i++)
50 itsString[i] = '\0';
51 itsLen=len;
52 cout << "\tString(int) constructor\n";
53 ConstructorCount++;
54 }
55
56 // Converts a character array to a String
57 String::String(const char * const cString)
58 {
59 itsLen = strlen(cString);
60 itsString = new char[itsLen+1];
61 for (int i = 0; i<itsLen; i++)
62 itsString[i] = cString[i];
63 itsString[itsLen]='\0';
64 cout << "\tString(char*) constructor\n";
65 ConstructorCount++;
66 }
67
68 // copy constructor
69 String::String (const String & rhs)
70 {
71 itsLen=rhs.GetLen();
72 itsString = new char[itsLen+1];
73 for (int i = 0; i<itsLen;i++)
74 itsString[i] = rhs[i];
75 itsString[itsLen] = '\0';
76 cout << "\tString(String&) constructor\n";
77 ConstructorCount++;
78 }
79
80 // destructor, frees allocated memory
81 String::~String ()
82 {
83 delete [] itsString;
84 itsLen = 0;
85 cout << "\tString destructor\n";
86 }
87
88 // operator equals, frees existing memory
89 // then copies string and size
90 String& String::operator=(const String & rhs)
91 {
92 if (this == &rhs)
93 return *this;
94 delete [] itsString;
95 itsLen=rhs.GetLen();
96 itsString = new char[itsLen+1];
97 for (int i = 0; i<itsLen;i++)
98 itsString[i] = rhs[i];
99 itsString[itsLen] = '\0';
100 return *this;
101 // cout << "\tString operator=\n";
102 }
103
104 //non constant offset operator, returns
105 // reference to character so it can be
106 // changed!
107 char & String::operator[](int offset)
108 {
109 if (offset > itsLen)
110 return itsString[itsLen-1];
111 else
112 return itsString[offset];
113 }
114
115 // constant offset operator for use
116 // on const objects (see copy constructor!)
117 char String::operator[](int offset) const
118 {
119 if (offset > itsLen)
120 return itsString[itsLen-1];
121 else
122 return itsString[offset];
123 }
124
125 // creates a new string by adding current
126 // string to rhs
127 String String::operator+(const String& rhs)
128 {
129 int totalLen = itsLen + rhs.GetLen();
130 String temp(totalLen);
131 int i, j;
132 for (i = 0; i<itsLen; i++)
133 temp[i] = itsString[i];
134 for (j = 0; j<rhs.GetLen(); j++, i++)
135 temp[i] = rhs[j];
136 temp[totalLen]='\0';
137 return temp;
138 }
139
140 // changes current string, returns nothing
141 void String::operator+=(const String& rhs)
142 {
143 unsigned short rhsLen = rhs.GetLen();
144 unsigned short totalLen = itsLen + rhsLen;
145 String temp(totalLen);
146 int i, j;
147 for (i = 0; i<itsLen; i++)
148 temp[i] = itsString[i];
149 for (j = 0; j<rhs.GetLen(); j++, i++)
150 temp[i] = rhs[i-itsLen];
151 temp[totalLen]='\0';
152 *this = temp;
153 }
154
155 int String::ConstructorCount = 0;
帮忙注释
156
156