类中运算符的重载应用——字符串类
一、实验内容
定义字符串类,并对构造函数进行重载,对主要运算符进行重载,定义对字符串进行操作的其它函数,通过以上操作实现对字符串的基本操作。
二、功能模块简介
1.定义字符串类
class Tstring
{
public:
//构造函数
TString();
//带有TString类常量的构造函数
TString(const TString& src);
//带有字符指针参变量的构造函数
TString(char* src);
//析构函数
~TString();
private:
char* m_pBuf;
}
2.字符串赋值操作:赋值运算符"="重载
public:
TString& operator=(const TString& src);//对赋值运算符"="进行重载
TString& operator=(char* src);
3.字符串连接操作:连接运算符"+"重载
public:
TString& operator+(const TString& src);//对连接运算符"+"进行重载
TString& operator+(char* src);
4.字符串连接、赋值操作:赋值运算符"+="重载
public:
TString& operator+=(const TString& src);//对连接、赋值运算符"+="进行重载
TString& operator+=(char* src);
5.求字符串中的单个字符元素操作:下标运算符"[]"重载
public:
char& operator[](int nIndex);//对下标运算符"[]"进行重载
6.求字符串的子串
1)求指定的字符串的左边几个字符
public:
TString Left(int nCount);//求左边几个字符
2)求指定的字符串的右边几个字符
public:
TString Right(int nCount);//求右边几个字符
3)求指定的字符串的某个位置开始的几个字符
public:
TString Mid(int nPos,int nCount);//求某个位置开始的几个字符
7.求字符串的长度
public:
int Length();//求字符串的长度
8.判断字符串是否为空
public:
bool IsEmpty();//判断字符串是否为空
9.清空字符串
public:
void Empty();//清空字符串
10.输出字符串
public:
void Print();//输出字符串
11.字符串类内部函数(私有函数)
private:
void SetValue(char* src);
三、程序代码
#include
#include
class TString
{
public:
//构造函数
TString();
//带有TString类常量的构造函数
TString(const TString& src);
//带有字符指针参变量的构造函数
TString(char* src);
//析构函数
~TString();
public:
//求字符串的长度
int Length();
//判断字符串是否为空
bool IsEmpty();
//清空字符串
void Empty();
//输出字符串
void Print();
private:
void SetValue(char* src);
public:
//对下标运算符"[]"进行重载
char& operator[](int nIndex);
//对赋值运算符"="进行重载
TString& operator=(const TString& src);
TString& operator=(char* src);
//对连接运算符"+"进行重载
TString& operator+(const TString& src);
TString& operator+(char* src);
//对连接、赋值运算符"+="进行重载
TString& operator+=(const TString& src);
TString& operator+=(char* src);
public:
//求左边几个字符
TString Left(int nCount);
//求右边几个字符
TString Right(int nCount);
//求某个位置开始的几个字符
TString Mid(int nPos,int nCount);
private:
char* m_pBuf;
};
//-------------------------------
TString::TString()
{
m_pBuf = NULL;
}
TString::TString(const TString& src)
{
this->SetValue(src.m_pBuf);
}
TString::TString(char* src)
{
this->SetValue(src);
}
TString::~TString()
{
if(m_pBuf)
delete[] m_pBuf;
}
//---------------------------------------------------
int TString::Length()
{
if(m_pBuf==NULL)
return 0;
return strlen(m_pBuf)+1;
}
bool TString::IsEmpty()
{
if(m_pBuf==NULL)
return true;
return false;
}
void TString::Empty()
{
if(m_pBuf)
{
delete[] m_pBuf;
m_pBuf = NULL;
}
}
void TString::Print()
{
if(m_pBuf==NULL)
cout<<""<m_pBuf)
return *this;
//以前有分配空间
if(m_pBuf)
delete[] m_pBuf;//释放以前的分配空间
SetValue(src);
return *this;
}
TString& TString::operator+(const TString& src)
{
TString *temp;
temp=new TString();
temp->m_pBuf=NULL;
//传来的对象为空
if(src.m_pBuf == NULL)
return *this;
if(m_pBuf==src.m_pBuf)
{
int nLen=2*strlen(src.m_pBuf)+1;
temp->m_pBuf=new char[nLen];
strcpy(temp->m_pBuf,src.m_pBuf);
strcat(temp->m_pBuf,src.m_pBuf);
}
else if(m_pBuf)
{
int nLen = strlen(src.m_pBuf)+strlen(m_pBuf)+1;
temp->m_pBuf = new char[nLen];
strcpy(temp->m_pBuf,m_pBuf);
strcat(temp->m_pBuf,src.m_pBuf);
}
else
{
int nLen = strlen(src.m_pBuf)+1;
temp->m_pBuf = new char[nLen];
strcpy(temp->m_pBuf,src.m_pBuf);
}
return *temp;
}
TString& TString::operator+(char* src)
{
TString *temp;
temp=new TString();
temp->m_pBuf=NULL;
if(src==NULL)
return *this;
else if(m_pBuf)
{
int nLen = strlen(m_pBuf)+strlen(src)+1;
temp->m_pBuf = new char[nLen];
strcpy(temp->m_pBuf,m_pBuf);
strcat(temp->m_pBuf,src);
}
else
{
int nLen = strlen(src)+1;
temp->m_pBuf=new char[nLen];
strcpy(temp->m_pBuf,src);
}
return *temp;
}
TString& TString::operator+=(const TString& src)
{
if(src.m_pBuf==NULL)
return *this;
if(m_pBuf==src.m_pBuf)
{
int nLen=2*strlen(src.m_pBuf)+1;
char *temp=src.m_pBuf;
m_pBuf=new char[nLen];
strcpy(m_pBuf,temp);
strcat(m_pBuf,temp);
}
else if(m_pBuf)
{
int nLen=strlen(src.m_pBuf)+strlen(m_pBuf)+1;
char* temp=m_pBuf;
m_pBuf=new char[nLen];
strcpy(m_pBuf,temp);
strcat(m_pBuf,src.m_pBuf);
}
else
{
int nLen=strlen(src.m_pBuf)+1;
m_pBuf=new char[nLen];
strcpy(m_pBuf,src.m_pBuf);
}
return *this;
}
TString& TString::operator+=(char* src)
{
if(src==NULL)
return *this;
if(m_pBuf)
{
int nLen=strlen(m_pBuf)+strlen(src)+1;
char *temp=m_pBuf;
m_pBuf=new char[nLen];
strcpy(m_pBuf,temp);
strcat(m_pBuf,src);
}
else
{
int nLen=strlen(src)+1;
m_pBuf=new char[nLen];
strcpy(m_pBuf,src);
}
return *this;
}
//---------------------------------------------------
//求左边几个字符
TString TString::Left(int nCount)
{
TString temp;
int i;
if(m_pBuf==NULL)
temp.m_pBuf=NULL;
else if(nCount>Length())
temp.m_pBuf=NULL;
else
{
temp.m_pBuf=new char[nCount+1];
for(i=0;iLength())
temp.m_pBuf=NULL;
else
{
int nLength=Length()-1;
temp.m_pBuf=new char[nCount+1];
for(i=nLength-1,j=nCount-1;i>=(nLength-nCount)&&j>=0;i--,j--)
temp.m_pBuf[j]=m_pBuf[i];
if(temp.m_pBuf[j-1]!='\0')
temp.m_pBuf[nCount]='\0';
}
return temp;
}
//某个位置开始的几个字符
TString TString::Mid(int nPos,int nCount)
{
TString temp;
int i,j;
if(m_pBuf==NULL)
temp.m_pBuf=NULL;
else if(nPos<1||nPos>=Length()||nCount<1||nCount>Length())
temp.m_pBuf=NULL;
else if((nPos+nCount)>Length())
temp.m_pBuf=NULL;
else
{
int nLength=Length()-1;
temp.m_pBuf=new char[nCount+1];
for(i=nPos-1,j=0;i<(nPos+nCount)&&j