几个处理字符串的成员函数:
size_t getsize()const; //返回字符串大小
void clear(); //把字符串清空
bool empty(); //判断字符串是否为空
void swap(MyString &str); //交换两个字符串
int compare(const MyString &str)const; //比较2个字符串的大小
//第一个const说明显式调用的字符串不可更改,括号外面的const说明隐式调用的字符串不可更改,只读数据
int compare(const char*str);
追加函数:
MyString &operator+=(const MyString&str);
MyString &operator+=(const char*str);
MyString &append(const MyString&str);
MyString &append(const char *str);
生成字串:
MyString substr(size_t pos = 0,n=npos) const;
生成字串,从第0个位置开始长度为n,若N超过长度,则为输出整个字符串的长度
4.友元函数(运算符重载):
友元函数一般都是在类得声明中进行定义,它不属于类得成员函数,但是它和类得成员函数一样同样的可以对类得所有数据成员进行访问。
friend bool operator==(const MyString &str1,const MyString &str2);
friend bool operator==(const char *str,const MyString &str2);
friend bool operator==(const MyString &str1,const MyString *str2);
friend bool operator>(const MyString &str1,const MyString &str2);
friend bool operator>(const char*str1,const MyString &str2);
friend bool operator>(const MyString &str1,const char*str2);
同样还有<等各种比较。
friend MyString operator+(const MyString &str1,const MyString &str2);
friend MyString operator+(const char*str1,const MyString &str2); //两个字符串进行相加
friend MyString operator+(const MyString &str1,const char*str2);
friend ostream & operator<<(ostream &os,const MyString &str); //输出命令符的重载
5.成员数据变量:
char *string; //指向字符串的指针
int length; //字符串的长度
static const int string_number = 0; //计数创建的字符串的数目
二、实现.cpp文件:
1.构造函数和析构函数:
MyString::MyString()
{
length = 0;
string = new char;
char *s = "/0";
memcpy(string,s,1);
++string_number;
}
MyString::MyString(const char*str)
{
length = strlen(str);
string = new char(length+1);
memcpy(string,s,length);
++string_number;
}
MyString::MyString(MyString &str)
{
length = str.length;
string = str.string;
++string_number;
}
MyString::~MyString()
{
delete[]string;
--string_number;
}










