home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
string.zip
/
STRINGS.CPP
< prev
next >
Wrap
Text File
|
1994-04-19
|
8KB
|
209 lines
/*----------------------------------------------------------------------------*/
/* (c) 1994 Larry Morley */
/*----------------------------------------------------------------------------*/
/* Module : strings.cpp Date: 04 April 1994 */
/* */
/* Description: A string class that automatically sizes itself based on the */
/* data assigned to it or provided when an object of the class */
/* type is created. Automatically adjusts string length for */
/* concatenation. Looks to other functions just like an */
/* ordinary char * via a conversion function. */
/* */
/*----------------------------------------------------------------------------*/
/* */
/* To contact the author, send an e-mail message to 73670,563 or a regular */
/* letter / note to: */
/* */
/* Larry Morley / Enlightened Computer Solutions */
/* 6909 Custer #2503 */
/* Plano, TX 75023 */
/* */
/* Please let me know if you're using this code, and your suggestions, */
/* questions, etc. Thanks. */
/* */
/*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
/*----------------------------------------------------------------------------*/
int main(void);
/*----------------------------------------------------------------------------*/
// Note that there's no need to override the assignment operator;
// since there's a constructor that takes a char * as an argument,
// the compiler automatically supplies the conversion from char *
// to String, by creating an interim string from the char *, and a
// new string through the constructor as a result of the assignment.
class String
{
private:
// The actual string text
char *szText;
public:
// Constructor for string literals,
// char * strings, etc.
String(char *initText)
{
// szText <- address if enough memory
// is available or NULL if out of memory.
szText = new char[strlen(initText)+1];
// Make sure we got the memory
if (szText)
strcpy(szText,initText);
};
//---------------------------------------------------
// Constructor for a string of
// a certain length
String(int size)
{
// szText <- address if enough memory
// is available or NULL if out of memory.
szText = new char [size];
// Make sure we got the memory
// Terminate the string at the
// first element (empty string)
if (szText)
szText[0] = (char)0;
};
//---------------------------------------------------
~String()
{
// If there's a string, delete it
if (szText)
delete[] szText;
};
//---------------------------------------------------
// Conversion function - makes class objects
// function and look like a normal char *s
operator char *()
{
return szText;
};
//---------------------------------------------------
// Overload addition operator
// to mean concatenation
String operator+(String szStringB)
{
// Create a temporary string
char *p = new char[strlen(szText)+strlen(szStringB)+1];
// Make sure we got the memory
if (p)
{
strcpy(p,szText);
// Delete the old string
delete[] szText;
// Copy the original string into the
// temporary string
szText = p;
// Do the concatenation
strcat(szText,szStringB);
// Delete the temporary free store
delete[] p;
// Return the new value of szText
return szText;
}
// Out of memory - return NULL
return (String)0;
};
//---------------------------------------------------
// Do the same for "+=". Since the copy constructor
// will automatically handle the assignment, all
// we need to do here is the concatenation; e.g.,
// the "+=" operator is equivalent to the "+" operator
String operator+=(String szStringB)
{
return String::operator+(szStringB);
};
//***************************************************
// Note that you could build in some handy functions
// like these, and not interfere with anything
int Length()
{
return strlen(szText);
};
//---------------------------------------------------
int IsEqual(String szStringB)
{
return (!strcmp(szText,szStringB));
};
//***************************************************
// And, you can overload the equality operator
String operator==(String szStringB)
{
return IsEqual(szStringB);
};
//---------------------------------------------------
};
/*----------------------------------------------------------------------------*/
// Try the String class out...
main()
{
String StringA = "This is a string";
char * StringB = ", alright.";
String StringC = " You bet.";
//============================================
// Test the concatenation operator ("+")
printf("StringA = StringA + StringB;\n");
printf("\"%s\" = \"%s\" + \"%s\" ;\n",StringA,StringA,StringB);
StringA = StringA + StringB;
printf("StringA: \"%s\".\n\n",StringA);
//============================================
// Test the add-and-assign operator ("+=")
printf("StringA += StringC;\n");
printf("\"%s\" += \"%s\" ;\n",StringA,StringC);
StringA += StringC;
printf("StringA: \"%s\".\n\n",StringA);
//============================================
// Test the length function (String::Length())
printf("Length of StringA: %d Characters.\n\n",StringA.Length());
//============================================
// Test the equality function (String::IsEqual())
printf("StringA (\"%s\") == StringB (\"%s\") ? %s.\n\n",
StringA,
StringB,
StringA.IsEqual(StringB) ? "Yes"
: "No");
//============================================
// Test the equality operator ("==")
printf("StringA (\"%s\") == StringA (\"%s\") ? %s.\n",
StringA,
StringA,
StringA == StringA ? "Yes" : "No");
printf("**************************************\n");
return 0;
};
/*----------------------------------------------------------------------------*/