1. C++ 实现mystring类

mystring.h

#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <string.h>
using namespace std;

class mystring;
class sender
{
public:
    ostream& operator<<(mystring const & s);

};


class mystring
{
    friend ostream& sender::operator<<(mystring const & s);

    friend ostream& operator <<(ostream & co,mystring const & s);
    friend istream& operator >>(istream & ci,mystring & s);
public:
#if 0
    mystring();
    mystring(char* p);
#endif
#if 1
    mystring(char* p = nullptr);//默认参数只能放在声明处,且这里用于做标志位。
#endif
    ~mystring();//析构
    mystring(const mystring & another);
    mystring& operator=(const mystring &another);
    mystring operator+(const mystring &another);
    mystring& operator+=(const mystring &another);
    bool operator>=(mystring const &another);
    char& operator[](int const & idx);
    mystring& operator++();
    const mystring operator++(int);
    char* c_str();
    void printstr();
private:
    char* _str;
};

ostream& operator <<(ostream & co,mystring const& s);
istream& operator >>(istream & ci,mystring & s);



#endif // MYSTRING_H


mystring.cpp

#include <iostream>
#include "mystring.h"
#include <string.h>
using namespace std;


ostream& sender::operator<<(mystring const & s)
{
    cout<<s._str;
    return cout;
}

#if 0
mystring::mystring(char* p)
{
    int len = strlen(p);
    this->_str = new char[len+1];
    strcpy(this->_str,p);
}
mystring::mystring()
{
    this->_str = new char[1];
    *(this->_str) = '\0';
}
#endif

#if 1
mystring::mystring(char* p)
{
    if(p)
    {
        int len = strlen(p);
        this->_str = new char[len+1];
        strcpy(this->_str,p);
    }
    else
    {
        this->_str = new char[1];
        *(this->_str) = '\0';
    }
}
#endif


mystring::mystring(const mystring & another)
{
     int len = strlen(another._str);//同类之间无私处
     this->_str = new char[len+1];
     strcpy(this->_str, another._str);
}

mystring::~mystring()
{
    cout<<"~mystring"<<endl;
    delete[] _str;
}


mystring& mystring::operator=(const mystring &another)
{
    if(this != &another)//防止自赋值
    {
        delete[]this->_str;//防止内存泄漏
        int len = strlen(another._str);
        this->_str = new char[len+1];
        strcpy(this->_str,another._str);
    }
    return *this;
}

mystring mystring::operator+(const mystring &another)
{
    mystring sumstr;
    delete[]sumstr._str;
    sumstr._str = new char[strlen(this->_str)+strlen(another._str)+1]{0};//初始化为0不可少
    strcpy(sumstr._str,this->_str);
    strcat(sumstr._str,another._str);
    return sumstr;
}

#if 0
mystring& mystring::operator+=(const mystring &another)
{
    char *sumstr = new char[strlen(this->_str)+strlen(another._str)+1];
    strcpy(sumstr,this->_str);
    strcat(sumstr,another._str);
    delete[]this->_str;
    this->_str = sumstr;
    return *this;
}
#endif

#if 1
mystring& mystring::operator +=(const mystring &another)
{
    int len = strlen(this->_str)+strlen(another._str);
    this->_str = (char*)(realloc(this->_str,len+1));
  //  this->_str = static_cast<char*>(realloc(this->_str,len+1));
    memset(this->_str+strlen(this->_str),0,strlen(another._str)+1);
    strcat(this->_str,another._str);
    return *this;
}
#endif

bool mystring::operator >=(mystring const &another)
{
    return strcmp(this->_str,another._str)>=0;
}

char& mystring::operator[](int const & idx)
{
    return this->_str[idx];
}

mystring& mystring::operator++()//前++
{
    char* p = _str;
    while(*p)
    {
        (*p)++;
        p++;
    }
    return *this;
}

const mystring mystring::operator++(int)//后++
{
    mystring stemp(*this);
    char* p = _str;
    while(*p)
    {
        (*p)++;
        p++;
    }
    return stemp;
}

char* mystring::c_str()
{
    return this->_str;
}
void mystring::printstr()
{
    cout<<_str<<endl;
}

ostream& operator <<(ostream & co,mystring const& s)
{
    co<<s._str;
    return co;
}
istream& operator >>(istream & ci,mystring & s)
{
    ci>>s._str;
    return ci;
}



main.c

#include <iostream>
#include "mystring.h"
#include <string>
using namespace std;

int main()
{
#if 0
//生成对象,需满足的各种生成对象方式
//调用构造函数
    mystring s1("china");//有参定义string对象
    s1.printstr();
    mystring ss11 = "china";
    ss11.printstr();

    mystring s2;//无参定义string对象的逻辑,其实本质是传入了一个nullptr空指针
    s2.printstr();

    mystring s3("");//有参定义string对象,传入空串,"",只包含一个'\0'
    s2.printstr();

    mystring *sp4 = new mystring("china");
    sp4->printstr();
    delete sp4;

    mystring *sp5 = new mystring;
    sp5->printstr();
    delete sp5;

//调用拷贝构造
    mystring s6 = s1;
    mystring s7(s1);
    mystring *sp8 = new mystring(s1);
    delete sp8;

//调用了转换构造
//=
    //防止重析构,防止内存泄漏,防止自赋值。

    mystring s9;
    s9 = s1;
    s9 = s2 = s1;
    s9 = (s2 = s1);
    (s9 = s2) = s1;
   // s9 = s3;
//+
    mystring s10 = s1+s2;
    s10 = s1+s2;
    s10 = s1+s2+s3;
    s10 = (s1+s2)+s3;
    (s9 + s10) = s1;
//+=
   mystring s11  = (s2 += s1);
    mystring s12(s2+=s1);
    mystring s13 = s1 += s2;
    s11 += s1;
    s11 += s1 += s2;

    //mystring s11;
   // s11 += s1 += s2;

  //  s11.operator +=(s1.operator+=(s2));
//>=
//[]
    mystring s14("china");
    s14[2] = 't';
    cout<<"s14:";
    //s14.printstr();
//c_str
    
//<< >>
    cout<<s14<<endl;
    mystring s15;
   // cin>>s15;
    cout<<"s15:"<<s15<<endl;
//前++ 后++
    mystring s16("aaaaa");
  //  cout<<"s16:"<<++s16<<endl;
  //  cout<<"s16:"<<s16<<endl;
  //  cout<<"s16:"<<++++s16<<endl;
  //  cout<<"s16:"<<s16<<endl;
    cout<<"s16:"<<s16++<<endl;
    cout<<"s16:"<<s16<<endl;

#endif

//转换构造函数
    mystring s17("aaaaa");
    s17 = "bbbbb";
   // cout<<"s17::"<<s17<<endl;

//sender<<s17
    cout<<"s17::";
    sender sd;
    sd<<s17<<endl<<"21312321"<<endl<<endl<<endl;//<<endl;

    sd<<"abcd"<<endl;

    return 0;
}

//explicit修饰符放在声明还是定义处?


 

猜你喜欢

转载自blog.csdn.net/CHEN_JYXHM/article/details/82934104