22081-11-7 c++作业

## 作业:

在昨天my_string的基础上,将能重载的运算符全部重载掉
关系运算符:>、<、==、>=、<=、!=
加号运算符:+
取成员运算符:[]
赋值运算符: =

#include <iostream>
#include <string>
using namespace std;
class my_string
{
private:
    char *str;
    int len;
public:
    my_string(){}//无参构造函数
    ~my_string(){}//析构函数
    my_string(char *p)//有参构造函数
    {
        len=my_size(p);
        str=new char[len];
        for(int i=0;i<len;i++)
        {
            str[i]=*(p+i);
        }
    }
     my_string(my_string& s)//拷贝构造函数
          {
              this->len=s.len;
              str=new char[len];
              for(int i=0;i<len;i++)
              {
                  str[i]=s.str[i];
              }
          }
     //加号运算符
     my_string& operator+(my_string& R)
     {
         int i;
         my_string tmp;
         R.len=my_size(R.str);
         tmp.len=this->len+R.len;
//         cout<<len<<endl;//5
//         cout<<R.len<<endl;//7
//         cout<<tmp.len<<endl;//12
         tmp.str=new char[tmp.len];
         for(i=0;i<tmp.len;i++)
         {
             if(i<this->len)//i<5
             {
                 tmp.str[i]=str[i];
                 //cout<<tmp.str[i]<<endl;
             }
             else//i=5
             {
                 tmp.str[i]=R.str[i-(this->len)];
                 //cout<<tmp.str[i]<<endl;
             }
         }
         return tmp;
     }
     //关系运算符==
     bool operator ==(my_string&S)
     {
         int i;
         for(i=0;i<len;i++)
         {
             if(str[i]!=S.str[i])
             {
                 break;
             }

         }
        // cout<<i<<endl;
         if((this->len==S.len)&&(i==this->len))
         {
             return true;
         }
         else
         {
             return false;
         }

     }
     //关系运算符>
     bool operator >(my_string&S)
     {
         int i;
         for(i=0;i<len;i++)
         {
             if(str[i]!=S.str[i])
             {
                 break;
             }

         }
         if(str[i]>S.str[i])
         {
             return true;
         }
         else
         {
             return false;
         }

     }
     //关系运算符<
     bool operator <(my_string&S)
     {
         int i;
         for(i=0;i<len;i++)
         {
             if(str[i]!=S.str[i])
             {
                 break;
             }

         }
         if(str[i]<S.str[i])
         {
             return true;
         }
         else
         {
             return false;
         }
     }
     //关系运算符>=
     bool operator >=(my_string&S)
     {
         int i;
         for(i=0;i<len;i++)
         {
             if(str[i]!=S.str[i])
             {
                 break;
             }

         }
         if(str[i]>=S.str[i])
         {
             return true;
         }
         else
         {
             return false;
         }
     }
     //关系运算符<=
     bool operator <=(my_string&S)
     {
         int i;
         for(i=0;i<len;i++)
         {
             if(str[i]!=S.str[i])
             {
                 break;
             }

         }
         if(str[i]<=S.str[i])
         {
             return true;
         }
         else
         {
             return false;
         }
     }
     //关系运算符!=
     bool operator !=(my_string&S)
     {
         int i;
         for(i=0;i<len;i++)
         {
             if(str[i]!=S.str[i])
             {
                 break;
             }

         }
         if((this->len==S.len)&&(i==this->len))
         {
             return false;
         }
         else
         {
             return true;
         }
     }
      my_string& operator=(my_string &R)//拷贝赋值函数(赋值运算符=)
      {
          this->len=R.len;
          this->str=new char[len];
          for(int i=0;i<len;i++)
          {
              str[i]=R.str[i];
          }
          return *this;
      }

    void show()
    {
       cout<<"str="<<this->str<<endl;
    }
    //长度
    int my_size(char *s)
    {
        int len=0;
        while(*s!='\0')
        {
            len++;
            s++;
        }
        return len;
    }
    //判空
    bool my_empty()
    {
      return len==0?true:false;
    }
    //
    char my_at(int i)
    {
        return str[i];
    }

};
int main()
{
    my_string s1("hqykj");
    s1.show();
    my_string s2(s1);
    s2.show();
    my_string s3;
    s3=s2;
    s3.show();
    if(s1.my_empty()==true)
    {
        cout<<"null"<<endl;
    }
    else
    {
        cout<<"full"<<endl;
    }
    //关系运算符
    my_string s4("hqykjp");
    s4.show();
    cout<<(s1==s4?"yes":"no")<<endl;
    cout<<(s1>s4?"yes":"no")<<endl;
    cout<<(s1<s4?"yes":"no")<<endl;
    cout<<(s1>=s4?"yes":"no")<<endl;
    cout<<(s1<=s4?"yes":"no")<<endl;
    cout<<(s1!=s4?"yes":"no")<<endl;
    my_string s5;
    s5=s1+s4;
    s5.show();
    cout << "Hello World!" << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值