Download this source file


// string1.cpp - A standard C++ string class that builds on string0.
// This version adds some overloaded operators: operator[] to fetch
// and set characters within the string; and operator char* to
// convert string1 objects to char* arrays.  This last allows one
// to use string1 objects where ever a char* is expected, such as
// arguments to strcmp function.  Note operator== was defined in
// class string0 already.  In this version this and other member
// functions are defined "inline".  (The operator+ for string
// concatenation and operator= to replace set, and the friend
// operator<< and operator>> for i/o of string1 objects, as well
// as any other operators, are left as an exersize for the reader.)
// Finally, note the use of checked exceptions and a try block.
//
// (C)2000 by Wayne Pollock, Tampa Florida USA.

#include <iostream>
#include <cstring>

using namespace std;

class string1
{
 public:
   string1 () { rep[0] = '\0'; }
   string1 ( char* s ) { strcpy( rep, s ); } // should have strlen check here.
   char& operator[] ( const int i ) const throw ( int );
   bool operator== ( const string1 &s ) const { return ! strcmp( rep, s.rep ); }
   void set ( const char* const s ) throw ( char* );
   // convert from string to char*:
   operator char* () const { return const_cast< char* > ( rep ); }
   friend const string1 operator+ ( const string1&, const string1& );

 private:
   static const int MaxLength = 25;  // 255 would be a more reasonable limit!
   char rep[MaxLength + 1];
};

int main ()  // This main is just a test driver for class string!
{  try
   {  string1 name1, name2;
      char s1[50], s2[50];

      cout << "Enter two names separated by a space: ";
      cin >> s1 >> s2;
      name1.set( s1 );
      name2.set( s2 );

      if ( ! strcmp( name1, "Wayne" ) ) // Note use of operator char* here.
         cout << "Hello Wayne!\n";
      else
         cout << "Hey, who the heck are you?\n";

      if ( name1 == name2 )
      {  cout << "The names are the same." << endl;
         name1[60] = 'X';  // This should throw an exception!
         cout << "Changed name1[60] = " << name1[60] << "." << endl;
      } else {
         cout << "The name are different." << endl;
         name1[0] = 'Z';
         cout << "Changed name1[0] = " << name1[0] << "." << endl;
      }
      char c = name2[0];
      cout << "The first character of name2 is " << c << endl;
      cout << "The full name is " << (name1 + " " + name2) << endl;
      return 0;
   }
   catch ( char* msg )
   {  cerr << "*** Exception caught:  String too long:" << endl;
      cerr << "\t" << msg << endl;
      return -1;
   }
   catch ( int i )
   {  cerr << "*** Exception caught: index out of bounds ("
           << i << ")." << endl;  // Note single multi-line output statement.
      return -1;
   }
   catch ( ... ) // Catch all other exceptions.
   {  cerr << "\n*** Unknown exception occured!\a" << endl;
      return -1;
   }
}

//--------------------------------------------------------------
// String1 Class Implementation:

char& string1::operator[] ( const int i ) const throw ( int )
{  if ( i < 0 || i > MaxLength )
      throw i;
   return char( rep[i] );
}

void string1::set ( const char* const s ) throw ( char* )
{  if ( strlen(s) > MaxLength )
      throw (char*) s;
   strcpy( rep, s );
   return;
}

const string1 operator+( const string1& s1, const string1& s2 )
{
   string1 temp = s1;
   strncat( temp, s2.rep, string1::MaxLength - strlen(s1.rep) );
   return temp;
}




Send comments and mail to the WebMaster.