< previous page page_325 next page >

Page 325
LISTING 20.3 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
137:      return temp;
138:   }
139:
140:   // changes current string, returns nothing
141:   void String::operator+=(const String& rhs)
142:   {
143:      int rhsLen = rhs.GetLen();
144:      int totalLen = itsLen + rhsLen;
145:       int i, j;
146:      String temp(totalLen);
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;

Output: This program has no output.
Analysis: On line 23 the static member variable ConstructorCount is declared, and on line 155 it is initialized. This variable is incremented in each string constructor.
Listing 20.4 describes an Employee class that contains three string objects. Note that a number of statements are commented out; they will be used in later listings.
LISTING 20.4 THEEmployee CLASS AND DRIVER PROGRAM

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:     class Employee
2:     {
3:
4:     public:
5:        Employee();
6:        Employee(char *, char *, char *, long);
7:        ~Employee();
8:        Employee(const Employee&);
9:        Employee & operator= (const Employee &);
10:
11:       const String & GetFirstName() const { return itsFirstName; }
12:       const String & GetLastName() const { return itsLastName; }
13:       const String & GetAddress() const { return itsAddress; }
14:       long GetSalary() const { return itsSalary; }
15:
16:       void SetFirstName(const String & fName) { itsFirstName = fName; }
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_325 next page >

If you like this book, buy it!