自己模拟写C++中的String类型实例讲解
下面是模拟实现字符串的相关功能,它包括一下功能:
String(constchar*s);//利用字符串来初始化对象
String();//默认构造函数
String(constString&s);//复制构造函数,利用String类型来初始化对象
~String();//析构函数
intlength();//返回String类型中字符串的长度
String&operator=(constString&s);//重载=运算符。
String&operator=(constchar*);
char&operator[](inti);//重载【】运算符
constchar&operator[](inti)const;
friendbooloperator<(constString&st,constString&st2);//重载<运算符,用来比较String类型中字符串的大小。
friendbooloperator>(constString&st,constString&st2);
friendbooloperator==(constString&st,constString&st2);//重载==运算符,判断两个String对象是否相等
friendostream&operator<<(ostream&os,constString&st2);//重载输出函数
friendistream&operator>>(istream&is,String&st2);//重载输入函数
staticintHowMang()//返回总共生成的String类对象的数目。
String.h:
#ifndefSTRING_H_INCLUDED #defineSTRING_H_INCLUDED #include"iostream" #includeusingstd::ostream; usingstd::istream; classString{ private: char*str; intlen; public: staticintnum_strings; staticconstintCINLM=80; String(constchar*s); String(); String(constString&s); ~String(); intlength(); String&operator=(constString&s); String&operator=(constchar*); char&operator[](inti); constchar&operator[](inti)const; friendbooloperator<(constString&st,constString&st2); friendbooloperator>(constString&st,constString&st2); friendbooloperator==(constString&st,constString&st2); friendostream&operator<<(ostream&os,constString&st2); friendistream&operator>>(istream&is,String&st2); staticintHowMang() { returnnum_strings; } }; #endif//STRING_H_INCLUDED
String.cpp:
#include#include"String.h" #include"string.h" usingnamespacestd; intString::num_strings=0; intString::length() { returnthis->len; } String::String(constchar*s) { len=strlen(s); str=newchar[len+1]; num_strings++; } String::String() { str=0; len=0; num_strings++; } String::String(constString&s) { num_strings++; len=strlen(s.str); str=newchar[len+1]; strcpy(str,s.str); } String::~String() { --num_strings; delete[]str; len=0; } String&String::operator=(constString&s) { if(this==&s) return*this; delete[]str; len=strlen(s.str); str=newchar[len+1]; strcpy(str,s.str); //num_strings++; } String&String::operator=(constchar*s) { len=strlen(s); str=newchar[len+1]; strcpy(str,s); //num_strings++; } char&String::operator[](inti) { returnstr[i]; } constchar&String::operator[](inti)const { returnstr[i]; } booloperator<(constString&st,constString&st2) { if(strcmp(st.str,st2.str)<0) returntrue; else returnfalse; } booloperator>(constString&st,constString&st2) { return(st 0) returntrue; else returnfalse; } ostream&operator<<(ostream&os,constString&st2) { os< >(istream&is,String&st2) { chartemp[String::CINLM]; is.get(temp,String::CINLM); if(is) st2=temp; while(is&&is.get()!='\n') continue; returnis; }
main.cpp:
#include#include"String.h" usingnamespacestd; intmain() { Stringname[5]; chartemp[10]; inti; for(i=0;i<5;i++) { cin.get(temp,10); while(cin&&cin.get()!='\n') continue; if(!cin&&temp[0]=='\0')//如果是空串的话,cin会为false break; else name[i]=temp; } inttotal=i; intfirs=0,shor=0; if(total<0) { cout<<"没有输入"< 以上这篇自己模拟写C++中的String类型实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。