// class_simple_string.h (c) 1998-2001 Kari Laitinen /* To make Borland C++ 5.5.1 compiler accept this program, the argument of function operator= must be of type const Simple_string&. That is an error in the compiler because some other compilers compile the function without the const declaration. The keyword const here does not affect the behavior of the program. This program is presented in the book without the const declaration. */ #ifndef CLASS_SIMPLE_STRING_H // prohibiting multiple inclusions #define CLASS_SIMPLE_STRING_H class Simple_string { protected: char characters_of_this_string[ 100 ] ; int number_of_characters_in_this_string ; public: Simple_string() ; Simple_string( char given_traditional_string[] ) ; Simple_string( Simple_string& given_simple_string ) ; Simple_string operator+ ( Simple_string& string_to_append ) ; Simple_string& operator= ( const Simple_string& string_to_copy ) ; char& operator[] ( int string_index ) ; friend ostream& operator<< ( ostream& output_stream, Simple_string& string_to_output ) ; } ; Simple_string::Simple_string() { number_of_characters_in_this_string = 0 ; } Simple_string::Simple_string( char given_traditional_string[] ) { int character_index = 0 ; while ( given_traditional_string[ character_index ] != 0 ) { characters_of_this_string[ character_index ] = given_traditional_string[ character_index ] ; character_index ++ ; } number_of_characters_in_this_string = character_index ; } Simple_string::Simple_string( Simple_string& given_simple_string ) { number_of_characters_in_this_string = given_simple_string.number_of_characters_in_this_string ; for ( int character_index = 0 ; character_index < number_of_characters_in_this_string ; character_index ++ ) { characters_of_this_string[ character_index ] = given_simple_string.characters_of_this_string[ character_index ] ; } } Simple_string Simple_string::operator+ ( Simple_string& string_to_append ) { Simple_string sum_of_strings( *this ) ; // sum_of_strings is now a copy of "this" string. // All we have to do is to copy the characters of // string_to_append to the end of sum_of_strings. int character_index = 0 ; while ( character_index < string_to_append.number_of_characters_in_this_string ) { sum_of_strings.characters_of_this_string[ sum_of_strings.number_of_characters_in_this_string + character_index ] = string_to_append.characters_of_this_string[ character_index ] ; character_index ++ ; } sum_of_strings.number_of_characters_in_this_string = sum_of_strings.number_of_characters_in_this_string + string_to_append.number_of_characters_in_this_string ; return sum_of_strings ; } Simple_string& Simple_string::operator= ( const Simple_string& string_to_copy ) { if ( this != &string_to_copy ) { number_of_characters_in_this_string = string_to_copy.number_of_characters_in_this_string ; for ( int character_index = 0 ; character_index < number_of_characters_in_this_string ; character_index ++ ) { characters_of_this_string[ character_index ] = string_to_copy.characters_of_this_string[ character_index ] ; } } return *this ; } ostream& operator<< ( ostream& output_stream, Simple_string& string_to_output ) { int character_index = 0 ; while ( character_index < string_to_output. number_of_characters_in_this_string ) { output_stream << string_to_output.characters_of_this_string[ character_index ] ; character_index ++ ; } return output_stream ; } #endif // end of include guard