String.h
#ifndef _CPlusPlusPrimer_chapter3_string
#define _CPlusPlusPrimer_chapter3_string
#include <iostream>
using namespace std;
namespace Icanth_CPlusPlusPrimer
{
class String;
// 输入输出操作作为非成员函数实现的
istream& operator>> ( istream&, String &);
ostream& operator<< ( ostream&, String &);
class String
{
public:
// 构造函数
String();
String(const String &); // 串拷贝函数
String(const char*);
// 析构函数
~String();
// 重载下标操作符
char &operator[](int );
bool operator==(const String &);
bool operator==(const char*);
bool operator!=(const String &);
bool operator!=(const char*);
String &operator=(const String &);
String &operator=(const char *);
// 成员函数定义
int size() { return _size; }
char * c_str() { return _string; }
private:
int _size;
char* _string;
};
}
#endif
String.cpp
#include "string.h"
#include <stdlib.h>
#include <string.h>
#include <iomanip> // setw
using namespace Icanth_CPlusPlusPrimer;
inline
String::String()
{
_size = 0;
_string = 0;
}
inline
String::String(const char* str)
{
if(!str)
{
_size = 0;
_string = 0;
}
else
{
_size = strlen(str);
_string = new char[_size+1]; // 用于存最后一个字符串'/0'结束符
strcpy(_string, str);
}
}
inline
String::String(const String &rhs)
{
_size = rhs._size;
if(!rhs._size)
{
_string = 0;
}
else
{
_string = new char[_size + 1];
strcpy(_string, rhs._string);
}
}
inline
String::~String()
{
_size = 0;
delete[] _string;
}
inline String&
String::operator=(const char *s)
{
if( ! s )
{
_size = 0;
delete [] _string;
_string = 0;
}
else
{
_size = strlen(s);
delete[] _string;
_string = new char[ _size + 1 ];
strcpy(_string, s);
}
return *this;
}
inline istream&
Icanth_CPlusPlusPrimer::operator>>( istream &io, String &s)
{
// 人工限制输入最大4096字符
const int limit_string_size = 4096;
char inBuf [ limit_string_size ];
io >> setw(limit_string_size) >> inBuf;
s = inBuf; // String::operator=(const char&)
return io;
}
inline ostream&
Icanth_CPlusPlusPrimer::operator<< ( ostream& io,String & s)
{
const char* str = s.c_str();
io << str;
return io;
}
inline bool
String::operator==(const String &s)
{
if(_size != s._size)
return false;
if(0 == strcmp(_string, s._string))
{
return true;
}
else
{
return false;
}
}
inline bool
String::operator==(const char* s)
{
if( _size != strlen(s) )
{
return false;
}
else
{
if(0 == strcmp(_string, s))
{
return true;
}
else
{
return false;
}
}
}
inline bool
String::operator!=(const String &s)
{
return !((*this) == s);
}
inline bool
String::operator!=(const char* s)
{
return !((*this) == s);
}
inline char &
String::operator[](int index)
{
return _string[index];
}
test.cpp
#include "string.cpp"
#include <iostream>
namespace ICP = Icanth_CPlusPlusPrimer;
using namespace std;
int main()
{
char *str = "This is a test string stored in char*";
using ICP::String;
String str1;
String str2(str);
String str3(str2);
cout << "Testing str2 is equal to str3 with [bool operator==(String &)]: \n" << (str2 == str3) << endl;
cout << "Testing str3 is equal to str with [bool operator==(char *)]: \n" << (str3 == str) << endl;
cin >> str1;
cout << "The str1 that u just input is :\n" << str1 << endl;
cout << "Testing str1 is not equal to str with [bool operator!=(char *)]: \n" << (str3 != str1) << endl;
cout << "The str3[2] is:" << str3[2] << endl;
return 0;
}
运行截图: