4月21日作业

 

#include <iostream>
#include <cstring>
using namespace std;

//定义类
class myString
{
private:
    char *str;
    int size;
    public:
//无参构造
myString():size(32)
{
    str =  new char[size];
    strcpy(str,"");
    cout << "无参构造" << endl;
}

//有参构造
myString(const char *s)
{
    size = strlen(s);
    str = new char[size + 1];
    strcpy(str, s);

    cout << "有参构造" << endl;
}

//拷贝构造
myString(const myString &other)
  {
      size = other.size;
      str = new char[size + 1];
      strcpy(str, other.str);

      cout << "拷贝构造" << endl;
  }
//析构函数
      ~myString()
      {
          delete []str;
          cout << "析构函数" << endl;
      }
//判空函数
      bool empty()
     {
         if(strlen(str) == 0)
             return true;
         else
             return false;
     }
//size函数
     int mysize()
     {
         return size;
     }

     //c_str函数
     const char *c_str()
     {
         return str;
     }
     //at函数
        char &at(int i)
        {
            if(i >= size || i < 0)
            {
                cout << "段错误" << endl;
            }
            return str[i];
        }
        //展示字符串
        void show()
        {
            cout<<str<<endl;
        }
        //拷贝赋值
        myString & operator=(const myString &R)
        {
            delete []str;
            str=new char[size];
            this->size=R.size;
            this->str=strcpy(this->str,R.str);
            return *this;
        }
        //中括号[]重载
        char operator[](int t)
        {
            return this->str[t];
        }
        //+重载 连接2个字符串
        const myString operator+(const myString &R)
        {
            myString temp;
            delete []temp.str;
            temp.str=new char[this->size+R.size+1];
            temp.str=strcat(this->str,R.str);
            temp.size=this->size+R.size;
            return temp;
        }
        //==重载 判定2个字符串是否相等
        bool operator==(const myString &R)const
        {
            bool a;
            if(0==strcmp(this->str,R.str))
                a=1;
            else
                a=0;
            return a;
        }
         //<重载
        bool operator<(const myString &R)const
        {
            bool a;
            if(0<strcmp(this->str,R.str))
                a=1;
            else
                a=0;
            return a;
        }
        //>重载
       bool operator>(const myString &R)const
       {
           bool a;
           if(0>strcmp(this->str,R.str))
               a=1;
           else
               a=0;
           return a;
       }
       friend ostream &operator<<(ostream &L,const myString &O);
       friend istream &operator>>(istream &L, myString &O);
};
//输出重载<<
ostream &operator<<(ostream &L,const myString &O)
{

    L<<O.str;
    return L;
}
//输入重载>>
istream &operator>>(istream &L, myString &O)
{

    L>>O.str;
    return L;
}

int main()
    {
        myString s1;//无参构造

        myString s2("hello");//有参构造
        myString s3(s2);//拷贝构造
        cout<<"s3=";
        s3.show();
        int a=s2.mysize();//计算长度
        cout<<"s2长度="<<a<<endl;
        myString s4("aaa");
        myString s5;
        s5=s4;//赋值
        cout<<"s5="<<s5<<endl;//打印s5字符串
        cout<<"s5[2]="<<s5[2]<<endl;
        myString s6;
        s6=s4+s5;//连接字符串
        cout<<"s6="<<s6<<endl;
        myString s7;
        cin>>s7;
        if(s7<s6)
            cout<<"s7<s6";
        else if(s7>s6)
            cout<<"s7>s6";
        else if(s7==s6)
            cout<<"s7=s6";



    }

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>