C++ 自定义string类

String.h

#ifndef STRING_H
#define STRING_H

#include <iostream>
#include <cstring>

using namespace std;

class String
{
public:
    String();                   //默认构造函数
    String(const char *str);    //构造函数:C字符串初始化
    String(const String &str);  //构造函数:String类对象初始化
    ~String();

    String& operator =(const char *str);    //重载赋值运算符:C字符串初始化
    String& operator =(const String &str);  //重载赋值运算符:String类对象初始化

    char& operator [](unsigned int index);              //重载[]运算符:可修改元素
    const char& operator[](unsigned int index) const;   //重载[]运算符:不可修改元素

    String operator +(const String &str);   //重载+运算符
    
    friend bool operator <(const String &str1, const String &str2);  //重载<运算符
    friend bool operator >(const String &str1, const String &str2);  //重载>运算符
    friend bool operator ==(const String &str1, const String &str2); //重载==运算符

    friend ostream& operator <<(ostream &os, const String &str); //重载<<运算符
    friend istream& operator >>(istream &os, const String &str); //重载>>运算符

private:
    char *m_str;
};

#endif // STRING_H

String.cpp

#include "String.h"

String::String()
{
    m_str = new char[1];
    m_str[0] = '\0';
}

String::String(const char *str)
{
    m_str = new char[strlen(str) + 1];
    strcpy(m_str, str);
}

String::String(const String &str)
{
    m_str = new char[strlen(str.m_str) + 1];
    strcpy(m_str, str.m_str);
}

String& String::operator =(const char *str)
{
    delete [] m_str;  //删除旧的str空间

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

String& String::operator =(const String &str)
{
    if (this == &str)
    {
        return *this; //将自身赋值给自身
    }
    delete [] m_str;  //删除旧的str空间

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

String String::operator +(const String &str)
{
    String tempStr;
    if (!str.m_str)
    {
        return *this;
    }
    else if (!this->m_str)
    {
        return str;
    }
    else
    {
        tempStr.m_str = new char[strlen(this->m_str) + strlen(str.m_str) + 1];
        strcpy(tempStr.m_str, this->m_str);
        strcpy(tempStr.m_str, str.m_str);
        return tempStr;
    }
}

char& String::operator [](unsigned int index)
{
    return this->m_str[index];
}

const char& String::operator[](unsigned int index) const
{
    return this->m_str[index];
}

bool operator <(const String &str1, const String &str2)
{
    if (strcmp(str1, str2) < 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool operator >(const String &str1, const String &str2)
{
    if (strcmp(str1, str2) > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool operator ==(const String &str1, const String &str2)
{
    if (strcmp(str1, str2) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

ostream& operator <<(ostream &os, const String &str)
{
    os << str.m_str << endl;
    return os;
}

istream& operator >>(istream &is, const String &str)
{
    char temp[100];
    is.get(temp, 100);

    if (is)
    {
        is = str;
    }

    while (is && is.get() != '\n')
    {
        continue;
    }

    return is;
}

String::~String()
{
    delete [] m_str;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值