C++中自定义string类的实现

C++中自定义string类的实现

mystring.h头文件

#ifndef MYSTRING_H
#define MYSTRING_H
#include <ostream>
#include <cstring>
using namespace std;


class MyString
{
public:
    MyString(); //无参构造函数
    MyString(const char *str); //有参构造函数
    MyString(const MyString &other); //拷贝构造函数
    MyString &operator=(const char *str);//重载“=”函数
    MyString &operator=(const MyString &other);//重载“=”函数
    MyString &operator+(const MyString &other);//重载“+”函数
    size_t myLen();  //获取字符串长度
    friend ostream &operator<<(ostream &output,const MyString &str);//重载输出函数
    char &operator[](int a); //重载“[]”函数

    ~MyString(); //析构函数
private:
    char *m_pString;
};

#endif // MYSTRING_H

mystring.c

#include "mystring.h"
#include <iostream>

MyString::MyString()
{
   int length = 0;
   m_pString = new char[length + 1];
   strcpy(m_pString,"");
}

MyString::MyString(const char *str)
{
    if(str == nullptr)
    {
        int length = 0;
        m_pString = new char[length + 1];
        strcpy(m_pString,"");
    }
    else {
        size_t length = strlen(str);
        m_pString = new char[length + 1];
        strcpy(m_pString,str);
    }

}

MyString::MyString(const MyString &other)
{
    if(this->m_pString != nullptr)
    {
        delete [] this->m_pString;
        this->m_pString = nullptr;
    }

    size_t length = strlen(other.m_pString);
    this->m_pString = new char[length + 1];
    strcpy(this->m_pString,other.m_pString);

}

MyString &MyString::operator=(const char *str)
{
    if(this->m_pString != nullptr)
    {
        delete [] this->m_pString;
        this->m_pString = nullptr;
    }

    size_t length = strlen(str);
    this->m_pString = new char[length + 1];
    strcpy(this->m_pString,str);
    return *this;
}

MyString &MyString::operator=(const MyString &other)
{
    if(this->m_pString != nullptr)
    {
        delete [] this->m_pString;
        this->m_pString = nullptr;
    }

    size_t length = strlen(other.m_pString);
    this->m_pString = new char[length + 1];
    strcpy(this->m_pString,other.m_pString);
    return *this;
}

MyString &MyString::operator+(const MyString &other)
{
    MyString *pStrTemp = new MyString;
    pStrTemp->m_pString = new char[strlen(this->m_pString) + strlen(other.m_pString) + 1];
    strcpy(pStrTemp->m_pString,this->m_pString);
    strcat(pStrTemp->m_pString,other.m_pString);
    return *pStrTemp;
}

size_t MyString::myLen()
{
    return strlen(this->m_pString);
}

ostream &operator<<(ostream &output, const MyString &str)
{
    output << str.m_pString << endl;
    return output;
}

char &MyString::operator[](int a)
{
    return this->m_pString[a];
}

MyString::~MyString()
{
    if(m_pString != nullptr)
    {
        delete [] m_pString;
    }
    m_pString = nullptr;
}

测试程序

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

int main()
{
    //测试无参构造函数,以及重载“<<”操作符
    MyString s1;
    cout << "s1:" << s1;

    //测试有参构造函数
    MyString s2("Hello");
    cout << "s2:" << s2;

    //测试拷贝构造函数,以及重载“=”操作符
    s1 = "World";
    MyString s3(s1);
    cout << "s3:" << s3;

    //测试重载“+”操作符
    MyString s4;
    s4 = s2 + " " + s3 + "!";
    cout << "s4:" << s4;

    //测试重载“[]”操作符
    cout << "s4[1]:" << s4[1] << endl;

    cout << "len of s4:" << s4.myLen();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值