1.string.h
//string.h
#ifndef STRING_H_
#define STRING_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char *str; //pointer to string
int len; //length of string
static int num_strings; //number of objects
static const int CINLIM = 80; //cin input limit
public:
//constructors and other methods
String(const char *s); //constructor
String();//default constructor
String(const String &);//copy constructor
~String(); //destructor
int length() const {return len;}
//overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
//overloaded operator friends
friend bool operator<(const String &st1, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st1, const String &st2);
friend ostream & operator<<(ostream &os, const String &st);
friend istream & operator>>(istream &is, String &st);
//static function
static int HowMany();
};
#endif // STRING_H_
2.string.cpp
//string.cpp--String class methods
#include <cstring>
#include "string.h"
using namespace std;
//initializing static class member
int String::num_strings = 0;
//static method
int String::HowMany()
{
return num_strings;
}
//constructors and other methods
String::String(const char *s) //constructor String from C string
{
len = strlen(s); //set size
str = new char[len + 1]; //allocate storage
strcpy(str, s);
num_strings++; //set object count
}
String::String()//default constructor
{
len = 0;
str = new char[1];
str[0] = '\0';
num_strings++;
}
String::String(const String &st) //copy constructor
{
num_strings++;
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
}
String::~String()
{
--num_strings;
delete [] str;
}
//overloaded operator methods
//assign a String to a String
String & String::operator=(const String &st)
{
if (this == &st)
return *this;
delete [] str;
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
return *this;
}
//assign a C string to a String
String & String::operator=(const char *st)
{
delete [] str;
len = strlen(st);
str = new char[len + 1];
strcpy(str, st);
return *this;
}
//read-write char access for non-const String
char & String::operator[](int i)
{
return str[i];
}
//read-only char access for const string
const char & String::operator[](int i) const
{
return str[i];
}
//overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
return (strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2 < st1;
}
bool operator==(const String &st1, const String &st2)
{
return (strcmp(st1.str, st2.str) == 0);
}
ostream & operator<<(ostream &os, const String &st)
{
os << st.str;
return os;
}
istream & operator>>(istream &is, String &st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
st = temp;
while (is && is.get() != '\n')
continue;
return is;
}
3.using_string.cpp
//using_string.cpp
#include <iostream>
#include "string.h"
using namespace std;
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
String name;
cout << "Hi, what's your name?\n>>";
cin >> name;
cout << name << ", please enter up to " << ArSize
<< " short sayings <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i = 0;
for (; i < ArSize; i++)
{
cout << i + 1 << ":";
cin.get(temp, MaxLen);
while (cin && cin.get() != '\n')
continue;
if (!cin || temp[0] == '\0') //empty line?
break;
else
sayings[i] = temp; //overloaded assignment
}
int total = i; //total # of lines read
if (total > 0)
{
cout << "Here are your sayings:\n";
for (i = 0; i < total; i++)
{
cout << sayings[i][0] << ": " <<sayings[i] << endl;
}
int shortest = 0;
int first = 0;
for (i = 1; i < total; i++)
{
if (sayings[i].length() < sayings[shortest].length())
shortest = i;
if (sayings[i] < sayings[first])
first = i;
}
cout << "shortest saying:\n" << sayings[shortest] << endl;
cout << "First alphabetically:\n" << sayings[first] << endl;
cout << "This program used " << String::HowMany() << " String objects. Bye.\n";
}
else
cout << "no input! Bye.\n";
return 0;
}
运行结果:
Hi, what's your name?
>>Misty Gutz
Misty Gutz, please enter up to 10 short sayings <empty line to quit>:
1:a fool and his money are soon parted
2:penny wise, pound foolish
3:the love of money is the root of much evil
4:out of sight, out of mind
5:absence makes the heart grow fonder
6:absinthe makes the hart grow fonder
7:
Here are your sayings:
a: a fool and his money are soon parted
p: penny wise, pound foolish
t: the love of money is the root of much evil
o: out of sight, out of mind
a: absence makes the heart grow fonder
a: absinthe makes the hart grow fonder
shortest saying:
penny wise, pound foolish
First alphabetically:
a fool and his money are soon parted
This program used 11 String objects. Bye.