From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp | 101 ++++++++++ .../Prog1/examples/ch08/Fig08_07_09/String1.cpp | 221 +++++++++++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h | 85 ++++++++ 3 files changed, 407 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h (limited to 'Bachelor/Prog1/examples/ch08/Fig08_07_09') diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp b/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp new file mode 100644 index 0000000..9a9d516 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp @@ -0,0 +1,101 @@ +// Fig. 8.9: fig08_09.cpp +// String class test program. +#include + +using std::cout; +using std::endl; + +#include "string1.h" + +int main() +{ + String s1( "happy" ); + String s2( " birthday" ); + String s3; + + // test overloaded equality and relational operators + cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 + << "\"; s3 is \"" << s3 << '\"' + << "\n\nThe results of comparing s2 and s1:" + << "\ns2 == s1 yields " + << ( s2 == s1 ? "true" : "false" ) + << "\ns2 != s1 yields " + << ( s2 != s1 ? "true" : "false" ) + << "\ns2 > s1 yields " + << ( s2 > s1 ? "true" : "false" ) + << "\ns2 < s1 yields " + << ( s2 < s1 ? "true" : "false" ) + << "\ns2 >= s1 yields " + << ( s2 >= s1 ? "true" : "false" ) + << "\ns2 <= s1 yields " + << ( s2 <= s1 ? "true" : "false" ); + + // test overloaded String empty (!) operator + cout << "\n\nTesting !s3:\n"; + + if ( !s3 ) { + cout << "s3 is empty; assigning s1 to s3;\n"; + s3 = s1; // test overloaded assignment + cout << "s3 is \"" << s3 << "\""; + } + + // test overloaded String concatenation operator + cout << "\n\ns1 += s2 yields s1 = "; + s1 += s2; // test overloaded concatenation + cout << s1; + + // test conversion constructor + cout << "\n\ns1 += \" to you\" yields\n"; + s1 += " to you"; // test conversion constructor + cout << "s1 = " << s1 << "\n\n"; + + // test overloaded function call operator () for substring + cout << "The substring of s1 starting at\n" + << "location 0 for 14 characters, s1(0, 14), is:\n" + << s1( 0, 14 ) << "\n\n"; + + // test substring "to-end-of-String" option + cout << "The substring of s1 starting at\n" + << "location 15, s1(15, 0), is: " + << s1( 15, 0 ) << "\n\n"; // 0 is "to end of string" + + // test copy constructor + String *s4Ptr = new String( s1 ); + cout << "\n*s4Ptr = " << *s4Ptr << "\n\n"; + + // test assignment (=) operator with self-assignment + cout << "assigning *s4Ptr to *s4Ptr\n"; + *s4Ptr = *s4Ptr; // test overloaded assignment + cout << "*s4Ptr = " << *s4Ptr << '\n'; + + // test destructor + delete s4Ptr; + + // test using subscript operator to create lvalue + s1[ 0 ] = 'H'; + s1[ 6 ] = 'B'; + cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " + << s1 << "\n\n"; + + // test subscript out of range + cout << "Attempt to assign 'd' to s1[30] yields:" << endl; + s1[ 30 ] = 'd'; // ERROR: subscript out of range + + return 0; + +} // end main + +/************************************************************************** + * (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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp new file mode 100644 index 0000000..7cca6d7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp @@ -0,0 +1,221 @@ +// Fig. 8.8: string1.cpp +// Member function definitions for class String. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // C++ standard "new" operator + +#include // strcpy and strcat prototypes +#include // 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 © ) + : 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h new file mode 100644 index 0000000..81012aa --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h @@ -0,0 +1,85 @@ +// Fig. 8.7: string1.h +// String class definition. +#ifndef STRING1_H +#define STRING1_H + +#include + +using std::ostream; +using std::istream; + +class String { + friend ostream &operator<<( ostream &, const String & ); + friend istream &operator>>( istream &, String & ); + +public: + String( const char * = "" ); // conversion/default ctor + String( const String & ); // copy constructor + ~String(); // destructor + + const String &operator=( const String & ); // assignment + const String &operator+=( const String & ); // concatenation + + bool operator!() const; // is String empty? + bool operator==( const String & ) const; // test s1 == s2 + bool operator<( const String & ) const; // test s1 < s2 + + // test s1 != s2 + bool operator!=( const String & right ) const + { + return !( *this == right ); + + } // end function operator!= + + // test s1 > s2 + bool operator>( const String &right ) const + { + return right < *this; + + } // end function operator> + + // test s1 <= s2 + bool operator<=( const String &right ) const + { + return !( right < *this ); + + } // end function operator <= + + // test s1 >= s2 + bool operator>=( const String &right ) const + { + return !( *this < right ); + + } // end function operator>= + + char &operator[]( int ); // subscript operator + const char &operator[]( int ) const; // subscript operator + + String operator()( int, int ); // return a substring + + int getLength() const; // return string length + +private: + int length; // string length + char *sPtr; // pointer to start of string + + void setString( const char * ); // utility function + +}; // end class String + +#endif + +/************************************************************************** + * (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. * + *************************************************************************/ \ No newline at end of file -- cgit v1.2.3