home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
TUTORIAL
/
ICLCC
/
TUTOR6
/
TSTRING.H
< prev
Wrap
C/C++ Source or Header
|
1993-05-07
|
5KB
|
121 lines
/******************************************************************************/
/* */
/* COPYRIGHT: */
/* ---------- */
/* Copyright (C) International Business Machines Corp., 1991,1992. */
/* */
/* DISCLAIMER OF WARRANTIES: */
/* ------------------------- */
/* The following [enclosed] code is sample code created by IBM */
/* Corporation. This sample code is not part of any standard IBM product */
/* and is provided to you solely for the purpose of assisting you in the */
/* development of your applications. The code is provided "AS IS", */
/* without warranty of any kind. IBM shall not be liable for any damages */
/* arising out of your use of the sample code, even if they have been */
/* advised of the possibility of such damages. */
/* */
/******************************************************************************/
#ifndef STRINGX_H
#define STRINGX_H
//
#include <string.h>
#include <iostream.h>
//
//
class String {
// ==========================================================
// Data members
// ==========================================================
char *text;
int length;
public:
// ==========================================================
// member functions
// ==========================================================
char * getText() const {
return text;
}
int getLength() const {
return length;
}
// ==========================================================
// Constructors, destructors, member functions
// ==========================================================
String(const char *iText) {
length = strlen(iText);
text = new char[length+1];
strcpy(text, iText);
}
String(const String& iString) {
length = iString.length;
text = new char[length+1];
strcpy(text, iString.text);
}
String(int iLength) {
length = iLength;
text = new char[length+1];
for (int i = 0; i < length; i++) text[i] = ' ';
text[length] = '\0';
}
// ==========================================================
// === Constructor
// ==========================================================
String() {
length = 0;
text = new char[length+1];
text[0] = '\0';
}
// ==========================================================
// === Destructor
// ==========================================================
String::~String() {
delete text;
}
// ==========================================================
// === (in) equality operator overloading
// ==========================================================
int operator == (const String& anOther)const {
return (strcmp(text, anOther.text) == 0);
}
int operator != (const String& anOther)const {
return !(*this == anOther);
}
// =========================================================
// === <, > operator overloading
// =========================================================
int operator < (const String& anOther)const {
return (strcmp(text, anOther.text) < 0);
}
int operator > (const String& anOther)const {
return (strcmp(text, anOther.text) > 0);
}
int operator <= (const String& anOther)const {
return !(*this > anOther);
}
int operator >= (const String& anOther)const {
return !(*this < anOther);
}
// ===========================================================
// === Assignment operator overloading
// ===========================================================
String& operator = (const String& iString) {
if (& iString == this) return *this;
delete text;
length = iString.length;
text = new char[length+1];
strcpy(text, iString.text);
return *this;
}
} ;
inline ostream& operator<< (ostream& sout, String const& s)
{ return sout << s.getText ();
}
inline int compare (String const & s1, String const & s2){
return strcmp(s1.getText(),s2.getText());
}
#endif