// class_simple_string.h (c) Kari Laitinen // http://www.naturalprogramming.com/ // 1998-??-?? File created. // 2013-11-13 Last modification. /* Keyword const has been added to function operator= parameter as well as to a constructor parameter. */ #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( const 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( const 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