#include #include #include using namespace std; class StrList { public: static int usize ( ); static char* toa (int ); static int toi (char* ); private: static char *strlist[ ]; static const int setsz = 3; }; char* StrList::strlist[ ] = {"argon", "boron", "carbon"}; int StrList::usize( ) { return setsz; } char* StrList::toa(int n) { string estr = "argument to StrList::toa( ) out of range."; if ( n < 0 || n >= usize( ) ) throw out_of_range( estr ); else return strlist[n]; } int StrList::toi(char* str) { string instr = str; string sa = strlist[0]; string sb = strlist[1]; string sc = strlist[2]; string estr = "argument to StrList::toi( ) not in Tsset."; if ( str ) { switch( str[0] ) { case 'a': if ( instr == sa ) return 0; else break; case 'b': if ( instr == sb ) return 1; else break; case 'c': if ( instr == sc ) return 2; } } throw invalid_argument( estr ); } int main ( ) { int i; cout << "Test of StrList class members:" << endl; cout << "usize( ) = " << StrList::usize( ) << endl; for ( i = 0; i < 3; i++ ) cout << "toa(" << i << ") = " << StrList::toa(i) << endl; cout << "toa(-1) = " ; try { cout << StrList::toa(-1) << endl; } catch (out_of_range e) { cout << "\nException thrown and caught: " << e.what( ) << endl; } cout << "toi(boron) = " << StrList::toi("boron") << endl; cout << "toi(borax) = " ; try { cout << StrList::toi("borax") << endl; } catch (invalid_argument e) { cout << "\nException thrown and caught: " << e.what( ) << endl; } return 0; }