資源簡介
C++的一般編譯器都定義和封裝了字符串功能,模仿定義string類的實現,可以實現并支持如下功能:
(1)賦值(2)拷貝構造
(3)重載“=”
(4)m.legnth() 函數測量字符串的長度
(5)m.cat(string const &)連接字符串
代碼片段和文件信息
#include
using?namespace?std;
class?str{
?char?*s;
?int?lenth;
public:
str(){s=new?char;}
str(const?str&p);
str(const?char?*p);
~str();
void?print();
int?legnth();
str&?operator=(const?str?&p);
str&?cat(str?const&?p);
};
str&?str::operator=(const?str?&p){
if(this==&p)?{return?*this;}
delete?[]s;s=0;
int?i=0;while(*(p.s+i)!=‘\0‘)i++;
lenth=i;
s=new?char[lenth+1];
for(int?j=0;j ???????s[j]=p.s[j];
???s[lenth]=‘\0‘;
???return?*this;
}
str&?str::cat(str?const&?p){
char?*news=new?char[lenth+p.lenth+1];
for(int?i=0;i news[i]=s[i];
for(int?i=0;i news[lenth+i]=p.s[i];
news[lenth+p.lenth+1]=‘\0‘;
s=news;
lenth=lenth+p.lenth;
return?*this;
}
str::~str(){
if(s==NULL)
return;
delete[]s;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-05-16?07:44??字符串\
?????文件????????1563??2018-05-09?15:47??字符串\syx3.cpp
?????文件?????1982440??2018-05-16?07:44??字符串\syx3.exe
- 上一篇:c++primer第五版源代碼
- 下一篇:C語言ATM簡易ATM 適合新手學習
評論
共有 條評論