⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringop.cpp

📁 做为SD0609的一员,我扎实的基础是如何打出来的,来看看我那时连猴子选大王都不会的人,是如何成长起来的吧!主要包括我所做的C++,JAVA基础联系.乱是乱了点,不过如果真心想学习,打好基础,看看也不
💻 CPP
字号:
/*******************************************
* 字符串运算符重载演示
* 构造、析构函数,拷贝构造函数,重载=
* 重载+,+=,==,(int),<<,>>
* 达内,陈宗权,2006/8
*******************************************/
#include <iostream>
using namespace std;

class Str{
    char * str;
    int len;
public:
    Str();
    Str(const char* sz);
    Str(const Str&);
    ~Str();
    int Size() const;
    Str& operator=(const Str&);
    friend Str operator+(const Str&,const Str&);
    Str& operator+=(const Str&);
    operator int()const;
    friend bool operator==(const Str&,const Str&);
    friend ostream& operator<<(ostream& o, const Str&);
    friend istream& operator>>(istream& i,Str&);
};
Str::Str()
{
    len=100;
    str = new char[100];
    if( str==NULL )
        len=0;
    else
        *str='\0';//str[0]='\0';zero-length
}
Str::Str( const char* sz )
{
    if( sz )
        len=strlen(sz)+1;
    else
        len=100;
    str = new char[len];
    if( str==NULL )
        len=0;
    else{
        if( sz )
            strcpy( str, sz );
        else
            *str=0;
    }
}
Str::Str(const Str& cs)
:str(NULL),len(0)
{
    *this=cs;//this->operator=(cs);
}
Str::~Str()
{
    if( str )
        delete[] str;
    str = NULL;
    len = 0;
}
Str& Str::operator=( const Str& cs )
{
    if( cs.str==NULL ){
        if(str)
            delete[]str;
        str=NULL;
        return *this;
    }
    int srclen= strlen(cs.str)+1;
    if( srclen>len ){
        if( str )
            delete[] str;
        str = new char[srclen];
        if( str==NULL )
            return *this;
        len=srclen;
    }
    strcpy( str, cs.str );
    return *this;
}
int Str::Size()const
{
    return str?strlen(str):0;
}
Str& Str::operator+=( const Str& app )
{
    if( app.str==NULL )
        return *this;
    int total=Size()+app.Size()+1;
    if( total>len ){
        if( !str )
            return *this=app;
        char* temp=new char[total];
        if( temp==NULL )
            return *this;
        strcpy( temp, str );
        delete[] str;
        str = temp;
        len = total;
    }
    //strcat( str, app.str );
    strcpy( str+strlen(str), app.str );
    return *this;
}
Str operator+(const Str& s1, const Str& s2)
{
    Str s=s1;
    s+=s2;
    return s;
}
bool operator==(const Str& s1, const Str& s2)
{
    return strcmp(s1.str,s2.str)==0;
}
Str::operator int()const
{
    int sum=0;
    for(int i=0;i<Size();i++)
        sum+=str[i];
    return sum;
}
ostream& operator<<(ostream&o,const Str&cs)
{
    o<<cs.str;
    return o;
}
istream& operator>>(istream&i, Str&s)
{
    char buf[500];
    i>>buf;
    s=buf;
    return i;
}

int main()
{
    Str s1;
    Str s2("Hello");
    Str s3(s2);
    s1=",world!";
    cout<<"s1="<<s1<<endl;
    cout<<"s2="<<s2<<endl;
    cout<<"s3="<<s3<<endl;
    cout<<"s2==s3?"<<(s2==s3)<<endl;
    cout<<"s1==s3?"<<(s1==s3)<<endl;
    s3=s2+s1;
    s2+=s1;
    cout<<"s1="<<s1<<endl;
    cout<<"s2="<<s2<<endl;
    cout<<"s3="<<s3<<endl;
    cout<<"(int)s1="<<(int)s1<<endl;
    cout<<"please input a word:"<<endl;
    cin>>s1;
    cout<<"s1="<<s1<<endl;
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -