欢迎来到天天文库
浏览记录
ID:37914769
大小:47.50 KB
页数:13页
时间:2019-06-02
《C++程序代码-类串示范程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#include#include"stringclass.h"String::String(constchar*c)//缺省构造函数{size=strlen(c);str=newchar[size+1];if(str==NULL)Error("String:overflow!");strcpy(str,c);}String::String(constString&s)//复制构造函数{size=s.
2、size;str=newchar[size+1];if(str==NULL)Error("String:overflow!");strcpy(str,s.str);}String::~String(){delete[]str;}voidString::Error(constchar*c)const{cout<3、w.str=newchar[len+1];if(w.str==NULL)Error("operator+:overflow!");strcpy(w.str,str);strcat(w.str,s.str);w.size=len;return(w);}StringString::operator+(constchar*c)const//类串+C串{Stringw;intlen=size+strlen(c);delete[]w.str;w.str=newchar[len+1];if(w.str==NULL)Error("oper4、ator+:overflow!");strcpy(w.str,str);strcat(w.str,c);w.size=len;return(w);//相当于String_temp=w;调用复制构造函数}Stringoperator+(constchar*c,constString&s1)//C串+类串{Stringw;//不能简化为return(s1+c),因为连接后的串顺序不对delete[]w.str;w.size=strlen(c)+s1.size;w.str=newchar[w.size+1];if(w.str==N5、ULL)//友元不能直接调用私有函数Errors1.Error("operator+:overflow!");strcpy(w.str,c);strcat(w.str,s1.str);return(w);}String&String::operator=(constString&s)//复制赋值:类串=类串{if(size!=s.size){delete[]str;str=newchar[s.size+1];if(str==NULL)Error("operator=:overflow!");size=s.size;}strc6、py(str,s.str);return(*this);}String&String::operator=(constchar*c)//转换赋值:类串=C串{intlen=strlen(c);if(size!=len){delete[]str;str=newchar[len+1];if(str==NULL)Error("operator=:overflow!");size=len;}strcpy(str,c);return(*this);}StringString::SubStr(intid,intnum)const//取子7、串{Strings;intlen=size;intleft=len-id,i;if(id<08、9、id>len-1)Error("idillegal!");if(num<=010、11、num>left)Error("numillegal!");delete[]s.str;s.str=newchar[num+1];if(s.str==NULL)Error("overflow!");s.size=num;char*p=str+id;char*p1=s.str;for(i=1;i<=num;i++)*p1++=*p++;*p1=' ';12、return(s);}String&String::Insert(intid,constString&s)//子串插入{char*p,*p1,*buf;intlen=size;//记录本串长度intlen1=s.size;//记录子串长度intleft=len-id;//计算后段长度int
3、w.str=newchar[len+1];if(w.str==NULL)Error("operator+:overflow!");strcpy(w.str,str);strcat(w.str,s.str);w.size=len;return(w);}StringString::operator+(constchar*c)const//类串+C串{Stringw;intlen=size+strlen(c);delete[]w.str;w.str=newchar[len+1];if(w.str==NULL)Error("oper
4、ator+:overflow!");strcpy(w.str,str);strcat(w.str,c);w.size=len;return(w);//相当于String_temp=w;调用复制构造函数}Stringoperator+(constchar*c,constString&s1)//C串+类串{Stringw;//不能简化为return(s1+c),因为连接后的串顺序不对delete[]w.str;w.size=strlen(c)+s1.size;w.str=newchar[w.size+1];if(w.str==N
5、ULL)//友元不能直接调用私有函数Errors1.Error("operator+:overflow!");strcpy(w.str,c);strcat(w.str,s1.str);return(w);}String&String::operator=(constString&s)//复制赋值:类串=类串{if(size!=s.size){delete[]str;str=newchar[s.size+1];if(str==NULL)Error("operator=:overflow!");size=s.size;}strc
6、py(str,s.str);return(*this);}String&String::operator=(constchar*c)//转换赋值:类串=C串{intlen=strlen(c);if(size!=len){delete[]str;str=newchar[len+1];if(str==NULL)Error("operator=:overflow!");size=len;}strcpy(str,c);return(*this);}StringString::SubStr(intid,intnum)const//取子
7、串{Strings;intlen=size;intleft=len-id,i;if(id<0
8、
9、id>len-1)Error("idillegal!");if(num<=0
10、
11、num>left)Error("numillegal!");delete[]s.str;s.str=newchar[num+1];if(s.str==NULL)Error("overflow!");s.size=num;char*p=str+id;char*p1=s.str;for(i=1;i<=num;i++)*p1++=*p++;*p1=' ';
12、return(s);}String&String::Insert(intid,constString&s)//子串插入{char*p,*p1,*buf;intlen=size;//记录本串长度intlen1=s.size;//记录子串长度intleft=len-id;//计算后段长度int
此文档下载收益归作者所有