📄 string.cpp
字号:
// Fig. 11.10: String.cpp
// Member-function definitions for class String.
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstring> // strcpy and strcat prototypes
using std::strcmp;
using std::strcpy;
using std::strcat;
#include <cstdlib> // exit prototype
using std::exit;
#include "String.h" // String class definition
// conversion (and default) constructor converts char * to String
String::String( const char *s )
: length( ( s != 0 ) ? strlen( s ) : 0 )
{
cout << "Conversion (and default) constructor: " << s << endl;
setString( s ); // call utility function
} // end String conversion constructor
// copy constructor
String::String( const String © )
: length( copy.length )
{
cout << "Copy constructor: " << copy.sPtr << endl;
setString( copy.sPtr ); // call utility function
} // end String copy constructor
// Destructor
String::~String()
{
cout << "Destructor: " << sPtr << endl;
delete [] sPtr; // release pointer-based string memory
} // end ~String destructor
// overloaded = operator; avoids self assignment
const String &String::operator=( const String &right )
{
cout << "operator= called" << endl;
if ( &right != this ) // avoid self assignment
{
delete [] sPtr; // prevents memory leak
length = right.length; // new String length
setString( right.sPtr ); // call utility function
} // end if
else
cout << "Attempted assignment of a String to itself" << endl;
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 a modifiable lvalue
char &String::operator[]( int subscript )
{
// test for subscript out of range
if ( subscript < 0 || subscript >= length )
{
cerr << "Error: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program
} // end if
return sPtr[ subscript ]; // non-const return; modifiable lvalue
} // end function operator[]
// return reference to character in String as rvalue
char String::operator[]( int subscript ) const
{
// test for subscript out of range
if ( subscript < 0 || subscript >= length )
{
cerr << "Error: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program
} // end if
return sPtr[ subscript ]; // returns copy of this element
} // end function operator[]
// return a substring beginning at index and of length subLength
String String::operator()( int index, int subLength ) const
{
// 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 getLength
// utility function called by constructors and operator=
void String::setString( const char *string2 )
{
sPtr = new char[ length + 1 ]; // allocate memory
if ( string2 != 0 ) // if string2 is not null pointer, copy contents
strcpy( sPtr, string2 ); // copy literal to object
else // if string2 is a null pointer, make this an empty string
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-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. 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. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -