C++第一次试水写cstring类

#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class cstring
{
      public:
                 cstring(const char*p=NULL);
                 cstring(const cstring &b);//拷贝构造函数必须要用const类型,否则极有可能出现拷贝中的转换类型的错误
                 void insert(int location,cstring b);//插入一个字符串
                 void assign(const char *b);//重置字符串
                 friend cstring operator +(cstring &a,cstring &b);
                 int length()const;
                 void put();
                 friend  std::ostream & operator<<(std::ostream & os,cstring & str);
                 cstring &operator =(const char*q);
                 cstring &operator =(const cstring &b);

      private:
                 char*str;//由于不是数组指针所以在用的时候必须先申请空间才行
};
std::ostream & operator<<(std::ostream & os,cstring & str)
{
    os<<str.str;
    return os;
}

cstring::cstring(const cstring &b)//要用b这个类必须把它引用过来
{
    int len=b.length();
    str=new char[len+1];
    int i;
    for(i=0;i<=len;i++)
    {
        *(str+i)=*(b.str+i);
    }
};//
cstring::cstring(const char*p )//不能读入,因为无法做到读入,只能够赋值
{
    if(p==NULL)
    {
        str=new char[1];
        *str='\0';
    }
    else
    {
        str=new char[strlen(p)+1];
        strcpy(str,p);
    }

}//close
/*void string::insert(int location,string b)//写错了,应该是拼接,字符串重载运算符的+法中用
{
    int  i,j;
    for(i=0;;i++)
    {
            if(*(str+i)=='\0')
           {
                *(str+i)=*(p+i);
                if(*(b+i)=='\0'){break;}
            }
    }
    return;

};*/
void cstring::insert(int location,cstring b)
{
    cstring a1(*this);//要用this指正否则不能指向当前空间
    cout<<a1.str<<'\n';
    cout<<b.str<<'\n';
    system("pause");//mo problem!
    int strlen=length(),blen=b.length(),nlen=b.length()+length();
    str=new char[nlen];
    int i;
    for(i=0;i<=location;i++)
    {
        *(str+i)=*(a1.str+i);
        cout<<str<<'\n';
        system("pause");
    }
    int j,r;
    cout<<"进入二阶段\n";
    for(j=location,r=0;j<=location+blen;j++,r++)
    {
         *(str+j)=*(b.str+r);
         cout<<str<<'\n';
        system("pause");
    }
    for(j=location+blen;j<=nlen;j++,i++)
    {
        *(str+j)=*(a1.str+i);
        cout<<str<<'\n';
        system("pause");
    }
    delete a1.str;
    delete b.str;
    cout<<str;

};//插入完成,正确
void cstring::assign(const char *b)
{
    delete str;
    str=new char[strlen(b)+1];
    strcpy(str,b);
    cout<<'\n'<<str;
    return;
};
int cstring::length()const
{
    int  i,num=0;
    for(i=0;;i++)
    {
        if(*(str+i)=='\0'){cout<<'\n';break;}
        num++;
    }
   // cout<<num<<'\n';
    return num;
};
void  cstring::put()
{
    cout<<str;
}
//运算符重载:
cstring operator +(cstring &a,cstring &b)
{
      cstring c;
      int len=strlen(a.str)+strlen(b.str);
      c.str=new char[len+1];
      int i,j;
      for(i=0;;i++)
      {
          *(c.str+i)=*(a.str+i);
          if(*(a.str+i)=='\0')
          {
              break;
          }
      }
      for(j=i;;j++)
      {
          *(c.str+j)=*(b.str+j);
          if(*(b.str+j)=='\0')
          {
              break;
          }
      }
      cout<<c.str;
      return(c);
};
cstring &cstring::operator =(const char*q)
{
      int len=strlen(q);
    //  cout<<"len="<<len;
      str=new char[len];
      int i;
      for(i=0;i<=len-1;i++)
      {
          *(str+i)=*(q+i);
      }
//    cout<<str;
    return (*this);
};

cstring &cstring::operator =(const cstring &b)
{
       int len=strlen(b.str);
    //  cout<<"len="<<len;
      str=new char[len];
      int i;
      for(i=0;i<=len-1;i++)
      {
          *(str+i)=*(b.str+i);
      }
//    cout<<str;
    return (*this);


}

int main()
{
    cstring a="abcdefg";
  //  a="abcdefg";
 //   cstring b=a;
    a.put();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值