#define MAXCHARS 80 class strtype { private: char name[MAXCHARS]; public: strtype( strtype &str ); strtype( char *str ); strtype( void ); strtype operator+(strtype str); strtype operator+(const char *str); strtype operator=(strtype str); strtype operator=(const char *str); void Showname( void ) { printf("%s",name) ;} char *StringAddress( void ) { return name; } };Strings have a maximum length that can be specified at compile time using the symbol MAXCHARS. The entire MAXCHARS number of bytes is allocated when the string is created. This wastes some memory, but it avoids some of the nasty problems associated with strings over-writing other structures in the heap. If you need the ram, then you can reduce MAXCHARS to a minimun of 5. This will also speed up the program some since it will spend less time in the string functions.
Note that the constructor function copies at most MAXCHARS characters into the string buffer. It also terminates the string with a null character. The default string is a null character in position 0.
The string object can be concatenated and assigned. The
concatenation operator is '+'. The left
operand of the plus operator must be a strtype. The plus is
overloaded to accept different kinds of right operands. The
right operands may be strings or constant character arrays.
This allows you to concatenate strings using statements
such as s1+"const"+" char"+s2
where s1 and s2 are
strings. The only problem is remembering that the first
term in the concatenation must be a string. (I did not see
the need of anything fancier than this in this context.)
Also, concatenation controls the length of the intermediate
results to be at most MAXCHARS.
Assignment is overloaded for strings and
constant character arrays. Repeated assignment such as
s1=s2="a silly string"
has not been tested, but I
guess it will work, providing that the leftmost assignments only
have strings as the right operands. Again, strings are
truncated to MAXCHARS during assignment.
The Showname member function simply prints the name. I have used the C function printf() instead of cout throughout the program because cout jumbled the output when piping output to a file. (Maybe I didn't do something right for cout, but printf worked so I kept it. You should use endl or flush to terminate the cout's. This helps coordinate printf with the iostreams by flushing the stream.)
The last member function is *StringAddress(). This merely returns the address of the name. This function is used by the matrix functions later. When it is used in the matrix functions, the strtype is private, so StringAddress is also private. This keeps you from accidently corrupting the name.
The strtype class could be replaced by more sophisticated string functions. You could allow them to have variable length by using pointers. You could also combine repeated references to the pointers to the name so that duplicate names would occupy the same space in the heap. ( See Voss and Chui [#!vc:disk!#] for more on strings.)