String类自实现

c++复习巩固!
以下为 myString.h

#ifndef MYSTRING_H_
#define MYSTRING_H_
#include <iostream>
using std::ostream;
using std::istream;
class Ms
{
friend istream &operator>>(istream &ai,Ms &an);
friend ostream &operator<<(ostream &a,const Ms &an);
public:
    Ms(const char *na=nullptr);//普通构造
    Ms(const Ms &an);          //拷贝构造
    Ms &operator=(const Ms &an);//返回引用的原因:支持链等,并且链等过程中不会调用拷贝构造函数。
    Ms operator +(const Ms &an);//返回值的原因:当a=b+c时,会产生新的对象
	//返回值:产生新的对象。 返回引用:支持链等,未产生新对象或更改已有对象的值
    Ms &operator +=(const Ms &an);
    bool operator>(const Ms &an);
    bool operator<(const Ms &an);
    bool operator==(const Ms &an);
    char at(int n);
    char *c_str();
    ~Ms();
    void dis();
private:
    char *_str;
};

#endif

以下为myString.cpp

#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "mystring.h"
using std::cout;

istream &operator>>(istream &ai,Ms &an)
{
    char p[1024];
    ai>>p;
    an._str=p;
    return ai;
}
ostream &operator<<(ostream &os,const Ms &an)
{
    os<<an._str;
    return os;
}
Ms::Ms(const char *na)
{
    if(nullptr==na)
    {
        _str=new char[1];
        *_str='\0';
    }
    else
    {
        int len=strlen(na);
        _str=new char[len+1];
        strcpy(_str,na);
    }
}
void Ms::dis()
{
    cout<<_str;
}
Ms::Ms(const Ms &an)
{
    int len=strlen(an._str);
    _str=new char[len+1];
    strcpy(_str,an._str);
}
Ms & Ms::operator=(const Ms &an)
{
    if(this->_str==an._str)
    {
        return *this;
    }
        delete [] this->_str;
        int len=strlen(an._str);
        this->_str=new char[len+1];
        strcpy(this->_str,an._str);
        return *this;
}
Ms Ms::operator +(const Ms &an)
{
    Ms temp;
    delete [] temp._str;
    int lenF=strlen(this->_str);
    int lenB=strlen(an._str);
    int len=lenB+lenF;
    temp._str=new char[len+1]{0};
    strcat(strcpy(temp._str,this->_str),an._str);
    return temp;
}
Ms & Ms::operator +=(const Ms &an)
{
    int lenF=strlen(this->_str);
    int lenB=strlen(an._str);
    int len=lenB+lenF;
    this->_str=static_cast<char*>(realloc(this->_str,len+1));
    memset(this->_str+lenF,0,lenB+1);
    strcat(this->_str,an._str);
    return *this;
}
bool Ms::operator>(const Ms &an)
{
    if((strcmp(this->_str,an._str))>0)
        return true;
    else
        return false;
}
bool Ms::operator<(const Ms &an)
{
    if((strcmp(this->_str,an._str))<0)
        return true;
    else
        return false;
}
bool Ms::operator==(const Ms &an)
{
    if((strcmp(this->_str,an._str))==0)
        return true;
    else
        return false;
}

char Ms::at(int n)
{
    return _str[n];
}
char * Ms::c_str()
{
    return _str;
}
Ms::~Ms()
{
    delete [] _str;
}

以下为main.cpp

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

int main()
{
    Ms ma("abc");          //构造
    cout<<ma<<endl;        //<<
    Ms mb(ma);             //拷贝构造
    cout<<mb<<endl;
    Ms mc="def";
    Ms md;
    md=mc=ma;             //==
    cout<<md<<endl;

    cout<<endl;
    Ms na="abc";
    Ms nb="def";
    na+=nb;               //+=
    cout<<na<<endl;
    Ms nc;
    nc=na+nb;             //+
    cout<<nc<<endl;
    cout<<endl;
    
    char *p=nullptr;
    Ms ms ="han";
    p=ms.c_str();
    cout<<p<<endl;
    cout<<endl;
    
    Ms ca;
    cin>>ca;               //>>
    cout<<ca<<endl;


    return 0;
}

测试结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值