自定义String类

来源:互联网 发布:mac os x系统怎么安装 编辑:程序博客网 时间:2024/05/16 11:29
//一个自建的MyString类#include <iostream>#include <cstring>using namespace std;class String{    private:        char * str;        int len;        static int num_strings;//number of objects;    public:        //constructors and other methods;        String(const char* s);//constructor        String();//default constructor        String(const String &);//copy constructor        ~String();//destructor        int length() const {return len;}        //overloaded operator methods        String & operator=(const String &);        String & operator=(const char*);        char & operator[](int i);        const char & operator[](int i) const;    //  String & operator + (String & ) ;//overloaded +(改变被加对象)        //overloaded operator friends;        friend bool operator<(const String &st1, const String &st2);         friend bool operator>(const String &st1, const String &st2);         friend bool operator==(const String &st1, const String &st2);         friend ostream & operator<<(ostream & os, const String &st);        friend String & operator+(String & left, String & right);         //static function        static int HowMany();   }; //initializing static class memberint String::num_strings=0;//static methodint String::HowMany(){    return num_strings;}//class methodsString::String(const char * s){ //construct String from C string    len=strlen(s);    str=new char[len+1];    strcpy(str,s);    num_strings++;}String::String(){//default constructor    len=0;    str=new char[1];//不适用new char,是为了与析构函数delete []str兼容    str[0]='\0';   //default string    num_strings++;}String::String(const String & st){//copy constructor    num_strings++;    len=st.len;    str=new char[len+1];    strcpy(str,st.str);}String::~String(){    --num_strings;    delete [] str;}//overloaded operator methodsString & String::operator=(const String & st){//assign a String to a String    if(this==&st)    return *this;    if(str!=NULL)    delete [] str;//先释放当前对象的堆内存     len=strlen(st.str);    str=new char[len+1];//然后分配足够大小长度的堆内存复制字符串     strcpy(str,st.str);    return *this;}/*String & String::operator +(String & st) //重载+ (改变被加对象) {    char * temp=str;    str=new char[strlen(temp)+strlen(st.str)+1];    strcpy(str,temp);     //复制第一个字符     delete [] temp;    strcat(str,st.str);   //连接第二个字符     return *this;} *///assign a C string to a StringString & String::operator=(const char * s){    delete [] str;    len=strlen(s);    str=new char[len+1];    strcpy(str,s);    return * this;}//read-write char access for non-const Stringchar & String::operator[](int i){    return str[i];}//read-only char access for const Stringconst char & String::operator[](int i) const{    return str[i];}//overloaded operator friendsbool operator<(const String &st1,const String &st2){    return strcmp(st1.str,st2.str)<0;}bool operator>(const String &st1,const String &st2){    return st2<st1;}bool operator==(const String & st1, const String & st2){    return strcmp(st1.str,st2.str)==0;}//simple String outputostream & operator<<(ostream & os, const String & st){    os<<st.str;    return os;}//友元实现重载+,不改变被加对象  String & operator+(String & left, String & right) {    String *pString=new String(" ");     pString->str=new char[strlen(left.str)+strlen(right.str)+1];     strcpy(pString->str,left.str);     strcat(pString->str,right.str);      return *pString; } int main(){    String a("hello ");    String b("world\n");    String c=a+b;    cout<<a<<b;     cout<<"overloaded +: "<<c;     cout<<"operator []:"<<c[4]<<endl; }
1 0
原创粉丝点击