PROGRAM 12
// FILE NAME: PROG12.CPP PROGRAMMER NAME: ANTHONY F. ORTIZ // THIS PROGRAM DEMONSTRATES A STRING OBJECT. #include #include #include "ourstr.h" int main () { clrscr (); string a; string b = "123"; cout << "ENTER A STRING: "; cin >> a; a = a + b + a; cout << endl << "A = " << a << endl << endl; cout << "LENGTH OF A = " << a.length () << endl; return 0; } // I DIDN'T WRITE OURSTR.H AND OURSTR.CPP. // FILENAME: OURSTR.H // THIS PROGRAM DECLARES A CLASS CALLED STRING. #ifndef ourstr_h #define ourstr_h #include class string { public: string :: string (); // POST: STRING OBJECT IS SET TO DEFAULT NULL STRING AS IN STRING A. string :: string (const string & strobj); // POST: OBJECT IS COPIED DURING ARGUMENT/PARAMETER ASSOCIATIONS // AND DURING A FUNCTION RETURN. OBJECT IS ALSO INITIALIZED AS // STRING ANOTHERSTR(ASTR). string :: string (const char * inittext); // POST: OBJECT IS INITIALIZED USING CHAR* ARGUMENT WHEN CONSTRUCTED // AS STRING ASTR ("HALL"). string :: ~string (); // POST: DESTRUCTOR. void string :: toUpper (); // POST: THE STRING IS CONVERTED TO UPPER CASE. string & string :: operator = (const char * rightvalue); // POST: THE LVALUE STRING OBJECT STORES THE STRING LITERAL AND // THE DYNAMIC LENGTH OF THE EXPRESSION TO THE RIGHT OF = // AS IN S1 = "A STRING LITERAL". string & string :: operator = (const string & rightvalue); // POST: THE LVALUE STRING OBJECT STORES THE STRING LITERAL AND // THE DYNAMIC LENGTH OF THE EXPRESSION TO THE RIGHT OF = // AS IN S1 = S2. string & string :: operator += (const string & right); // POST: THE RIGHT STRING OBJECT IS APPENDED TO THE // STRING OBJECT ON THE LEFT. char & string :: operator [] (int index); // POST: A REFERENCE TO ONE INDIVIDUAL CHARACTER IS RETURNED. // THIS MAY BE APPLIED TO NON-CONST OBJECTS. const char & string :: operator [] (int index) const; // POST: THE DYNAMIC LENGTH OF THE STRING OBJECT IS RETURNED. unsigned int string :: length () const; // POST: THE CHARACTERS (CHAR *) OF THE STRING OBJECT ARE RETURNED. // COULD USE AS ARG TO IFSTREAM INFILE(FILENAME.THECHARS()). char * string :: chars () const; // POST: THE CHARACTERS (CHAR *) OF THE STRING OBJECT ARE RETURNED. // COULD USE AS ARG TO IFSTREAM INFILE(FILENAME.THECHARS()). private: int len; char * thechars; }; ostream & operator << (ostream & os, const string & outputstring); istream & operator >> (istream & is, string & inputstring); string operator + (const string & left, const string &right); string operator + (const string & left, const char * right); string operator + (const char * left, const string & right); int operator < (const string & left, const string & right); int operator <= (const string & left, const string & right); int operator > (const string & left, const string & right); int operator >= (const string & left, const string & right); int operator == (const string & left, const string & right); int operator != (const string & left, const string & right); #include "ourstr.cpp" #endif // FILENAME: OURSTR.CPP // THIS PROGRAM CONTAINS THE IMPLEMENTATION OF THE CLASS STRING. #include #include #include #include string :: string () { len = 0; thechars = new char [1]; thechars [0] = 0; } string :: string (const char * inittext) { len = strlen (inittext); thechars = new char [len + 1]; if (! thechars) { cout << "**ERROR** MEMORY EXAUSTED TRYING TO CREATE " << inittext << endl << "...PROGRAM TERMINATED..." << endl; exit (0); } strcpy (thechars, inittext); } string :: string (const string & source) { len = source.len; thechars = new char [len + 1]; if (! thechars) { cout << "**ERROR** MEMORY EXAUSTED TRYING TO CREATE " << source.thechars << endl << "...PROGRAM TERMINATED..." << endl; exit (0); } strcpy (thechars, source.thechars); } unsigned int string :: length () const { return len; } char * string :: chars () const { return thechars; } string :: ~string () { delete [] thechars; } void string :: toUpper () { int lastchar = length (); for (int j = 0; j <= lastchar - 1; j++) thechars [j] = toupper (thechars [j]); } string & string :: operator = (const string & rightvalue) { if (this != &rightvalue) { delete thechars; len = rightvalue.len; thechars = new char [len + 1]; strcpy (thechars, rightvalue.thechars); } return *this; } string & string :: operator = (const char * rightvalue) { delete thechars; len = strlen (rightvalue); thechars = new char [len + 1]; strcpy (thechars, rightvalue); return *this; } string & string :: operator += (const string & right) { int combinedlength = this->len + right.len; char * temp = new char [combinedlength + 1]; if (! temp) { cout << "**ERROR** MEMORY EXAUSTED TRYING TO CREATE " << right.thechars << endl; exit (0); } strcpy (temp, thechars); strcat (temp, right.thechars); delete thechars; thechars = temp; len = combinedlength; return *this; } char & string :: operator [] (int index) { if ((index < 0) || (index >= len)) { cout << "\n**ERROR** FOR STRING OBJECT '" << thechars << "', SUBSCRIPT [" << index << "] IS OUT OF RANGE. " << endl; cout << "SUBSCRIPT MUST BE IN THE RANGE OF 1.." << (len - 1) << endl; exit (0); } return thechars [index]; } const char & string :: operator [] (int index) const { if ((index < 0) || (index > len)) { cout << "\n**ERROR** FOR STRING OBJECT '" << thechars << "', INDEX [" << index << "] IS OUT OF RANGE. " << endl; cout << "INDEX MUST BE IN THE RANGE OF 1.." << (len - 1) << endl; exit (0); } return thechars [index]; } ostream & operator << (ostream & os, const string & outputstring) { os << outputstring.chars (); return os; } istream & operator >> (istream & is, string & inputstring) { char temp [128]; is >> temp; inputstring = string (temp); return is; } string operator + (const string & left, const string & right) { string temp (left); temp += right; return temp; } string operator + (const string & left, const char * right) { string temp (left); temp += right; return temp; } string operator + (const char * left, const string & right) { string temp; temp = left; temp += right; return temp; } int operator < (const string & left, const string & right) { return (strcmp (left.chars (), right.chars ()) < 0); } int operator <= (const string & left, const string & right) { return ! (strcmp (left.chars (), right.chars ()) > 0); } int operator > (const string & left, const string & right) { return (strcmp (left.chars (), right.chars ()) > 0); } int operator >= (const string & left, const string & right) { return ! (strcmp (left.chars (), right.chars ()) < 0); } int operator == (const string & left, const string & right) { return (strcmp (left.chars (), right.chars ()) == 0); } int operator != (const string & left, const string & right) { return (strcmp (left.chars (), right.chars ()) != 0); } // OUTFILE: PROG12.OUT ENTER A STRING: anthony A = anthony123anthony LENGTH OF A = 17
BACK TO COMP251 PAGE.