summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp
blob: 7cca6d703b7f07e8d8489b07eb49504784f496f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Fig. 8.8: string1.cpp
// Member function definitions for class String.
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <new>        // C++ standard "new" operator

#include <cstring>    // strcpy and strcat prototypes
#include <cstdlib>    // exit prototype

#include "string1.h"  // String class definition

// conversion constructor converts char * to String
String::String( const char *s ) 
   : length( ( s != 0 ) ? strlen( s ) : 0 )
{
   cout << "Conversion constructor: " << s << '\n';
   setString( s );         // call utility function

} // end String conversion constructor

// copy constructor
String::String( const String &copy ) 
   : length( copy.length )
{
   cout << "Copy constructor: " << copy.sPtr << '\n';
   setString( copy.sPtr ); // call utility function

} // end String copy constructor

// Destructor
String::~String()
{
   cout << "Destructor: " << sPtr << '\n';
   delete [] sPtr;         // reclaim string

} // end ~String destructor

// overloaded = operator; avoids self assignment
const String &String::operator=( const String &right )
{
   cout << "operator= called\n";

   if ( &right != this ) {         // avoid self assignment
      delete [] sPtr;              // prevents memory leak
      length = right.length;       // new String length
      setString( right.sPtr );     // call utility function
   }

   else
      cout << "Attempted assignment of a String to itself\n";

   return *this;   // enables cascaded assignments

} // end function operator=

// concatenate right operand to this object and
// store in this object.
const String &String::operator+=( const String &right )
{
   size_t newLength = length + right.length;   // new length
   char *tempPtr = new char[ newLength + 1 ];  // create memory

   strcpy( tempPtr, sPtr );                 // copy sPtr
   strcpy( tempPtr + length, right.sPtr );  // copy right.sPtr

   delete [] sPtr;      // reclaim old space
   sPtr = tempPtr;      // assign new array to sPtr
   length = newLength;  // assign new length to length

   return *this;  // enables cascaded calls

} // end function operator+=

// is this String empty?
bool String::operator!() const 
{ 
   return length == 0; 

} // end function operator! 

// Is this String equal to right String?
bool String::operator==( const String &right ) const
{ 
   return strcmp( sPtr, right.sPtr ) == 0; 

} // end function operator==

// Is this String less than right String?
bool String::operator<( const String &right ) const
{ 
   return strcmp( sPtr, right.sPtr ) < 0; 

} // end function operator<

// return reference to character in String as lvalue
char &String::operator[]( int subscript )
{
   // test for subscript out of range
   if( subscript < 0 || subscript >= length ) {
      cout << "Error: Subscript " << subscript 
           << " out of range" << endl;

      exit( 1 );  // terminate program
   }

   return sPtr[ subscript ];  // creates lvalue

} // end function operator[]

// return reference to character in String as rvalue
const char &String::operator[]( int subscript ) const
{
   // test for subscript out of range
   if( subscript < 0 || subscript >= length ) {
      cout << "Error: Subscript " << subscript 
           << " out of range" << endl;

      exit( 1 );  // terminate program
   }

   return sPtr[ subscript ];  // creates rvalue

} // end function operator[]

// return a substring beginning at index and
// of length subLength
String String::operator()( int index, int subLength )
{
   // if index is out of range or substring length < 0, 
   // return an empty String object
   if ( index < 0 || index >= length || subLength < 0 )  
      return "";  // converted to a String object automatically

   // determine length of substring
   int len;

   if ( ( subLength == 0 ) || ( index + subLength > length ) )
      len = length - index;
   else
      len = subLength;

   // allocate temporary array for substring and 
   // terminating null character
   char *tempPtr = new char[ len + 1 ];

   // copy substring into char array and terminate string
   strncpy( tempPtr, &sPtr[ index ], len );
   tempPtr[ len ] = '\0';

   // create temporary String object containing the substring
   String tempString( tempPtr );
   delete [] tempPtr;  // delete temporary array

   return tempString;  // return copy of the temporary String

} // end function operator()

// return string length
int String::getLength() const 
{ 
   return length; 

} // end function getLenth

// utility function called by constructors and operator=
void String::setString( const char *string2 )
{
   sPtr = new char[ length + 1 ]; // allocate memory

   // if string2 is not a null pointer, copy contents 
   if ( string2 != 0 )
      strcpy( sPtr, string2 );    // copy literal to object

   // if string2 is a null pointer, make this an empty string
   else
      sPtr[ 0 ] = '\0';           // empty string

} // end function setString 

// overloaded output operator
ostream &operator<<( ostream &output, const String &s )
{
   output << s.sPtr;

   return output;   // enables cascading

} // end function operator<<

// overloaded input operator
istream &operator>>( istream &input, String &s )
{
   char temp[ 100 ];   // buffer to store input

   input >> setw( 100 ) >> temp;
   s = temp;        // use String class assignment operator

   return input;    // enables cascading

} // end function operator>>

/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/